| 1 |
// Copyright 2022 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 |
package typeparams |
| 6 |
|
| 7 |
import ( |
| 8 |
"go/ast" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"go/types" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 15 |
) |
| 16 |
|
| 17 |
func TestFree(t *testing.T) { |
| 18 |
const source = ` |
| 19 |
package P |
| 20 |
type A int |
| 21 |
func (A) f() |
| 22 |
func (*A) g() |
| 23 |
|
| 24 |
type fer interface { f() } |
| 25 |
|
| 26 |
func Apply[T fer](x T) T { |
| 27 |
x.f() |
| 28 |
return x |
| 29 |
} |
| 30 |
|
| 31 |
type V[T any] []T |
| 32 |
func (v *V[T]) Push(x T) { *v = append(*v, x) } |
| 33 |
` |
| 34 |
|
| 35 |
fset := token.NewFileSet() |
| 36 |
f, err := parser.ParseFile(fset, "hello.go", source, 0) |
| 37 |
if err != nil { |
| 38 |
t.Fatal(err) |
| 39 |
} |
| 40 |
|
| 41 |
var conf types.Config |
| 42 |
pkg, err := conf.Check("P", fset, []*ast.File{f}, nil) |
| 43 |
if err != nil { |
| 44 |
t.Fatal(err) |
| 45 |
} |
| 46 |
|
| 47 |
for _, test := range []struct { |
| 48 |
expr string // type expression |
| 49 |
want bool // expected value |
| 50 |
}{ |
| 51 |
{"A", false}, |
| 52 |
{"*A", false}, |
| 53 |
{"error", false}, |
| 54 |
{"*error", false}, |
| 55 |
{"struct{A}", false}, |
| 56 |
{"*struct{A}", false}, |
| 57 |
{"fer", false}, |
| 58 |
{"Apply", true}, |
| 59 |
{"Apply[A]", false}, |
| 60 |
{"V", true}, |
| 61 |
{"V[A]", false}, |
| 62 |
{"*V[A]", false}, |
| 63 |
{"(*V[A]).Push", false}, |
| 64 |
} { |
| 65 |
tv, err := types.Eval(fset, pkg, 0, test.expr) |
| 66 |
if err != nil { |
| 67 |
t.Errorf("Eval(%s) failed: %v", test.expr, err) |
| 68 |
} |
| 69 |
|
| 70 |
if got := new(Free).Has(tv.Type); got != test.want { |
| 71 |
t.Logf("Eval(%s) returned the type %s", test.expr, tv.Type) |
| 72 |
t.Errorf("isParameterized(%s) = %v, want %v", test.expr, got, test.want) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestFree124(t *testing.T) { |
| 78 |
testenv.NeedsGo1Point(t, 24) |
| 79 |
const source = ` |
| 80 |
package P |
| 81 |
|
| 82 |
func Within[T any]() { |
| 83 |
type p[V []T] = int |
| 84 |
|
| 85 |
type q[V any] = T |
| 86 |
|
| 87 |
var end int // end provides a position to test at. |
| 88 |
_ = end |
| 89 |
} |
| 90 |
` |
| 91 |
|
| 92 |
fset := token.NewFileSet() |
| 93 |
f, err := parser.ParseFile(fset, "hello.go", source, 0) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
|
| 98 |
var conf types.Config |
| 99 |
pkg, err := conf.Check("P", fset, []*ast.File{f}, nil) |
| 100 |
if err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
|
| 104 |
for _, test := range []struct { |
| 105 |
expr string // type expression |
| 106 |
want bool // expected value |
| 107 |
}{ |
| 108 |
{"p", true}, // not an instantiation |
| 109 |
{"p[[]T]", false}, // is an instantiation |
| 110 |
{"q[int]", true}, |
| 111 |
} { |
| 112 |
pos := pkg.Scope().Lookup("Within").(*types.Func).Scope().Lookup("end").Pos() |
| 113 |
tv, err := types.Eval(fset, pkg, pos, test.expr) |
| 114 |
if err != nil { |
| 115 |
t.Errorf("Eval(%s) failed: %v", test.expr, err) |
| 116 |
} |
| 117 |
|
| 118 |
if got := new(Free).Has(tv.Type); got != test.want { |
| 119 |
t.Logf("Eval(%s) returned the type %s", test.expr, tv.Type) |
| 120 |
t.Errorf("isParameterized(%s) = %v, want %v", test.expr, got, test.want) |
| 121 |
} |
| 122 |
} |
| 123 |
} |