Skip to content

Commit ece493e

Browse files
Jason7602findleyr
authored andcommitted
cmd/compile: fix type error reported on the wrong line
The 'Does not match' type error shoud be reported where the function is called, not where the function is declared. And fix the todo by gri of issue45985 Fixes #45985 Fixes #49800 Change-Id: I15aac44dd44f2a57c485a1c273fcd79db912c389 Reviewed-on: https://go-review.googlesource.com/c/go/+/362634 Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 78b4518 commit ece493e

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

src/cmd/compile/internal/types2/infer.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
6060
// If we have type arguments, see how far we get with constraint type inference.
6161
if len(targs) > 0 && useConstraintTypeInference {
6262
var index int
63-
targs, index = check.inferB(tparams, targs)
63+
targs, index = check.inferB(pos, tparams, targs)
6464
if targs == nil || index < 0 {
6565
return targs
6666
}
@@ -171,7 +171,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
171171
// Note that even if we don't have any type arguments, constraint type inference
172172
// may produce results for constraints that explicitly specify a type.
173173
if useConstraintTypeInference {
174-
targs, index = check.inferB(tparams, targs)
174+
targs, index = check.inferB(pos, tparams, targs)
175175
if targs == nil || index < 0 {
176176
return targs
177177
}
@@ -209,7 +209,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
209209

210210
// Again, follow up with constraint type inference.
211211
if useConstraintTypeInference {
212-
targs, index = check.inferB(tparams, targs)
212+
targs, index = check.inferB(pos, tparams, targs)
213213
if targs == nil || index < 0 {
214214
return targs
215215
}
@@ -360,7 +360,7 @@ func (w *tpWalker) isParameterizedTypeList(list []Type) bool {
360360
// first type argument in that list that couldn't be inferred (and thus is nil). If all
361361
// type arguments were inferred successfully, index is < 0. The number of type arguments
362362
// provided may be less than the number of type parameters, but there must be at least one.
363-
func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type, index int) {
363+
func (check *Checker) inferB(pos syntax.Pos, tparams []*TypeParam, targs []Type) (types []Type, index int) {
364364
assert(len(tparams) >= len(targs) && len(targs) > 0)
365365

366366
// Setup bidirectional unification between constraints
@@ -388,7 +388,7 @@ func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type,
388388
if !u.unify(tpar, sbound) {
389389
// TODO(gri) improve error message by providing the type arguments
390390
// which we know already
391-
check.errorf(tpar.obj, "%s does not match %s", tpar, sbound)
391+
check.errorf(pos, "%s does not match %s", tpar, sbound)
392392
return nil, 0
393393
}
394394
}

src/cmd/compile/internal/types2/testdata/fixedbugs/issue45985.go2

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
package issue45985
66

7-
// TODO(gri): this error should be on app[int] below.
8-
func app[S /* ERROR "S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
7+
func app[S interface{ ~[]T }, T any](s S, e T) S {
98
return append(s, e)
109
}
1110

1211
func _() {
13-
_ = app[int]
12+
_ = app[/* ERROR "S does not match" */int]
1413
}

src/go/types/infer.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
5959
// If we have type arguments, see how far we get with constraint type inference.
6060
if len(targs) > 0 {
6161
var index int
62-
targs, index = check.inferB(tparams, targs)
62+
targs, index = check.inferB(posn, tparams, targs)
6363
if targs == nil || index < 0 {
6464
return targs
6565
}
@@ -174,7 +174,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
174174
// See how far we get with constraint type inference.
175175
// Note that even if we don't have any type arguments, constraint type inference
176176
// may produce results for constraints that explicitly specify a type.
177-
targs, index = check.inferB(tparams, targs)
177+
targs, index = check.inferB(posn, tparams, targs)
178178
if targs == nil || index < 0 {
179179
return targs
180180
}
@@ -210,7 +210,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
210210
}
211211

212212
// Again, follow up with constraint type inference.
213-
targs, index = check.inferB(tparams, targs)
213+
targs, index = check.inferB(posn, tparams, targs)
214214
if targs == nil || index < 0 {
215215
return targs
216216
}
@@ -359,7 +359,7 @@ func (w *tpWalker) isParameterizedTypeList(list []Type) bool {
359359
// first type argument in that list that couldn't be inferred (and thus is nil). If all
360360
// type arguments were inferred successfully, index is < 0. The number of type arguments
361361
// provided may be less than the number of type parameters, but there must be at least one.
362-
func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type, index int) {
362+
func (check *Checker) inferB(posn positioner, tparams []*TypeParam, targs []Type) (types []Type, index int) {
363363
assert(len(tparams) >= len(targs) && len(targs) > 0)
364364

365365
// Setup bidirectional unification between constraints
@@ -387,7 +387,7 @@ func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type,
387387
if !u.unify(tpar, sbound) {
388388
// TODO(gri) improve error message by providing the type arguments
389389
// which we know already
390-
check.errorf(tpar.obj, _InvalidTypeArg, "%s does not match %s", tpar, sbound)
390+
check.errorf(posn, _InvalidTypeArg, "%s does not match %s", tpar, sbound)
391391
return nil, 0
392392
}
393393
}

src/go/types/testdata/fixedbugs/issue45985.go2

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
package issue45985
66

7-
// TODO(rFindley): this error should be on app[int] below.
8-
func app[S /* ERROR "S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
7+
func app[S interface{ ~[]T }, T any](s S, e T) S {
98
return append(s, e)
109
}
1110

1211
func _() {
13-
_ = app[int]
12+
_ = app/* ERROR "S does not match" */[int]
1413
}

0 commit comments

Comments
 (0)