@@ -15,6 +15,10 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
1515 func A[T any](){
1616 type B struct{X T}
1717 type C[U any] struct{X T; Y U}
18+ }
19+
20+ func D(){
21+ type E[V any] struct{X V}
1822 }`
1923
2024 fSet := token .NewFileSet ()
@@ -37,70 +41,18 @@ func TestNestedSubstInGenericFunction(t *testing.T) {
3741 want string // expected underlying value after substitution
3842 }{
3943 {
40- fnName : "A" , fnArgs : []string {" int" },
41- stName : "B" , stArgs : []string {},
42- want : " struct{X int}" ,
44+ fnName : `A` , fnArgs : []string {` int` },
45+ stName : `B` , stArgs : []string {},
46+ want : ` struct{X int}` ,
4347 },
44- } {
45- ctxt := types .NewContext ()
46-
47- fnGen , _ := pkg .Scope ().Lookup (test .fnName ).(* types.Func )
48- if fnGen == nil {
49- t .Fatal ("Failed to find the function " + test .fnName )
50- }
51- fnType := fnGen .Type ().(* types.Signature )
52- fnArgs := evalTypeList (t , fSet , pkg , test .fnArgs )
53- fnInst , err := types .Instantiate (ctxt , fnType , fnArgs , true )
54- if err != nil {
55- t .Fatalf ("Failed to instantiate %s: %v" , fnType , err )
56- }
57- fnFunc := types .NewFunc (fnGen .Pos (), pkg , fnGen .Name (), fnInst .(* types.Signature ))
58-
59- stType , _ := fnFunc .Scope ().Lookup (test .stName ).Type ().(* types.Named )
60- if stType == nil {
61- t .Fatal ("Failed to find the object " + test .fnName + " in function " + test .fnName )
62- }
63- stArgs := evalTypeList (t , fSet , pkg , test .stArgs )
64-
65- stSubst := New (types .NewContext (), fnFunc , stType .TypeParams (), stArgs )
66- stInst := stSubst .Type (stType .Underlying ())
67-
68- if got := stInst .String (); got != test .want {
69- t .Errorf ("subst{%v->%v}.typ(%s) = %v, want %v" , test .stName , test .stArgs , stType .Underlying (), got , test .want )
70- }
71- }
72- }
73-
74- func TestNestedSubstInConcreteFunction (t * testing.T ) {
75- const source = `
76- package P
77-
78- func A(){
79- type C[U any] struct{X T; Y U}
80- }`
81-
82- fSet := token .NewFileSet ()
83- f , err := parser .ParseFile (fSet , "hello.go" , source , 0 )
84- if err != nil {
85- t .Fatal (err )
86- }
87-
88- var conf types.Config
89- pkg , err := conf .Check ("P" , fSet , []* ast.File {f }, nil )
90- if err != nil {
91- t .Fatal (err )
92- }
93-
94- for _ , test := range []struct {
95- fnName string // the name of the nesting function
96- fnArgs []string // type expressions of args for the nesting function
97- stName string // the name of the named type
98- stArgs []string // type expressions of args for the named type
99- want string // expected underlying value after substitution
100- }{
10148 {
102- fnName : "A" , fnArgs : []string {"int" },
103- stName : "B" , stArgs : []string {},
49+ fnName : `A` , fnArgs : []string {`int` },
50+ stName : `C` , stArgs : []string {`bool` },
51+ want : "struct{X int; Y bool}" ,
52+ },
53+ {
54+ fnName : `D` , fnArgs : []string {},
55+ stName : `E` , stArgs : []string {`int` },
10456 want : "struct{X int}" ,
10557 },
10658 } {
@@ -110,21 +62,16 @@ func TestNestedSubstInConcreteFunction(t *testing.T) {
11062 if fnGen == nil {
11163 t .Fatal ("Failed to find the function " + test .fnName )
11264 }
113- fnType := fnGen .Type ().(* types.Signature )
11465 fnArgs := evalTypeList (t , fSet , pkg , test .fnArgs )
115- fnInst , err := types .Instantiate (ctxt , fnType , fnArgs , true )
116- if err != nil {
117- t .Fatalf ("Failed to instantiate %s: %v" , fnType , err )
118- }
119- fnFunc := types .NewFunc (fnGen .Pos (), pkg , fnGen .Name (), fnInst .(* types.Signature ))
66+ fnFunc := types .NewFunc (fnGen .Pos (), pkg , fnGen .Name (), fnGen .Type ().(* types.Signature ))
12067
12168 stType , _ := fnFunc .Scope ().Lookup (test .stName ).Type ().(* types.Named )
12269 if stType == nil {
12370 t .Fatal ("Failed to find the object " + test .fnName + " in function " + test .fnName )
12471 }
12572 stArgs := evalTypeList (t , fSet , pkg , test .stArgs )
12673
127- stSubst := New ( types . NewContext () , fnFunc , stType .TypeParams (), stArgs )
74+ stSubst := NewNested ( ctxt , fnFunc , fnArgs , stType .TypeParams (), stArgs )
12875 stInst := stSubst .Type (stType .Underlying ())
12976
13077 if got := stInst .String (); got != test .want {
0 commit comments