Commit 05fc42ab authored by Robert Griesemer's avatar Robert Griesemer

go/parser: fix object kind

Bug introduced with CL 6624047.

R=r
CC=golang-dev
https://golang.org/cl/6620073
parent 9e811683
......@@ -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
}
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment