1
0
mirror of https://github.com/golang/go synced 2024-11-26 12:48:11 -07:00

go/parser: fix object kind

Bug introduced with CL 6624047.

R=r
CC=golang-dev
https://golang.org/cl/6620073
This commit is contained in:
Robert Griesemer 2012-10-07 17:58:13 -07:00
parent 9e811683f1
commit 05fc42ab02
2 changed files with 52 additions and 1 deletions

View File

@ -2116,7 +2116,11 @@ func (p *parser) parseValueSpec(doc *ast.CommentGroup, keyword token.Token, iota
Values: values,
Comment: p.lineComment,
}
p.declare(spec, iota, p.topScope, ast.Con, idents...)
kind := ast.Con
if keyword == token.VAR {
kind = ast.Var
}
p.declare(spec, iota, p.topScope, kind, idents...)
return spec
}

View File

@ -135,6 +135,53 @@ func TestVarScope(t *testing.T) {
}
}
func TestObjects(t *testing.T) {
const src = `
package p
import fmt "fmt"
const pi = 3.14
type T struct{}
var x int
func f() { L: }
`
f, err := ParseFile(fset, "", src, 0)
if err != nil {
t.Fatal(err)
}
objects := map[string]ast.ObjKind{
"p": ast.Bad, // not in a scope
"fmt": ast.Bad, // not resolved yet
"pi": ast.Con,
"T": ast.Typ,
"x": ast.Var,
"int": ast.Bad, // not resolved yet
"f": ast.Fun,
"L": ast.Lbl,
}
ast.Inspect(f, func(n ast.Node) bool {
if ident, ok := n.(*ast.Ident); ok {
obj := ident.Obj
if obj == nil {
if objects[ident.Name] != ast.Bad {
t.Errorf("no object for %s", ident.Name)
}
return true
}
if obj.Name != ident.Name {
t.Errorf("names don't match: obj.Name = %s, ident.Name = %s", obj.Name, ident.Name)
}
kind := objects[ident.Name]
if obj.Kind != kind {
t.Errorf("%s: obj.Kind = %s; want %s", ident.Name, obj.Kind, kind)
}
}
return true
})
}
func TestUnresolved(t *testing.T) {
f, err := ParseFile(fset, "", `
package p