]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: handle case of no specific target types in conversion
authorRobert Findley <rfindley@google.com>
Mon, 29 Nov 2021 22:39:19 +0000 (17:39 -0500)
committerRobert Findley <rfindley@google.com>
Mon, 29 Nov 2021 22:57:42 +0000 (22:57 +0000)
Avoid a panic by handling the case of no specific target type in a type
parameter to type parameter conversions.

Fixes #49864

Change-Id: I117dd80cc9d47c8c1e168f1caf0f281726270c84
Reviewed-on: https://go-review.googlesource.com/c/go/+/367616
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2 [new file with mode: 0644]
src/go/types/conversions.go
src/go/types/testdata/fixedbugs/issue49864.go2 [new file with mode: 0644]

index 47f9ac0a5a72350613e4f8958594fd8a71923d8f..253868cf931b20adbd7861ad21b094c35a0c245c 100644 (file)
@@ -233,6 +233,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
                        }
                        x.typ = V.typ
                        return Tp.is(func(T *term) bool {
+                               if T == nil {
+                                       return false // no specific types
+                               }
                                if !x.convertibleTo(check, T.typ, cause) {
                                        errorf("cannot convert %s (in %s) to %s (in %s)", V.typ, Vp, T.typ, Tp)
                                        return false
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49864.go2
new file mode 100644 (file)
index 0000000..0437e74
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2021 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.
+
+package p
+
+func _[P ~int, Q any](p P) {
+       _ = Q(p /* ERROR cannot convert */ )
+}
index 5995d5920f4592ed91cf98bb5d7ff648925e2b3d..fb3771635d33c252b2ee9e39a39f723dfbc17860 100644 (file)
@@ -224,6 +224,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
                        }
                        x.typ = V.typ
                        return Tp.is(func(T *term) bool {
+                               if T == nil {
+                                       return false // no specific types
+                               }
                                if !x.convertibleTo(check, T.typ, cause) {
                                        errorf("cannot convert %s (in %s) to %s (in %s)", V.typ, Vp, T.typ, Tp)
                                        return false
diff --git a/src/go/types/testdata/fixedbugs/issue49864.go2 b/src/go/types/testdata/fixedbugs/issue49864.go2
new file mode 100644 (file)
index 0000000..0437e74
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2021 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.
+
+package p
+
+func _[P ~int, Q any](p P) {
+       _ = Q(p /* ERROR cannot convert */ )
+}