]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: fix object kind
authorRobert Griesemer <gri@golang.org>
Mon, 8 Oct 2012 00:58:13 +0000 (17:58 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 8 Oct 2012 00:58:13 +0000 (17:58 -0700)
Bug introduced with CL 6624047.

R=r
CC=golang-dev
https://golang.org/cl/6620073

src/pkg/go/parser/parser.go
src/pkg/go/parser/parser_test.go

index a305a95f0f3a8daaa95e8537f1a565ee996e0a8b..26b31b247ad01d3cdb7e4ad1fc8a2ea968e90995 100644 (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
 }
index 1b7a41b1bf15007e1ae92551f1f1703429a6287f..1960377b0a69287425a3ded030b813e9ef0a8dcd 100644 (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