1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements tests for various issues.
21 func TestIssue5770(t *testing.T) {
22 src := `package p; type S struct{T}`
23 f, err := parser.ParseFile(fset, "", src, 0)
28 conf := Config{Importer: importer.Default()}
29 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash
30 want := "undeclared name: T"
31 if err == nil || !strings.Contains(err.Error(), want) {
32 t.Errorf("got: %v; want: %s", err, want)
36 func TestIssue5849(t *testing.T) {
44 _ = uint64(64 << s + s)
45 _ = (interface{})("foo")
46 _ = (interface{})(nil)
48 f, err := parser.ParseFile(fset, "", src, 0)
54 types := make(map[ast.Expr]TypeAndValue)
55 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
60 for x, tv := range types {
62 switch x := x.(type) {
72 want = Typ[Uint] // because of "+ s", s is of type uint
78 want = Typ[UntypedNil]
81 if want != nil && !Identical(tv.Type, want) {
82 t.Errorf("got %s; want %s", tv.Type, want)
87 func TestIssue6413(t *testing.T) {
96 f, err := parser.ParseFile(fset, "", src, 0)
102 types := make(map[ast.Expr]TypeAndValue)
103 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
110 for x, tv := range types {
111 if _, ok := x.(*ast.CallExpr); ok {
113 t.Errorf("%s: got %s; want %s", fset.Position(x.Pos()), tv.Type, want)
120 t.Errorf("got %d CallExprs; want 2", n)
124 func TestIssue7245(t *testing.T) {
127 func (T) m() (res bool) { return }
128 type T struct{} // receiver type after method declaration
130 f, err := parser.ParseFile(fset, "", src, 0)
136 defs := make(map[*ast.Ident]Object)
137 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs})
142 m := f.Decls[0].(*ast.FuncDecl)
143 res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
144 res2 := defs[m.Type.Results.List[0].Names[0]].(*Var)
147 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
151 // This tests that uses of existing vars on the LHS of an assignment
152 // are Uses, not Defs; and also that the (illegal) use of a non-var on
153 // the LHS of an assignment is a Use nonetheless.
154 func TestIssue7827(t *testing.T) {
158 const w = 1 // defs w
159 x, y := 2, 3 // defs x, y
160 w, x, z := 4, 5, 6 // uses w, x, defs z; error: cannot assign to w
161 _, _, _ = x, y, z // uses x, y, z
164 const want = `L3 defs func p._()
165 L4 defs const w untyped int
169 L6 uses const w untyped int
175 f, err := parser.ParseFile(fset, "", src, 0)
180 // don't abort at the first error
181 conf := Config{Error: func(err error) { t.Log(err) }}
182 defs := make(map[*ast.Ident]Object)
183 uses := make(map[*ast.Ident]Object)
184 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs, Uses: uses})
185 if s := fmt.Sprint(err); !strings.HasSuffix(s, "cannot assign to w") {
186 t.Errorf("Check: unexpected error: %s", s)
190 for id, obj := range defs {
192 fact := fmt.Sprintf("L%d defs %s", fset.Position(id.Pos()).Line, obj)
193 facts = append(facts, fact)
196 for id, obj := range uses {
197 fact := fmt.Sprintf("L%d uses %s", fset.Position(id.Pos()).Line, obj)
198 facts = append(facts, fact)
202 got := strings.Join(facts, "\n")
204 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)