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>
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) {
_ = 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
_ = 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
--- /dev/null
+// 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"
+)