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>
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)
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
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:
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)
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
--- /dev/null
+// 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]() {}