1
0
mirror of https://github.com/golang/go synced 2024-09-30 03:24:39 -06:00

go/types, types2: better internal comment, added suitable test case

Change-Id: If55cd001ab3d274cd9c61c06f73bb98162aa12a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/471019
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2023-02-23 21:26:46 -08:00 committed by Gopher Robot
parent 35a4d1b3bc
commit 4eb3aea2b5
3 changed files with 46 additions and 4 deletions

View File

@ -73,8 +73,25 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
// Substitute type arguments for their respective type parameters in params,
// if any. Note that nil targs entries are ignored by check.subst.
// TODO(gri) Can we avoid this (we're setting known type arguments below,
// but that doesn't impact the isParameterized check for now).
// We do this for better error messages; it's not needed for correctness.
// For instance, given:
//
// func f[P, Q any](P, Q) {}
//
// func _(s string) {
// f[int](s, s) // ERROR
// }
//
// With substitution, we get the error:
// "cannot use s (variable of type string) as int value in argument to f[int]"
//
// Without substitution we get the (worse) error:
// "type string of s does not match inferred type int for P"
// even though the type int was provided (not inferred) for P.
//
// TODO(gri) We might be able to finesse this in the error message reporting
// (which only happens in case of an error) and then avoid doing
// the substitution (which always happens).
if params.Len() > 0 {
smap := makeSubstMap(tparams, targs)
params = check.subst(nopos, params, smap, nil, check.context()).(*Tuple)

View File

@ -75,8 +75,25 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
// Substitute type arguments for their respective type parameters in params,
// if any. Note that nil targs entries are ignored by check.subst.
// TODO(gri) Can we avoid this (we're setting known type arguments below,
// but that doesn't impact the isParameterized check for now).
// We do this for better error messages; it's not needed for correctness.
// For instance, given:
//
// func f[P, Q any](P, Q) {}
//
// func _(s string) {
// f[int](s, s) // ERROR
// }
//
// With substitution, we get the error:
// "cannot use s (variable of type string) as int value in argument to f[int]"
//
// Without substitution we get the (worse) error:
// "type string of s does not match inferred type int for P"
// even though the type int was provided (not inferred) for P.
//
// TODO(gri) We might be able to finesse this in the error message reporting
// (which only happens in case of an error) and then avoid doing
// the substitution (which always happens).
if params.Len() > 0 {
smap := makeSubstMap(tparams, targs)
params = check.subst(nopos, params, smap, nil, check.context()).(*Tuple)

View File

@ -102,3 +102,11 @@ func (p *Settable) Set(s string) {
}
var _ = FromStrings[Settable]([]string{"1", "2"})
// Suitable error message when the type parameter is provided (rather than inferred).
func f8[P, Q any](P, Q) {}
func _(s string) {
f8[int](s /* ERROR "cannot use s (variable of type string) as int value in argument to f8[int]" */ , s)
}