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.
17 _ "go/internal/gcimporter"
21 func TestIssue5770(t *testing.T) {
22 src := `package p; type S struct{T}`
23 f, err := parser.ParseFile(fset, "", src, 0)
28 _, err = Check(f.Name.Name, fset, []*ast.File{f}) // do not crash
29 want := "undeclared name: T"
30 if err == nil || !strings.Contains(err.Error(), want) {
31 t.Errorf("got: %v; want: %s", err, want)
35 func TestIssue5849(t *testing.T) {
43 _ = uint64(64 << s + s)
44 _ = (interface{})("foo")
45 _ = (interface{})(nil)
47 f, err := parser.ParseFile(fset, "", src, 0)
53 types := make(map[ast.Expr]TypeAndValue)
54 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
59 for x, tv := range types {
61 switch x := x.(type) {
71 want = Typ[Uint] // because of "+ s", s is of type uint
77 want = Typ[UntypedNil]
80 if want != nil && !Identical(tv.Type, want) {
81 t.Errorf("got %s; want %s", tv.Type, want)
86 func TestIssue6413(t *testing.T) {
95 f, err := parser.ParseFile(fset, "", src, 0)
101 types := make(map[ast.Expr]TypeAndValue)
102 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
109 for x, tv := range types {
110 if _, ok := x.(*ast.CallExpr); ok {
112 t.Errorf("%s: got %s; want %s", fset.Position(x.Pos()), tv.Type, want)
119 t.Errorf("got %d CallExprs; want 2", n)
123 func TestIssue7245(t *testing.T) {
126 func (T) m() (res bool) { return }
127 type T struct{} // receiver type after method declaration
129 f, err := parser.ParseFile(fset, "", src, 0)
135 defs := make(map[*ast.Ident]Object)
136 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs})
141 m := f.Decls[0].(*ast.FuncDecl)
142 res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
143 res2 := defs[m.Type.Results.List[0].Names[0]].(*Var)
146 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
150 // This tests that uses of existing vars on the LHS of an assignment
151 // are Uses, not Defs; and also that the (illegal) use of a non-var on
152 // the LHS of an assignment is a Use nonetheless.
153 func TestIssue7827(t *testing.T) {
157 const w = 1 // defs w
158 x, y := 2, 3 // defs x, y
159 w, x, z := 4, 5, 6 // uses w, x, defs z; error: cannot assign to w
160 _, _, _ = x, y, z // uses x, y, z
163 const want = `L3 defs func p._()
164 L4 defs const w untyped int
168 L6 uses const w untyped int
174 f, err := parser.ParseFile(fset, "", src, 0)
179 // don't abort at the first error
180 conf := Config{Error: func(err error) { t.Log(err) }}
181 defs := make(map[*ast.Ident]Object)
182 uses := make(map[*ast.Ident]Object)
183 _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs, Uses: uses})
184 if s := fmt.Sprint(err); !strings.HasSuffix(s, "cannot assign to w") {
185 t.Errorf("Check: unexpected error: %s", s)
189 for id, obj := range defs {
191 fact := fmt.Sprintf("L%d defs %s", fset.Position(id.Pos()).Line, obj)
192 facts = append(facts, fact)
195 for id, obj := range uses {
196 fact := fmt.Sprintf("L%d uses %s", fset.Position(id.Pos()).Line, obj)
197 facts = append(facts, fact)
201 got := strings.Join(facts, "\n")
203 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)