| 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 ssa_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "go/types" |
| 10 | "reflect" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "golang.org/x/tools/go/ssa" |
| 16 | "golang.org/x/tools/go/ssa/ssautil" |
| 17 | ) |
| 18 | |
| 19 | // TestNeedsInstance ensures that new method instances can be created via MethodValue. |
| 20 | func TestNeedsInstance(t *testing.T) { |
| 21 | const input = ` |
| 22 | package p |
| 23 | |
| 24 | import "unsafe" |
| 25 | |
| 26 | type Pointer[T any] struct { |
| 27 | v unsafe.Pointer |
| 28 | } |
| 29 | |
| 30 | func (x *Pointer[T]) Load() *T { |
| 31 | return (*T)(LoadPointer(&x.v)) |
| 32 | } |
| 33 | |
| 34 | func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer) |
| 35 | ` |
| 36 | // The SSA members for this package should look something like this: |
| 37 | // func LoadPointer func(addr *unsafe.Pointer) (val unsafe.Pointer) |
| 38 | // type Pointer struct{v unsafe.Pointer} |
| 39 | // method (*Pointer[T any]) Load() *T |
| 40 | // func init func() |
| 41 | // var init$guard bool |
| 42 | |
| 43 | for _, mode := range []ssa.BuilderMode{ |
| 44 | ssa.SanityCheckFunctions, |
| 45 | ssa.SanityCheckFunctions | ssa.InstantiateGenerics, |
| 46 | } { |
| 47 | p, _ := buildPackage(t, input, mode) |
| 48 | prog := p.Prog |
| 49 | |
| 50 | ptr := p.Type("Pointer").Type().(*types.Named) |
| 51 | if ptr.NumMethods() != 1 { |
| 52 | t.Fatalf("Expected Pointer to have 1 method. got %d", ptr.NumMethods()) |
| 53 | } |
| 54 | |
| 55 | obj := ptr.Method(0) |
| 56 | if obj.Name() != "Load" { |
| 57 | t.Errorf("Expected Pointer to have method named 'Load'. got %q", obj.Name()) |
| 58 | } |
| 59 | |
| 60 | meth := prog.FuncValue(obj) |
| 61 | |
| 62 | // instantiateLoadMethod returns the first method (Load) of the instantiation *Pointer[T]. |
| 63 | instantiateLoadMethod := func(T types.Type) *ssa.Function { |
| 64 | ptrT, err := types.Instantiate(nil, ptr, []types.Type{T}, false) |
| 65 | if err != nil { |
| 66 | t.Fatalf("Failed to Instantiate %q by %q", ptr, T) |
| 67 | } |
| 68 | methods := types.NewMethodSet(types.NewPointer(ptrT)) |
| 69 | if methods.Len() != 1 { |
| 70 | t.Fatalf("Expected 1 method for %q. got %d", ptrT, methods.Len()) |
| 71 | } |
| 72 | return prog.MethodValue(methods.At(0)) |
| 73 | } |
| 74 | |
| 75 | intSliceTyp := types.NewSlice(types.Typ[types.Int]) |
| 76 | instance := instantiateLoadMethod(intSliceTyp) // (*Pointer[[]int]).Load |
| 77 | if instance.Origin() != meth { |
| 78 | t.Errorf("Expected Origin of %s to be %s. got %s", instance, meth, instance.Origin()) |
| 79 | } |
| 80 | if len(instance.TypeArgs()) != 1 || !types.Identical(instance.TypeArgs()[0], intSliceTyp) { |
| 81 | t.Errorf("Expected TypeArgs of %s to be %v. got %v", instance, []types.Type{intSliceTyp}, instance.TypeArgs()) |
| 82 | } |
| 83 | |
| 84 | // A second request with an identical type returns the same Function. |
| 85 | second := instantiateLoadMethod(types.NewSlice(types.Typ[types.Int])) |
| 86 | if second != instance { |
| 87 | t.Error("Expected second identical instantiation to be the same function") |
| 88 | } |
| 89 | |
| 90 | // (*Pointer[[]uint]).Load |
| 91 | inst2 := instantiateLoadMethod(types.NewSlice(types.Typ[types.Uint])) |
| 92 | |
| 93 | if instance.Name() >= inst2.Name() { |
| 94 | t.Errorf("Expected name of instance %s to be before instance %v", instance, inst2) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // TestCallsToInstances checks that calles of calls to generic functions, |
| 100 | // without monomorphization, are wrappers around the origin generic function. |
| 101 | func TestCallsToInstances(t *testing.T) { |
| 102 | const input = ` |
| 103 | package p |
| 104 | |
| 105 | type I interface { |
| 106 | Foo() |
| 107 | } |
| 108 | |
| 109 | type A int |
| 110 | func (a A) Foo() {} |
| 111 | |
| 112 | type J[T any] interface{ Bar() T } |
| 113 | type K[T any] struct{ J[T] } |
| 114 | |
| 115 | func Id[T any] (t T) T { |
| 116 | return t |
| 117 | } |
| 118 | |
| 119 | func Lambda[T I]() func() func(T) { |
| 120 | return func() func(T) { |
| 121 | return T.Foo |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func NoOp[T any]() {} |
| 126 | |
| 127 | func Bar[T interface { Foo(); ~int | ~string }, U any] (t T, u U) { |
| 128 | Id[U](u) |
| 129 | Id[T](t) |
| 130 | } |
| 131 | |
| 132 | func Make[T any]() interface{} { |
| 133 | NoOp[K[T]]() |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | func entry(i int, a A) int { |
| 138 | Lambda[A]()()(a) |
| 139 | |
| 140 | x := Make[int]() |
| 141 | if j, ok := x.(interface{ Bar() int }); ok { |
| 142 | print(j) |
| 143 | } |
| 144 | |
| 145 | Bar[A, int](a, i) |
| 146 | |
| 147 | return Id[int](i) |
| 148 | } |
| 149 | ` |
| 150 | p, _ := buildPackage(t, input, ssa.SanityCheckFunctions) |
| 151 | all := ssautil.AllFunctions(p.Prog) |
| 152 | |
| 153 | for _, ti := range []struct { |
| 154 | orig string |
| 155 | instance string |
| 156 | tparams string |
| 157 | targs string |
| 158 | chTypeInstrs int // number of ChangeType instructions in f's body |
| 159 | }{ |
| 160 | {"Id", "Id[int]", "[T]", "[int]", 2}, |
| 161 | {"Lambda", "Lambda[p.A]", "[T]", "[p.A]", 1}, |
| 162 | {"Make", "Make[int]", "[T]", "[int]", 0}, |
| 163 | {"NoOp", "NoOp[p.K[T]]", "[T]", "[p.K[T]]", 0}, |
| 164 | } { |
| 165 | test := ti |
| 166 | t.Run(test.instance, func(t *testing.T) { |
| 167 | f := p.Members[test.orig].(*ssa.Function) |
| 168 | if f == nil { |
| 169 | t.Fatalf("origin function not found") |
| 170 | } |
| 171 | |
| 172 | var i *ssa.Function |
| 173 | for _, fn := range instancesOf(all, f) { |
| 174 | if fn.Name() == test.instance { |
| 175 | i = fn |
| 176 | break |
| 177 | } |
| 178 | } |
| 179 | if i == nil { |
| 180 | t.Fatalf("instance not found") |
| 181 | } |
| 182 | |
| 183 | // for logging on failures |
| 184 | var body strings.Builder |
| 185 | i.WriteTo(&body) |
| 186 | t.Log(body.String()) |
| 187 | |
| 188 | if len(i.Blocks) != 1 { |
| 189 | t.Fatalf("body has more than 1 block") |
| 190 | } |
| 191 | |
| 192 | if instrs := changeTypeInstrs(i.Blocks[0]); instrs != test.chTypeInstrs { |
| 193 | t.Errorf("want %v instructions; got %v", test.chTypeInstrs, instrs) |
| 194 | } |
| 195 | |
| 196 | if test.tparams != tparams(i) { |
| 197 | t.Errorf("want %v type params; got %v", test.tparams, tparams(i)) |
| 198 | } |
| 199 | |
| 200 | if test.targs != targs(i) { |
| 201 | t.Errorf("want %v type arguments; got %v", test.targs, targs(i)) |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func tparams(f *ssa.Function) string { |
| 208 | tplist := f.TypeParams() |
| 209 | var tps []string |
| 210 | for tparam := range tplist.TypeParams() { |
| 211 | tps = append(tps, tparam.String()) |
| 212 | } |
| 213 | return fmt.Sprint(tps) |
| 214 | } |
| 215 | |
| 216 | func targs(f *ssa.Function) string { |
| 217 | var tas []string |
| 218 | for _, ta := range f.TypeArgs() { |
| 219 | tas = append(tas, ta.String()) |
| 220 | } |
| 221 | return fmt.Sprint(tas) |
| 222 | } |
| 223 | |
| 224 | func changeTypeInstrs(b *ssa.BasicBlock) int { |
| 225 | cnt := 0 |
| 226 | for _, i := range b.Instrs { |
| 227 | if _, ok := i.(*ssa.ChangeType); ok { |
| 228 | cnt++ |
| 229 | } |
| 230 | } |
| 231 | return cnt |
| 232 | } |
| 233 | |
| 234 | func TestInstanceUniqueness(t *testing.T) { |
| 235 | const input = ` |
| 236 | package p |
| 237 | |
| 238 | func H[T any](t T) { |
| 239 | print(t) |
| 240 | } |
| 241 | |
| 242 | func F[T any](t T) { |
| 243 | H[T](t) |
| 244 | H[T](t) |
| 245 | H[T](t) |
| 246 | } |
| 247 | |
| 248 | func G[T any](t T) { |
| 249 | H[T](t) |
| 250 | H[T](t) |
| 251 | } |
| 252 | |
| 253 | func Foo[T any, S any](t T, s S) { |
| 254 | Foo[S, T](s, t) |
| 255 | Foo[T, S](t, s) |
| 256 | } |
| 257 | ` |
| 258 | p, _ := buildPackage(t, input, ssa.SanityCheckFunctions) |
| 259 | |
| 260 | all := ssautil.AllFunctions(p.Prog) |
| 261 | for _, test := range []struct { |
| 262 | orig string |
| 263 | instances string |
| 264 | }{ |
| 265 | {"H", "[p.H[T] p.H[T]]"}, |
| 266 | {"Foo", "[p.Foo[S T] p.Foo[T S]]"}, |
| 267 | } { |
| 268 | t.Run(test.orig, func(t *testing.T) { |
| 269 | f := p.Members[test.orig].(*ssa.Function) |
| 270 | if f == nil { |
| 271 | t.Fatalf("origin function not found") |
| 272 | } |
| 273 | |
| 274 | instances := instancesOf(all, f) |
| 275 | sort.Slice(instances, func(i, j int) bool { return instances[i].Name() < instances[j].Name() }) |
| 276 | |
| 277 | if got := fmt.Sprintf("%v", instances); !reflect.DeepEqual(got, test.instances) { |
| 278 | t.Errorf("got %v instances, want %v", got, test.instances) |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // instancesOf returns a new unordered slice of all instances of the |
| 285 | // specified function g in fns. |
| 286 | func instancesOf(fns map[*ssa.Function]bool, g *ssa.Function) []*ssa.Function { |
| 287 | var instances []*ssa.Function |
| 288 | for fn := range fns { |
| 289 | if fn != g && fn.Origin() == g { |
| 290 | instances = append(instances, fn) |
| 291 | } |
| 292 | } |
| 293 | return instances |
| 294 | } |