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 ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/
6
7 import (
8 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
9 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
10 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
11 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
12 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
13 )
14
15 func TestSubstscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/TestSubst().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().()
51 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "hello.go", source, 0)
52 if err != nil {
53 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
54 }
55
56 var conf typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#
57 pkg, err := conf.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().("P", fset, []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, nil)
58 if err != nil {
59 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
60 }
61
62 within, _ := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("within").(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Funcscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Func#)
63 if within == nil {
64 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Evalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Eval().(fset, pkg, 0, test.expr)
91 if err != nil {
92 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Eval(%s) failed: %v", test.expr, err)
93 }
94 // Eval() test.args[i] to get the i'th type arg.
95 var targs []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#
96 for _, astr := range test.args {
97 tv, err := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Evalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Eval().(fset, pkg, 0, astr)
98 if err != nil {
99 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Eval(%s) failed: %v", astr, err)
100 }
101 targs = append(targs, tv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type.)
102 }
103
104 T := tv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type..(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#)
105
106 subst := makeSubsterscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/makeSubster().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewContextscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewContext().(), within, T.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().(), targs)
107 sub := subst.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/subster#typ().(T.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().())
108 if got := sub.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().(); got != test.want {
109 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("subst{%v->%v}.typ(%s) = %v, want %v", test.expr, test.args, T.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().(), got, test.want)
110 }
111 }
112 }