]> Cypherpunks repositories - gostls13.git/commitdiff
comd/compile/internal/types2: add missing nil check in const conversion
authorRobert Griesemer <gri@golang.org>
Tue, 2 Nov 2021 17:14:29 +0000 (10:14 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 2 Nov 2021 17:53:48 +0000 (17:53 +0000)
Follow-up on CL 360396.

Fixes #49296.

Change-Id: Ie08f86ae884da4cfd5db557cbf4f721a237dc39f
Reviewed-on: https://go-review.googlesource.com/c/go/+/360796
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue49296.go2 [new file with mode: 0644]

index 0e26a73cf8d67deef730ce222b585e6bf01ee6f6..4d0ed79c38b9c4c6d3521f76d6ec6e2f0357900a 100644 (file)
@@ -18,6 +18,8 @@ func (check *Checker) conversion(x *operand, T Type) {
 
        constConvertibleTo := func(T Type, val *constant.Value) bool {
                switch t := asBasic(T); {
+               case t == nil:
+                       // nothing to do
                case representableConst(x.val, check, t, val):
                        return true
                case isInteger(x.typ) && isString(t):
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49296.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49296.go2
new file mode 100644 (file)
index 0000000..8f52acc
--- /dev/null
@@ -0,0 +1,20 @@
+// 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 _[
+        T0 any,
+        T1 []int,
+        T2 ~float64 | ~complex128 | chan int,
+]() {
+        _ = T0(nil /* ERROR cannot convert untyped nil to T0 */ )
+        _ = T1(1 /* ERROR cannot convert 1 .* to T1 */ )
+        _ = T2(2 /* ERROR cannot convert 2 .* to T2 */ )
+}
+
+// test case from issue
+func f[T interface{[]int}]() {
+       _ = T(1 /* ERROR cannot convert */ )
+}