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
fd31d9fd
Commit
fd31d9fd
authored
Oct 27, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/parser: test and fix := scoping bug
R=iant CC=golang-dev, gri
https://golang.org/cl/5327048
parent
d066e02a
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
src/pkg/go/parser/parser.go
src/pkg/go/parser/parser.go
+9
-1
src/pkg/go/parser/parser_test.go
src/pkg/go/parser/parser_test.go
+44
-0
No files found.
src/pkg/go/parser/parser.go
View file @
fd31d9fd
...
...
@@ -434,7 +434,9 @@ func (p *parser) parseLhsList() []ast.Expr {
switch
p
.
tok
{
case
token
.
DEFINE
:
// lhs of a short variable declaration
p
.
shortVarDecl
(
p
.
makeIdentList
(
list
))
// but doesn't enter scope until later:
// caller must call p.shortVarDecl(p.makeIdentList(list))
// at appropriate time.
case
token
.
COLON
:
// lhs of a label declaration or a communication clause of a select
// statement (parseLhsList is not called when parsing the case clause
...
...
@@ -1398,6 +1400,9 @@ func (p *parser) parseSimpleStmt(mode int) (ast.Stmt, bool) {
}
else
{
y
=
p
.
parseRhsList
()
}
if
tok
==
token
.
DEFINE
{
p
.
shortVarDecl
(
p
.
makeIdentList
(
x
))
}
return
&
ast
.
AssignStmt
{
x
,
pos
,
tok
,
y
},
isRange
}
...
...
@@ -1722,6 +1727,9 @@ func (p *parser) parseCommClause() *ast.CommClause {
}
p
.
next
()
rhs
=
p
.
parseRhs
()
if
tok
==
token
.
DEFINE
&&
lhs
!=
nil
{
p
.
shortVarDecl
(
p
.
makeIdentList
(
lhs
))
}
}
else
{
// rhs must be single receive operation
if
len
(
lhs
)
>
1
{
...
...
src/pkg/go/parser/parser_test.go
View file @
fd31d9fd
...
...
@@ -5,6 +5,7 @@
package
parser
import
(
"go/ast"
"go/token"
"os"
"testing"
...
...
@@ -134,3 +135,46 @@ func TestParse4(t *testing.T) {
}
}
}
func
TestColonEqualsScope
(
t
*
testing
.
T
)
{
f
,
err
:=
ParseFile
(
fset
,
""
,
`package p; func f() { x, y, z := x, y, z }`
,
0
)
if
err
!=
nil
{
t
.
Errorf
(
"parse: %s"
,
err
)
}
// RHS refers to undefined globals; LHS does not.
as
:=
f
.
Decls
[
0
]
.
(
*
ast
.
FuncDecl
)
.
Body
.
List
[
0
]
.
(
*
ast
.
AssignStmt
)
for
_
,
v
:=
range
as
.
Rhs
{
id
:=
v
.
(
*
ast
.
Ident
)
if
id
.
Obj
!=
nil
{
t
.
Errorf
(
"rhs %s has Obj, should not"
,
id
.
Name
)
}
}
for
_
,
v
:=
range
as
.
Lhs
{
id
:=
v
.
(
*
ast
.
Ident
)
if
id
.
Obj
==
nil
{
t
.
Errorf
(
"lhs %s does not have Obj, should"
,
id
.
Name
)
}
}
}
func
TestVarScope
(
t
*
testing
.
T
)
{
f
,
err
:=
ParseFile
(
fset
,
""
,
`package p; func f() { var x, y, z = x, y, z }`
,
0
)
if
err
!=
nil
{
t
.
Errorf
(
"parse: %s"
,
err
)
}
// RHS refers to undefined globals; LHS does not.
as
:=
f
.
Decls
[
0
]
.
(
*
ast
.
FuncDecl
)
.
Body
.
List
[
0
]
.
(
*
ast
.
DeclStmt
)
.
Decl
.
(
*
ast
.
GenDecl
)
.
Specs
[
0
]
.
(
*
ast
.
ValueSpec
)
for
_
,
v
:=
range
as
.
Values
{
id
:=
v
.
(
*
ast
.
Ident
)
if
id
.
Obj
!=
nil
{
t
.
Errorf
(
"rhs %s has Obj, should not"
,
id
.
Name
)
}
}
for
_
,
id
:=
range
as
.
Names
{
if
id
.
Obj
==
nil
{
t
.
Errorf
(
"lhs %s does not have Obj, should"
,
id
.
Name
)
}
}
}
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