]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: avoid type inference error if arguments are invalid
authorRobert Griesemer <gri@golang.org>
Fri, 17 Nov 2023 00:04:17 +0000 (16:04 -0800)
committerGopher Robot <gobot@golang.org>
Fri, 17 Nov 2023 15:47:08 +0000 (15:47 +0000)
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>

src/cmd/compile/internal/types2/infer.go
src/go/types/infer.go
src/internal/types/testdata/fixedbugs/issue60434.go [new file with mode: 0644]

index c186d70d95e1e48b6759dfe5cdb9f9f95905c821..3a7c74dc82c30d0533504e8ee2b127430e407d5b 100644 (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 {
index b804b0eb080283fd6b472a56192c14fd52e688aa..244f311281a46d2be54de0280fb4ef2673415a94 100644 (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 {
diff --git a/src/internal/types/testdata/fixedbugs/issue60434.go b/src/internal/types/testdata/fixedbugs/issue60434.go
new file mode 100644 (file)
index 0000000..e1d7652
--- /dev/null
@@ -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)
+}