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)
318 func TestIssue25627(t *testing.T) {
319 const prefix = `package p; import "unsafe"; type P *struct{}; type I interface{}; type T `
320 // The src strings (without prefix) are constructed such that the number of semicolons
321 // plus one corresponds to the number of fields expected in the respective struct.
322 for _, src := range []string{
323 `struct { x Missing }`,
324 `struct { Missing }`,
325 `struct { *Missing }`,
326 `struct { unsafe.Pointer }`,
329 `struct { a int; b Missing; *Missing }`,
331 f, err := parser.ParseFile(fset, "", prefix+src, 0)
336 cfg := Config{Importer: importer.Default(), Error: func(err error) {}}
337 info := &Info{Types: make(map[ast.Expr]TypeAndValue)}
338 _, err = cfg.Check(f.Name.Name, fset, []*ast.File{f}, info)
340 if _, ok := err.(Error); !ok {
345 ast.Inspect(f, func(n ast.Node) bool {
346 if spec, _ := n.(*ast.TypeSpec); spec != nil {
347 if tv, ok := info.Types[spec.Type]; ok && spec.Name.Name == "T" {
348 want := strings.Count(src, ";") + 1
349 if got := tv.Type.(*Struct).NumFields(); got != want {
350 t.Errorf("%s: got %d fields; want %d", src, got, want)
359 func TestIssue28005(t *testing.T) {
360 // method names must match defining interface name for this test
361 // (see last comment in this function)
362 sources := [...]string{
363 "package p; type A interface{ A() }",
364 "package p; type B interface{ B() }",
365 "package p; type X interface{ A; B }",
368 // compute original file ASTs
369 var orig [len(sources)]*ast.File
370 for i, src := range sources {
371 f, err := parser.ParseFile(fset, "", src, 0)
378 // run the test for all order permutations of the incoming files
379 for _, perm := range [][len(sources)]int{
387 // create file order permutation
388 files := make([]*ast.File, len(sources))
389 for i := range perm {
390 files[i] = orig[perm[i]]
393 // type-check package with given file order permutation
395 info := &Info{Defs: make(map[*ast.Ident]Object)}
396 _, err := conf.Check("", fset, files, info)
401 // look for interface object X
403 for name, def := range info.Defs {
404 if name.Name == "X" {
410 t.Fatal("interface not found")
412 iface := obj.Type().Underlying().(*Interface) // I must be an interface
414 // Each iface method m is embedded; and m's receiver base type name
415 // must match the method's name per the choice in the source file.
416 for i := 0; i < iface.NumMethods(); i++ {
418 recvName := m.Type().(*Signature).Recv().Type().(*Named).Obj().Name()
419 if recvName != m.Name() {
420 t.Errorf("perm %v: got recv %s; want %s", perm, recvName, m.Name())