]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: better error for type assertion/switch on type parameter value
authorRobert Findley <rfindley@google.com>
Tue, 16 Nov 2021 16:24:22 +0000 (11:24 -0500)
committerRobert Findley <rfindley@google.com>
Wed, 17 Nov 2021 14:10:29 +0000 (14:10 +0000)
This is a port of CL 363439 from types2 to go/types.

Change-Id: Ic71871874345e1d0a4a42703e3673aadd11f2bfc
Reviewed-on: https://go-review.googlesource.com/c/go/+/364378
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/expr.go
src/go/types/stmt.go
src/go/types/testdata/check/typeparams.go2

index ddb0149bf49b45af3d10222ab5af26f130e01a34..5e66a4a4b571a691ea3064a3ef7107c9b2be4ec6 100644 (file)
@@ -1432,6 +1432,11 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                if x.mode == invalid {
                        goto Error
                }
+               // TODO(gri) we may want to permit type assertions on type parameter values at some point
+               if isTypeParam(x.typ) {
+                       check.invalidOp(x, _InvalidAssert, "cannot use type assertion on type parameter value %s", x)
+                       goto Error
+               }
                xtyp, _ := under(x.typ).(*Interface)
                if xtyp == nil {
                        check.invalidOp(x, _InvalidAssert, "%s is not an interface", x)
index 363ea35acf9515c82f5acca03eecc1e2888b6d53..ee7d4e4cf1ef6ae710c883415bd63f25632c67c5 100644 (file)
@@ -685,6 +685,11 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
                if x.mode == invalid {
                        return
                }
+               // TODO(gri) we may want to permit type switches on type parameter values at some point
+               if isTypeParam(x.typ) {
+                       check.errorf(&x, _InvalidTypeSwitch, "cannot use type switch on type parameter value %s", &x)
+                       return
+               }
                xtyp, _ := under(x.typ).(*Interface)
                if xtyp == nil {
                        check.errorf(&x, _InvalidTypeSwitch, "%s is not an interface", &x)
index 9e2bffb539d448fc44330eddc8537c826406e1f4..fdbb7a27404899054b8b07613bda954f2ad6fbe7 100644 (file)
@@ -481,8 +481,8 @@ func (_ R2[X, Y]) m2(X) Y
 // type assertions and type switches over generic types lead to errors for now
 
 func _[T any](x T) {
-       _ = x /* ERROR not an interface */ .(int)
-       switch x /* ERROR not an interface */ .(type) {
+       _ = x /* ERROR cannot use type assertion */ .(int)
+       switch x /* ERROR cannot use type switch */ .(type) {
        }
 
        // work-around
@@ -493,8 +493,8 @@ func _[T any](x T) {
 }
 
 func _[T interface{~int}](x T) {
-       _ = x /* ERROR not an interface */ .(int)
-       switch x /* ERROR not an interface */ .(type) {
+       _ = x /* ERROR cannot use type assertion */ .(int)
+       switch x /* ERROR cannot use type switch */ .(type) {
        }
 
        // work-around