]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/issues_test.go
go/types: move go/types/internal/gcimport => go/internal/gcimporter
[gostls13.git] / src / go / types / issues_test.go
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.
4
5 // This file implements tests for various issues.
6
7 package types_test
8
9 import (
10         "fmt"
11         "go/ast"
12         "go/parser"
13         "sort"
14         "strings"
15         "testing"
16
17         _ "go/internal/gcimporter"
18         . "go/types"
19 )
20
21 func TestIssue5770(t *testing.T) {
22         src := `package p; type S struct{T}`
23         f, err := parser.ParseFile(fset, "", src, 0)
24         if err != nil {
25                 t.Fatal(err)
26         }
27
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)
32         }
33 }
34
35 func TestIssue5849(t *testing.T) {
36         src := `
37 package p
38 var (
39         s uint
40         _ = uint8(8)
41         _ = uint16(16) << s
42         _ = uint32(32 << s)
43         _ = uint64(64 << s + s)
44         _ = (interface{})("foo")
45         _ = (interface{})(nil)
46 )`
47         f, err := parser.ParseFile(fset, "", src, 0)
48         if err != nil {
49                 t.Fatal(err)
50         }
51
52         var conf Config
53         types := make(map[ast.Expr]TypeAndValue)
54         _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
55         if err != nil {
56                 t.Fatal(err)
57         }
58
59         for x, tv := range types {
60                 var want Type
61                 switch x := x.(type) {
62                 case *ast.BasicLit:
63                         switch x.Value {
64                         case `8`:
65                                 want = Typ[Uint8]
66                         case `16`:
67                                 want = Typ[Uint16]
68                         case `32`:
69                                 want = Typ[Uint32]
70                         case `64`:
71                                 want = Typ[Uint] // because of "+ s", s is of type uint
72                         case `"foo"`:
73                                 want = Typ[String]
74                         }
75                 case *ast.Ident:
76                         if x.Name == "nil" {
77                                 want = Typ[UntypedNil]
78                         }
79                 }
80                 if want != nil && !Identical(tv.Type, want) {
81                         t.Errorf("got %s; want %s", tv.Type, want)
82                 }
83         }
84 }
85
86 func TestIssue6413(t *testing.T) {
87         src := `
88 package p
89 func f() int {
90         defer f()
91         go f()
92         return 0
93 }
94 `
95         f, err := parser.ParseFile(fset, "", src, 0)
96         if err != nil {
97                 t.Fatal(err)
98         }
99
100         var conf Config
101         types := make(map[ast.Expr]TypeAndValue)
102         _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Types: types})
103         if err != nil {
104                 t.Fatal(err)
105         }
106
107         want := Typ[Int]
108         n := 0
109         for x, tv := range types {
110                 if _, ok := x.(*ast.CallExpr); ok {
111                         if tv.Type != want {
112                                 t.Errorf("%s: got %s; want %s", fset.Position(x.Pos()), tv.Type, want)
113                         }
114                         n++
115                 }
116         }
117
118         if n != 2 {
119                 t.Errorf("got %d CallExprs; want 2", n)
120         }
121 }
122
123 func TestIssue7245(t *testing.T) {
124         src := `
125 package p
126 func (T) m() (res bool) { return }
127 type T struct{} // receiver type after method declaration
128 `
129         f, err := parser.ParseFile(fset, "", src, 0)
130         if err != nil {
131                 t.Fatal(err)
132         }
133
134         var conf Config
135         defs := make(map[*ast.Ident]Object)
136         _, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, &Info{Defs: defs})
137         if err != nil {
138                 t.Fatal(err)
139         }
140
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)
144
145         if res1 != res2 {
146                 t.Errorf("got %s (%p) != %s (%p)", res1, res2, res1, res2)
147         }
148 }
149
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) {
154         const src = `
155 package p
156 func _() {
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
161 }
162 `
163         const want = `L3 defs func p._()
164 L4 defs const w untyped int
165 L5 defs var x int
166 L5 defs var y int
167 L6 defs var z int
168 L6 uses const w untyped int
169 L6 uses var x int
170 L7 uses var x int
171 L7 uses var y int
172 L7 uses var z int`
173
174         f, err := parser.ParseFile(fset, "", src, 0)
175         if err != nil {
176                 t.Fatal(err)
177         }
178
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)
186         }
187
188         var facts []string
189         for id, obj := range defs {
190                 if obj != nil {
191                         fact := fmt.Sprintf("L%d defs %s", fset.Position(id.Pos()).Line, obj)
192                         facts = append(facts, fact)
193                 }
194         }
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)
198         }
199         sort.Strings(facts)
200
201         got := strings.Join(facts, "\n")
202         if got != want {
203                 t.Errorf("Unexpected defs/uses\ngot:\n%s\nwant:\n%s", got, want)
204         }
205 }