1
0
mirror of https://github.com/golang/go synced 2024-09-28 18:14:29 -06:00

go/types, types2: avoid type inference error if arguments are invalid

Fixes #60434.

Change-Id: I6eca4c508fa96fe81c4ee8a12b76c3de405fee7c
Reviewed-on: https://go-review.googlesource.com/c/go/+/543176
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2023-11-16 16:04:17 -08:00 committed by Gopher Robot
parent 8f7006a6df
commit b34059032e
3 changed files with 33 additions and 0 deletions

View File

@ -56,6 +56,14 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
return targs
}
// If we have invalid (ordinary) arguments, an error was reported before.
// Avoid additional inference errors and exit early (go.dev/issue/60434).
for _, arg := range args {
if arg.mode == invalid {
return nil
}
}
// Make sure we have a "full" list of type arguments, some of which may
// be nil (unknown). Make a copy so as to not clobber the incoming slice.
if len(targs) < n {

View File

@ -58,6 +58,14 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
return targs
}
// If we have invalid (ordinary) arguments, an error was reported before.
// Avoid additional inference errors and exit early (go.dev/issue/60434).
for _, arg := range args {
if arg.mode == invalid {
return nil
}
}
// Make sure we have a "full" list of type arguments, some of which may
// be nil (unknown). Make a copy so as to not clobber the incoming slice.
if len(targs) < n {

View File

@ -0,0 +1,17 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test that there are no type inference errors
// if function arguments are invalid.
package p
func f[S any](S) {}
var s struct{ x int }
func _() {
f(s.y /* ERROR "s.y undefined" */)
f(1 /* ERROR "cannot convert 1" */ / s)
}