Debian Codegraph @

go/ssa/subst_test.go

raw symbols SCIP
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
6
7 import (
8 "go/ast"
9 "go/parser"
10 "go/token"
11 "go/types"
12 "testing"
13 )
14
15 func TestSubst(t *testing.T) {
16 const source = `
17 package P
18
19 func within(){
20 // Pretend that the instantiation happens within this function.
21 }
22
23 type t0 int
24 func (t0) f()
25 type t1 interface{ f() }
26 type t2 interface{ g() }
27 type t3 interface{ ~int }
28
29 func Fn0[T t1](x T) T {
30 x.f()
31 return x
32 }
33
34 type A[T any] [4]T
35 type B[T any] []T
36 type C[T, S any] []struct{s S; t T}
37 type D[T, S any] *struct{s S; t *T}
38 type E[T, S any] interface{ F() (T, S) }
39 type F[K comparable, V any] map[K]V
40 type G[T any] chan *T
41 type H[T any] func() T
42 type I[T any] struct{x, y, z int; t T}
43 type J[T any] interface{ t1 }
44 type K[T any] interface{ t1; F() T }
45 type L[T any] interface{ F() T; J[T] }
46
47 var _ L[int] = Fn0[L[int]](nil)
48 `
49
50 fset := token.NewFileSet()
51 f, err := parser.ParseFile(fset, "hello.go", source, 0)
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 var conf types.Config
57 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
58 if err != nil {
59 t.Fatal(err)
60 }
61
62 within, _ := pkg.Scope().Lookup("within").(*types.Func)
63 if within == nil {
64 t.Fatal("Failed to find the function within()")
65 }
66
67 for _, test := range []struct {
68 expr string // type expression of Named parameterized type
69 args []string // type expressions of args for named
70 want string // expected underlying value after substitution
71 }{
72 {"A", []string{"string"}, "[4]string"},
73 {"A", []string{"int"}, "[4]int"},
74 {"B", []string{"int"}, "[]int"},
75 {"B", []string{"int8"}, "[]int8"},
76 {"C", []string{"int8", "string"}, "[]struct{s string; t int8}"},
77 {"C", []string{"string", "int8"}, "[]struct{s int8; t string}"},
78 {"D", []string{"int16", "string"}, "*struct{s string; t *int16}"},
79 {"E", []string{"int32", "string"}, "interface{F() (int32, string)}"},
80 {"F", []string{"int64", "string"}, "map[int64]string"},
81 {"G", []string{"uint64"}, "chan *uint64"},
82 {"H", []string{"uintptr"}, "func() uintptr"},
83 {"I", []string{"t0"}, "struct{x int; y int; z int; t P.t0}"},
84 {"J", []string{"t0"}, "interface{P.t1}"},
85 {"K", []string{"t0"}, "interface{F() P.t0; P.t1}"},
86 {"L", []string{"t0"}, "interface{F() P.t0; P.J[P.t0]}"},
87 {"L", []string{"L[t0]"}, "interface{F() P.L[P.t0]; P.J[P.L[P.t0]]}"},
88 } {
89 // Eval() expr for its type.
90 tv, err := types.Eval(fset, pkg, 0, test.expr)
91 if err != nil {
92 t.Fatalf("Eval(%s) failed: %v", test.expr, err)
93 }
94 // Eval() test.args[i] to get the i'th type arg.
95 var targs []types.Type
96 for _, astr := range test.args {
97 tv, err := types.Eval(fset, pkg, 0, astr)
98 if err != nil {
99 t.Fatalf("Eval(%s) failed: %v", astr, err)
100 }
101 targs = append(targs, tv.Type)
102 }
103
104 T := tv.Type.(*types.Named)
105
106 subst := makeSubster(types.NewContext(), within, T.TypeParams(), targs)
107 sub := subst.typ(T.Underlying())
108 if got := sub.String(); got != test.want {
109 t.Errorf("subst{%v->%v}.typ(%s) = %v, want %v", test.expr, test.args, T.Underlying(), got, test.want)
110 }
111 }
112 }