]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: types in type switch cases must be instantiated
authorRobert Griesemer <gri@golang.org>
Sat, 28 Aug 2021 00:04:42 +0000 (17:04 -0700)
committerRobert Griesemer <gri@golang.org>
Sat, 28 Aug 2021 00:51:39 +0000 (00:51 +0000)
We already have a function that does all the right checks and it's called
varType. The only reason it wasn't used for type switch cases was that we
also have to accept the nil value. That was handled with typeOrNil. But
that function (typeOrNil) was only used for this specific purpose and I long
wished to get rid of it. It turns out that there's only one way to write the
untyped value nil, which is to actually write "nil" (maybe with parentheses).
So looking for that turned out to be simpler than using typeOrNil.

The new code does exactly that, and now we can just use varType and delete
typeOrNil. With this, there is now less code (excluding the test) and the code
is simpler and more correct.

Fixes #48008.

Change-Id: I8f2d80e61ae663c886924909f22bbfa634e7779c
Reviewed-on: https://go-review.googlesource.com/c/go/+/345790
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types2/stmt.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue48008.go2 [new file with mode: 0644]
src/cmd/compile/internal/types2/typexpr.go
src/go/types/stmt.go
src/go/types/testdata/fixedbugs/issue48008.go2 [new file with mode: 0644]
src/go/types/typexpr.go

index 8cfdf92e679ac08a1c2e2108cb58f008b885174f..3e2ac2e29e911b4353433b09c7660f22b4ee6bf8 100644 (file)
@@ -264,15 +264,29 @@ L:
        }
 }
 
+// isNil reports whether the expression e denotes the predeclared value nil.
+func (check *Checker) isNil(e syntax.Expr) bool {
+       // The only way to express the nil value is by literally writing nil (possibly in parentheses).
+       if name, _ := unparen(e).(*syntax.Name); name != nil {
+               _, ok := check.lookup(name.Value).(*Nil)
+               return ok
+       }
+       return false
+}
+
 func (check *Checker) caseTypes(x *operand, xtyp *Interface, types []syntax.Expr, seen map[Type]syntax.Expr) (T Type) {
+       var dummy operand
 L:
        for _, e := range types {
-               T = check.typOrNil(e)
-               if T == Typ[Invalid] {
-                       continue L
-               }
-               if T != nil {
-                       check.ordinaryType(e.Pos(), T)
+               // The spec allows the value nil instead of a type.
+               if check.isNil(e) {
+                       T = nil
+                       check.expr(&dummy, e) // run e through expr so we get the usual Info recordings
+               } else {
+                       T = check.varType(e)
+                       if T == Typ[Invalid] {
+                               continue L
+                       }
                }
                // look for duplicate types
                // (quadratic algorithm, but type switches tend to be reasonably small)
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48008.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue48008.go2
new file mode 100644 (file)
index 0000000..5c97268
--- /dev/null
@@ -0,0 +1,60 @@
+// 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[P any] struct{}
+
+func _(x interface{}) {
+       switch x.(type) {
+       case nil:
+       case int:
+
+       case T[int]:
+       case []T[int]:
+       case [10]T[int]:
+       case struct{T[int]}:
+       case *T[int]:
+       case func(T[int]):
+       case interface{m(T[int])}:
+       case map[T[int]] string:
+       case chan T[int]:
+
+       case T /* ERROR cannot use generic type T\[P interface{}\] without instantiation */ :
+       case []T /* ERROR cannot use generic type */ :
+       case [10]T /* ERROR cannot use generic type */ :
+       case struct{T /* ERROR cannot use generic type */ }:
+       case *T /* ERROR cannot use generic type */ :
+       case func(T /* ERROR cannot use generic type */ ):
+       case interface{m(T /* ERROR cannot use generic type */ )}:
+       case map[T /* ERROR cannot use generic type */ ] string:
+       case chan T /* ERROR cannot use generic type */ :
+
+       case T /* ERROR cannot use generic type */ , *T /* ERROR cannot use generic type */ :
+       }
+}
+
+// Make sure a parenthesized nil is ok.
+
+func _(x interface{}) {
+       switch x.(type) {
+       case ((nil)), int:
+       }
+}
+
+// Make sure we look for the predeclared nil.
+
+func _(x interface{}) {
+       type nil int
+       switch x.(type) {
+       case nil: // ok - this is the type nil
+       }
+}
+
+func _(x interface{}) {
+       var nil int
+       switch x.(type) {
+       case nil /* ERROR not a type */ : // not ok - this is the variable nil
+       }
+}
index 6938648bbc50e63a6a08fe82c7b0256c8eef685e..33e7559cc9b881af683042bd3d26ed5bc98d6504 100644 (file)
@@ -398,30 +398,6 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
        return typ
 }
 
-// typeOrNil type-checks the type expression (or nil value) e
-// and returns the type of e, or nil. If e is a type, it must
-// not be an (uninstantiated) generic type.
-// If e is neither a type nor nil, typeOrNil returns Typ[Invalid].
-// TODO(gri) should we also disallow non-var types?
-func (check *Checker) typOrNil(e syntax.Expr) Type {
-       var x operand
-       check.rawExpr(&x, e, nil)
-       switch x.mode {
-       case invalid:
-               // ignore - error reported before
-       case novalue:
-               check.errorf(&x, "%s used as type", &x)
-       case typexpr:
-               check.instantiatedOperand(&x)
-               return x.typ
-       case nilvalue:
-               return nil
-       default:
-               check.errorf(&x, "%s is not a type", &x)
-       }
-       return Typ[Invalid]
-}
-
 func (check *Checker) instantiatedType(x syntax.Expr, targsx []syntax.Expr, def *Named) Type {
        gtyp := check.genericType(x, true)
        if gtyp == Typ[Invalid] {
index fd6eba2deb26f5bbaa55a41340f196a9ff55dd89..056b21e3d2bc3dd62a748a5c94e54767050fd3e7 100644 (file)
@@ -276,15 +276,29 @@ L:
        }
 }
 
+// isNil reports whether the expression e denotes the predeclared value nil.
+func (check *Checker) isNil(e ast.Expr) bool {
+       // The only way to express the nil value is by literally writing nil (possibly in parentheses).
+       if name, _ := unparen(e).(*ast.Ident); name != nil {
+               _, ok := check.lookup(name.Name).(*Nil)
+               return ok
+       }
+       return false
+}
+
 func (check *Checker) caseTypes(x *operand, xtyp *Interface, types []ast.Expr, seen map[Type]ast.Expr) (T Type) {
+       var dummy operand
 L:
        for _, e := range types {
-               T = check.typeOrNil(e)
-               if T == Typ[Invalid] {
-                       continue L
-               }
-               if T != nil {
-                       check.ordinaryType(e, T)
+               // The spec allows the value nil instead of a type.
+               if check.isNil(e) {
+                       T = nil
+                       check.expr(&dummy, e) // run e through expr so we get the usual Info recordings
+               } else {
+                       T = check.varType(e)
+                       if T == Typ[Invalid] {
+                               continue L
+                       }
                }
                // look for duplicate types
                // (quadratic algorithm, but type switches tend to be reasonably small)
diff --git a/src/go/types/testdata/fixedbugs/issue48008.go2 b/src/go/types/testdata/fixedbugs/issue48008.go2
new file mode 100644 (file)
index 0000000..5c97268
--- /dev/null
@@ -0,0 +1,60 @@
+// 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[P any] struct{}
+
+func _(x interface{}) {
+       switch x.(type) {
+       case nil:
+       case int:
+
+       case T[int]:
+       case []T[int]:
+       case [10]T[int]:
+       case struct{T[int]}:
+       case *T[int]:
+       case func(T[int]):
+       case interface{m(T[int])}:
+       case map[T[int]] string:
+       case chan T[int]:
+
+       case T /* ERROR cannot use generic type T\[P interface{}\] without instantiation */ :
+       case []T /* ERROR cannot use generic type */ :
+       case [10]T /* ERROR cannot use generic type */ :
+       case struct{T /* ERROR cannot use generic type */ }:
+       case *T /* ERROR cannot use generic type */ :
+       case func(T /* ERROR cannot use generic type */ ):
+       case interface{m(T /* ERROR cannot use generic type */ )}:
+       case map[T /* ERROR cannot use generic type */ ] string:
+       case chan T /* ERROR cannot use generic type */ :
+
+       case T /* ERROR cannot use generic type */ , *T /* ERROR cannot use generic type */ :
+       }
+}
+
+// Make sure a parenthesized nil is ok.
+
+func _(x interface{}) {
+       switch x.(type) {
+       case ((nil)), int:
+       }
+}
+
+// Make sure we look for the predeclared nil.
+
+func _(x interface{}) {
+       type nil int
+       switch x.(type) {
+       case nil: // ok - this is the type nil
+       }
+}
+
+func _(x interface{}) {
+       var nil int
+       switch x.(type) {
+       case nil /* ERROR not a type */ : // not ok - this is the variable nil
+       }
+}
index baa4e3c2d0385e2853ee81c0f66715b858b6ceb9..5c8a6b497d6375847105f7102482663a318b6342 100644 (file)
@@ -383,33 +383,6 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
        return typ
 }
 
-// typeOrNil type-checks the type expression (or nil value) e
-// and returns the type of e, or nil. If e is a type, it must
-// not be an (uninstantiated) generic type.
-// If e is neither a type nor nil, typeOrNil returns Typ[Invalid].
-// TODO(gri) should we also disallow non-var types?
-func (check *Checker) typeOrNil(e ast.Expr) Type {
-       var x operand
-       check.rawExpr(&x, e, nil)
-       switch x.mode {
-       case invalid:
-               // ignore - error reported before
-       case novalue:
-               check.errorf(&x, _NotAType, "%s used as type", &x)
-       case typexpr:
-               check.instantiatedOperand(&x)
-               return x.typ
-       case value:
-               if x.isNil() {
-                       return nil
-               }
-               fallthrough
-       default:
-               check.errorf(&x, _NotAType, "%s is not a type", &x)
-       }
-       return Typ[Invalid]
-}
-
 func (check *Checker) instantiatedType(x ast.Expr, targsx []ast.Expr, def *Named) Type {
        gtyp := check.genericType(x, true)
        if gtyp == Typ[Invalid] {