Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
05dc3bf5
Commit
05dc3bf5
authored
Oct 02, 2012
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/parser: correctly parse <-chan T(x) as <-(chan T)(x)
Fixes #4110. R=iant CC=golang-dev
https://golang.org/cl/6597069
parent
10ea3254
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
9 deletions
+50
-9
src/pkg/go/parser/parser.go
src/pkg/go/parser/parser.go
+42
-5
src/pkg/go/parser/short_test.go
src/pkg/go/parser/short_test.go
+8
-4
No files found.
src/pkg/go/parser/parser.go
View file @
05dc3bf5
...
@@ -1399,13 +1399,50 @@ func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
...
@@ -1399,13 +1399,50 @@ func (p *parser) parseUnaryExpr(lhs bool) ast.Expr {
// channel type or receive expression
// channel type or receive expression
pos
:=
p
.
pos
pos
:=
p
.
pos
p
.
next
()
p
.
next
()
if
p
.
tok
==
token
.
CHAN
{
p
.
next
()
// If the next token is token.CHAN we still don't know if it
value
:=
p
.
parseType
()
// is a channel type or a receive operation - we only know
return
&
ast
.
ChanType
{
Begin
:
pos
,
Dir
:
ast
.
RECV
,
Value
:
value
}
// once we have found the end of the unary expression. There
}
// are two cases:
//
// <- type => (<-type) must be channel type
// <- expr => <-(expr) is a receive from an expression
//
// In the first case, the arrow must be re-associated with
// the channel type parsed already:
//
// <- (chan type) => (<-chan type)
// <- (chan<- type) => (<-chan (<-type))
x
:=
p
.
parseUnaryExpr
(
false
)
x
:=
p
.
parseUnaryExpr
(
false
)
// determine which case we have
if
typ
,
ok
:=
x
.
(
*
ast
.
ChanType
);
ok
{
// (<-type)
// re-associate position info and <-
arrow
:=
true
for
ok
&&
arrow
{
begin
:=
typ
.
Begin
if
typ
.
Dir
==
ast
.
RECV
{
// error: (<-type) is (<-(<-chan T))
p
.
errorExpected
(
begin
,
"'chan'"
)
}
arrow
=
typ
.
Dir
==
ast
.
SEND
typ
.
Begin
=
pos
typ
.
Dir
=
ast
.
RECV
typ
,
ok
=
typ
.
Value
.
(
*
ast
.
ChanType
)
// TODO(gri) ast.ChanType should store exact <- position
pos
=
begin
// estimate (we don't have the exact position of <- for send channels)
}
if
arrow
{
p
.
errorExpected
(
pos
,
"'chan'"
)
}
return
x
}
// <-(expr)
return
&
ast
.
UnaryExpr
{
OpPos
:
pos
,
Op
:
token
.
ARROW
,
X
:
p
.
checkExpr
(
x
)}
return
&
ast
.
UnaryExpr
{
OpPos
:
pos
,
Op
:
token
.
ARROW
,
X
:
p
.
checkExpr
(
x
)}
case
token
.
MUL
:
case
token
.
MUL
:
...
...
src/pkg/go/parser/short_test.go
View file @
05dc3bf5
...
@@ -13,8 +13,10 @@ var valids = []string{
...
@@ -13,8 +13,10 @@ var valids = []string{
`package p;`
,
`package p;`
,
`package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`
,
`package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`
,
`package p; func f() { if f(T{}) {} };`
,
`package p; func f() { if f(T{}) {} };`
,
`package p; func f() { _ = (<-chan int)(x) };`
,
`package p; func f() { _ = <-chan int(nil) };`
,
`package p; func f() { _ = (<-chan <-chan int)(x) };`
,
`package p; func f() { _ = (<-chan int)(nil) };`
,
`package p; func f() { _ = (<-chan <-chan int)(nil) };`
,
`package p; func f() { _ = <-chan <-chan <-chan <-chan <-int(nil) };`
,
`package p; func f(func() func() func());`
,
`package p; func f(func() func() func());`
,
`package p; func f(...T);`
,
`package p; func f(...T);`
,
`package p; func f(float, ...int);`
,
`package p; func f(float, ...int);`
,
...
@@ -64,8 +66,10 @@ var invalids = []string{
...
@@ -64,8 +66,10 @@ var invalids = []string{
`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`
,
`package p; var a = []int{[ /* ERROR "expected expression" */ ]int};`
,
`package p; var a = ( /* ERROR "expected expression" */ []int);`
,
`package p; var a = ( /* ERROR "expected expression" */ []int);`
,
`package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`
,
`package p; var a = a[[ /* ERROR "expected expression" */ ]int:[]int];`
,
`package p; var a = <- /* ERROR "expected expression" */ chan int;`
,
`package p; var a = <- /* ERROR "expected expression" */ chan int;`
,
`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`
,
`package p; func f() { select { case _ <- chan /* ERROR "expected expression" */ int: } };`
,
`package p; func f() { _ = (<-<- /* ERROR "expected 'chan'" */ chan int)(nil) };`
,
`package p; func f() { _ = (<-chan<-chan<-chan<-chan<-chan /* ERROR "expected 'chan'" */ <-int)(nil) };`
,
}
}
func
TestInvalid
(
t
*
testing
.
T
)
{
func
TestInvalid
(
t
*
testing
.
T
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment