]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: better error message for invalid untyped nil conversion
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 5 Oct 2021 16:53:07 +0000 (23:53 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 7 Oct 2021 14:37:45 +0000 (14:37 +0000)
This is port of CL 354049 for types2 to go/type.

The change is identical, but for some tweaks to the error message/position
in tests, since when go/types reports the exact operation "cannot convert"
instead of the general "invalid operation" like types2.

Updates #48784

Change-Id: I3e99f2721501d23187fd0a8970eb1de28e0c41d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/354050
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/go/types/errorcodes.go
src/go/types/expr.go
src/go/types/testdata/check/expr2.src

index bcc850f75381f32bd6ca3f7c88b708195c5d8e04..49c6a74c20d5487d7e398a4a6b278f0573a2428f 100644 (file)
@@ -875,7 +875,7 @@ const (
        // context in which it is used.
        //
        // Example:
-       //  var _ = 1 + nil
+       //  var _ = 1 + new(int)
        _InvalidUntypedConversion
 
        // _BadOffsetofSyntax occurs when unsafe.Offsetof is called with an argument
index 2fc5aa85d2ec0a40a46b29e0e3153c7f66fe8af7..fac5a5e31d46f79aceb482ea8f5038ea3d60e91c 100644 (file)
@@ -967,6 +967,12 @@ func (check *Checker) binary(x *operand, e ast.Expr, lhs, rhs ast.Expr, op token
                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..8757fd9e487f3c7d1aac003b87cc04beb9dba87f 100644 (file)
@@ -29,7 +29,7 @@ func arrays() {
        _ = a == b
        _ = a != b
        _ = a /* ERROR < not defined */ < b
-       _ = a == nil /* ERROR cannot convert */
+       _ = a /* ERROR cannot compare.*mismatched types */ == nil
 
        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 /* ERROR cannot compare.*mismatched types */ == nil
 
        type S struct {
                x int