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.
22 func TestIssue5770(t *testing.T) {
23 src := `package p; type S struct{T}`
24 f, err := parser.ParseFile(fset, "", src, 0)
29 conf := Config{Importer: importer.Default()}
30 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash
31 want := "undeclared name: T"
32 if err == nil || !strings.Contains(err.Error(), want) {
33 t.Errorf("got: %v; want: %s", err, want)
37 func TestIssue5849(t *testing.T) {
45 _ = uint64(64 << s + s)
46 _ = (interface{})("foo")
47 _ = (interface{})(nil)
49 f, err := parser.ParseFile(fset, "", src, 0)
55 types := make(map[ast.Expr]TypeAndValue)
56 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
61 for x, tv := range types {
63 switch x := x.(type) {
73 want = Typ[Uint] // because of "+ s", s is of type uint
79 want = Typ[UntypedNil]
82 if want != nil && !Identical(tv.Type, want) {
83 t.Errorf("got %s; want %s", tv.Type, want)
88 func TestIssue6413(t *testing.T) {
97 f, err := parser.ParseFile(fset, "", src, 0)
103 types := make(map[ast.Expr]TypeAndValue)
104 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
111 for x, tv := range types {
112 if _, ok := x.(*ast.CallExpr); ok {
114 t.Errorf("%s: got %s; want %s", fset.Position(x.Pos()), tv.Type, want)
121 t.Errorf("got %d CallExprs; want 2", n)
125 func TestIssue7245(t *testing.T) {
128 func (T) m() (res bool) { return }
129 type T struct{} // receiver type after method declaration
131 f, err := parser.ParseFile(fset, "", src, 0)
137 defs := make(map[*ast.Ident]Object)
138 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs})
143 m := f.Decls[0].(*ast.FuncDecl)
144 res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
145 res2 := defs[m.Type.Results.List[0].Names[0]].(*Var)
148 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
152 // This tests that uses of existing vars on the LHS of an assignment
153 // are Uses, not Defs; and also that the (illegal) use of a non-var on
154 // the LHS of an assignment is a Use nonetheless.
155 func TestIssue7827(t *testing.T) {
159 const w = 1 // defs w
160 x, y := 2, 3 // defs x, y
161 w, x, z := 4, 5, 6 // uses w, x, defs z; error: cannot assign to w
162 _, _, _ = x, y, z // uses x, y, z
165 const want = `L3 defs func p._()
166 L4 defs const w untyped int
170 L6 uses const w untyped int
176 f, err := parser.ParseFile(fset, "", src, 0)
181 // don't abort at the first error
182 conf := Config{Error: func(err error) { t.Log(err) }}
183 defs := make(map[*ast.Ident]Object)
184 uses := make(map[*ast.Ident]Object)
185 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs, Uses: uses})
186 if s := fmt.Sprint(err); !strings.HasSuffix(s, "cannot assign to w") {
187 t.Errorf("Check: unexpected error: %s", s)
191 for id, obj := range defs {
193 fact := fmt.Sprintf("L%d defs %s", fset.Position(id.Pos()).Line, obj)
194 facts = append(facts, fact)
197 for id, obj := range uses {
198 fact := fmt.Sprintf("L%d uses %s", fset.Position(id.Pos()).Line, obj)
199 facts = append(facts, fact)
203 got := strings.Join(facts, "\n")
205 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)
209 // This tests that the package associated with the types.Object.Pkg method
210 // is the type's package independent of the order in which the imports are
211 // listed in the sources src1, src2 below.
212 // The actual issue is in go/internal/gcimporter which has a corresponding
213 // test; we leave this test here to verify correct behavior at the go/types
215 func TestIssue13898(t *testing.T) {
216 testenv.MustHaveGoBuild(t)
225 for _, obj := range info.Uses {
230 // like src0, but also imports go/importer
241 for _, obj := range info.Uses {
246 // like src1 but with different import order
247 // (used to fail with this issue)
258 for _, obj := range info.Uses {
263 f := func(test, src string) {
264 f, err := parser.ParseFile(fset, "", src, 0)
268 cfg := Config{Importer: importer.Default()}
269 info := Info{Uses: make(map[*ast.Ident]Object)}
270 _, err = cfg.Check("main", fset, []*ast.File{f}, &info)
277 for id, obj := range info.Uses {
278 if id.Name == "Pkg" {
284 t.Fatalf("%s: got %d entries named Pkg; want 1", test, count)
286 if pkg.Name() != "types" {
287 t.Fatalf("%s: got %v; want package types", test, pkg)
296 func TestIssue22525(t *testing.T) {
297 src := `package p; func f() { var a, b, c, d, e int }`
298 f, err := parser.ParseFile(fset, "", src, 0)
304 conf := Config{Error: func(err error) { got += err.Error() + "\n" }}
305 conf.Check(f.Name.Name, fset, []*ast.File{f}, nil) // do not crash
307 1:27: a declared but not used
308 1:30: b declared but not used
309 1:33: c declared but not used
310 1:36: d declared but not used
311 1:39: e declared but not used
314 t.Errorf("got: %swant: %s", got, want)