@@ -20,13 +20,14 @@ func (check *Checker) infer(pos token.Pos, tparams []*TypeName, params *Tuple, a
20
20
21
21
errorf := func (kind string , tpar , targ Type , arg * operand ) {
22
22
// provide a better error message if we can
23
- if tpar , _ := tpar .(* TypeParam ); tpar != nil {
24
- if inferred := u .x .at (tpar .index ); inferred != nil {
25
- check .errorf (arg .pos (), "%s %s of %s does not match inferred type %s for %s" , kind , targ , arg .expr , inferred , tpar )
26
- return
27
- }
23
+ targs , _ := u .x .types ()
24
+ smap := makeSubstMap (tparams , targs )
25
+ inferred := check .subst (arg .pos (), tpar , smap )
26
+ if inferred != tpar {
27
+ check .errorf (arg .pos (), "%s %s of %s does not match inferred type %s for %s" , kind , targ , arg .expr , inferred , tpar )
28
+ } else {
29
+ check .errorf (arg .pos (), "%s %s of %s does not match %s" , kind , targ , arg .expr , tpar )
28
30
}
29
- check .errorf (arg .pos (), "%s %s of %s does not match %s" , kind , targ , arg .expr , tpar )
30
31
}
31
32
32
33
// Terminology: generic parameter = function parameter with a type-parameterized type
@@ -96,24 +97,22 @@ func (check *Checker) infer(pos token.Pos, tparams []*TypeName, params *Tuple, a
96
97
97
98
// Collect type arguments and check if they all have been determined.
98
99
// TODO(gri) consider moving this outside this function and then we won't need to pass in pos
99
- var targs []Type // lazily allocated
100
- for i , tpar := range tparams {
101
- targ := u .x .at (i )
102
- if targ == nil {
103
- ppos := check .fset .Position (tpar .pos ).String ()
104
- check .errorf (pos , "cannot infer %s (%s)" , tpar .name , ppos )
105
- return nil
106
- }
107
- if targs == nil {
108
- targs = make ([]Type , len (tparams ))
109
- }
110
- targs [i ] = targ
100
+ targs , failed := u .x .types ()
101
+ if failed >= 0 {
102
+ tpar := tparams [failed ]
103
+ ppos := check .fset .Position (tpar .pos ).String ()
104
+ check .errorf (pos , "cannot infer %s (%s)" , tpar .name , ppos )
105
+ return nil
111
106
}
112
107
113
108
return targs
114
109
}
115
110
116
111
// IsParameterized reports whether typ contains any type parameters.
112
+ // TODO(gri) This is not strictly correct. We only want the free
113
+ // type parameters for a given type. (At the moment, the only way
114
+ // to mix free and bound type parameters is through method type parameters
115
+ // on parameterized receiver types - need to investigate.)
117
116
func IsParameterized (typ Type ) bool {
118
117
return isParameterized (typ , make (map [Type ]bool ))
119
118
}
0 commit comments