]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: better error message for invalid untyped nil conversion
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 5 Oct 2021 15:59:49 +0000 (22:59 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 7 Oct 2021 14:37:36 +0000 (14:37 +0000)
In case of an invalid untyped nil conversion, the compiler's original
type checker leaves it to the caller to report a suitable error message.
But types2 does not, it always reports the invalid conversion.

CL 328053 made types2 report a better error message, and match the
original compiler behavior. But it ignored the case of untyped nil.

This CL adds that missing case, by checking whether the two operands can
be mixed when untyped nil is present.

Fixes #48784

Change-Id: Idc7d86eb0245aa18ca428e278f4416d6b3679058
Reviewed-on: https://go-review.googlesource.com/c/go/+/354049
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/check/expr2.src
test/fixedbugs/issue48784.go [new file with mode: 0644]

index 90c80f9de0b8726cfa138618379aaa2022ad8d72..3a3a1391563bea13544a17f02cb4e6cbf5ffb56e 100644 (file)
@@ -996,6 +996,12 @@ func (check *Checker) binary(x *operand, e syntax.Expr, lhs, rhs syntax.Expr, op
                if isString(x.typ) != isString(y.typ) {
                        return false
                }
+               if x.isNil() && !hasNil(y.typ) {
+                       return false
+               }
+               if y.isNil() && !hasNil(x.typ) {
+                       return false
+               }
                return true
        }
        if canMix(x, &y) {
index f9726b5de53202c301757e36ea5bde812cd3db24..8e5862319e650b9dc3afbae66435516471bfaef1 100644 (file)
@@ -29,7 +29,7 @@ func arrays() {
        _ = a == b
        _ = a != b
        _ = a /* ERROR < not defined */ < b
-       _ = a == nil /* ERROR cannot convert */
+       _ = a == nil /* ERROR invalid operation.*mismatched types */
 
        type C [10]int
        var c C
@@ -53,7 +53,7 @@ func structs() {
        _ = s == t
        _ = s != t
        _ = s /* ERROR < not defined */ < t
-       _ = s == nil /* ERROR cannot convert */
+       _ = s == nil /* ERROR invalid operation.*mismatched types */
 
        type S struct {
                x int
diff --git a/test/fixedbugs/issue48784.go b/test/fixedbugs/issue48784.go
new file mode 100644 (file)
index 0000000..6048518
--- /dev/null
@@ -0,0 +1,23 @@
+// errorcheck -e
+
+// 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
+
+type T struct{}
+
+var s string
+var b bool
+var i int
+var t T
+var a [1]int
+
+var (
+       _ = s == nil // ERROR "invalid operation:.*mismatched types string and untyped nil"
+       _ = b == nil // ERROR "invalid operation:.*mismatched types bool and untyped nil"
+       _ = i == nil // ERROR "invalid operation:.*mismatched types int and untyped nil"
+       _ = t == nil // ERROR "invalid operation:.*mismatched types T and untyped nil"
+       _ = a == nil // ERROR "invalid operation:.*mismatched types \[1\]int and untyped nil"
+)