]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: report error for invalid (but empty...
authorRobert Griesemer <gri@golang.org>
Tue, 15 Dec 2020 00:58:46 +0000 (16:58 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 15 Dec 2020 19:43:32 +0000 (19:43 +0000)
Enable one more errorcheck test.

Updates #43110.
Updates #43200.

Change-Id: Ib7b971d5e9989c65320579f75d65266bbbbeec53
Reviewed-on: https://go-review.googlesource.com/c/go/+/278132
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/fixedbugs/issue43110.src [new file with mode: 0644]
src/cmd/compile/internal/types2/stmt.go
test/run.go
test/switch3.go

diff --git a/src/cmd/compile/internal/types2/fixedbugs/issue43110.src b/src/cmd/compile/internal/types2/fixedbugs/issue43110.src
new file mode 100644 (file)
index 0000000..4a46945
--- /dev/null
@@ -0,0 +1,43 @@
+// Copyright 2020 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 P *struct{}
+
+func _() {
+       // want an error even if the switch is empty
+       var a struct{ _ func() }
+       switch a /* ERROR cannot switch on a */ {
+       }
+
+       switch a /* ERROR cannot switch on a */ {
+       case a: // no follow-on error here
+       }
+
+       // this is ok because f can be compared to nil
+       var f func()
+       switch f {
+       }
+
+       switch f {
+       case nil:
+       }
+
+       switch (func())(nil) {
+       case nil:
+       }
+
+       switch (func())(nil) {
+       case f /* ERROR cannot compare */ :
+       }
+
+       switch nil /* ERROR use of untyped nil in switch expression */ {
+       }
+
+       // this is ok
+       switch P(nil) {
+       case P(nil):
+       }
+}
index 477bc58bd034f57d0338594bbab45e26f7555ed9..3463cfdf57a0534e085060f0f9a41e4bcfe33018 100644 (file)
@@ -605,6 +605,10 @@ func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
                // By checking assignment of x to an invisible temporary
                // (as a compiler would), we get all the relevant checks.
                check.assignment(&x, nil, "switch expression")
+               if x.mode != invalid && !Comparable(x.typ) && !hasNil(x.typ) {
+                       check.errorf(&x, "cannot switch on %s (%s is not comparable)", &x, x.typ)
+                       x.mode = invalid
+               }
        } else {
                // spec: "A missing switch expression is
                // equivalent to the boolean value true."
index 9cfd13ae481d928c42424289f5741a94640f8e40..01e67e8db88eeea6dd243616061c5df8cfd9c276 100644 (file)
@@ -1937,7 +1937,6 @@ var excluded = map[string]bool{
        "initializerr.go": true, // types2 reports extra errors
        "linkname2.go":    true, // error reported by noder (not running for types2 errorcheck test)
        "shift1.go":       true, // issue #42989
-       "switch3.go":      true, // issue #43110
        "switch4.go":      true, // error reported by noder (not running for types2 errorcheck test)
        "typecheck.go":    true, // invalid function is not causing errors when called
 
index 28705e464ef2c20949e7298fe1072a24fe2ab2df..403563223c7d11d7bbd82ace8e84e5d76538465f 100644 (file)
@@ -28,21 +28,21 @@ func bad() {
        var m, m1 map[int]int
        switch m {
        case nil:
-       case m1: // ERROR "can only compare map m to nil|map can only be compared to nil"
+       case m1: // ERROR "can only compare map m to nil|map can only be compared to nil|cannot compare"
        default:
        }
 
        var a, a1 []int
        switch a {
        case nil:
-       case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil"
+       case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil|cannot compare"
        default:
        }
 
        var f, f1 func()
        switch f {
        case nil:
-       case f1: // ERROR "can only compare func f to nil|func can only be compared to nil"
+       case f1: // ERROR "can only compare func f to nil|func can only be compared to nil|cannot compare"
        default:
        }