]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: avoid unused variable error in invalid x.(type) expression
authorRobert Griesemer <gri@golang.org>
Tue, 21 Feb 2023 20:51:34 +0000 (12:51 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 22 Feb 2023 00:37:45 +0000 (00:37 +0000)
This change removes one of the two follow-on errors in the issue below.

For #58612.

Change-Id: If1eec5031e524bad33caa4a914f52e6a1e273b60
Reviewed-on: https://go-review.googlesource.com/c/go/+/470015
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/expr.go
src/go/types/expr.go
src/internal/types/testdata/fixedbugs/issue58612.go [new file with mode: 0644]

index 0be2a4533caf6636f0d438b8756c75bbd63a1b75..f53ecec855b06eab3aeeb05d25b9c0e2eacb809a 100644 (file)
@@ -1613,6 +1613,11 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                if x.mode == invalid {
                        goto Error
                }
+               // x.(type) expressions are encoded via TypeSwitchGuards
+               if e.Type == nil {
+                       check.error(e, InvalidSyntaxTree, "invalid use of AssertExpr")
+                       goto Error
+               }
                // TODO(gri) we may want to permit type assertions on type parameter values at some point
                if isTypeParam(x.typ) {
                        check.errorf(x, InvalidAssert, invalidOp+"cannot use type assertion on type parameter value %s", x)
@@ -1622,11 +1627,6 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
                        check.errorf(x, InvalidAssert, invalidOp+"%s is not an interface", x)
                        goto Error
                }
-               // x.(type) expressions are encoded via TypeSwitchGuards
-               if e.Type == nil {
-                       check.error(e, InvalidSyntaxTree, "invalid use of AssertExpr")
-                       goto Error
-               }
                T := check.varType(e.Type)
                if T == Typ[Invalid] {
                        goto Error
@@ -1638,6 +1638,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin
        case *syntax.TypeSwitchGuard:
                // x.(type) expressions are handled explicitly in type switches
                check.error(e, InvalidSyntaxTree, "use of .(type) outside type switch")
+               check.use(e.X)
                goto Error
 
        case *syntax.CallExpr:
index d67bc8b756407d124b087e29a04dc8b38d88c3aa..df2ada4b25e2110728887a33d431b3e7ab55bd90 100644 (file)
@@ -1596,6 +1596,13 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                if x.mode == invalid {
                        goto Error
                }
+               // x.(type) expressions are handled explicitly in type switches
+               if e.Type == nil {
+                       // Don't use invalidAST because this can occur in the AST produced by
+                       // go/parser.
+                       check.error(e, BadTypeKeyword, "use of .(type) outside type switch")
+                       goto Error
+               }
                // TODO(gri) we may want to permit type assertions on type parameter values at some point
                if isTypeParam(x.typ) {
                        check.errorf(x, InvalidAssert, invalidOp+"cannot use type assertion on type parameter value %s", x)
@@ -1605,13 +1612,6 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                        check.errorf(x, InvalidAssert, invalidOp+"%s is not an interface", x)
                        goto Error
                }
-               // x.(type) expressions are handled explicitly in type switches
-               if e.Type == nil {
-                       // Don't use invalidAST because this can occur in the AST produced by
-                       // go/parser.
-                       check.error(e, BadTypeKeyword, "use of .(type) outside type switch")
-                       goto Error
-               }
                T := check.varType(e.Type)
                if T == Typ[Invalid] {
                        goto Error
diff --git a/src/internal/types/testdata/fixedbugs/issue58612.go b/src/internal/types/testdata/fixedbugs/issue58612.go
new file mode 100644 (file)
index 0000000..db6a62d
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2023 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
+
+func _() {
+       var x = new(T)
+       f[x /* ERROR "not a type" */ /* ERROR "use of .(type) outside type switch" */ .(type)]()
+}
+
+type T struct{}
+
+func f[_ any]() {}