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.errorf(x, invalidOp+"cannot use type assertion on type parameter value %s", x)
+ goto Error
+ }
xtyp, _ := under(x.typ).(*Interface)
if xtyp == nil {
- check.errorf(x, "%s is not an interface type", x)
+ check.errorf(x, invalidOp+"%s is not an interface", x)
goto Error
}
// x.(type) expressions are encoded via TypeSwitchGuards
if x.mode == invalid {
return
}
- // Caution: We're not using asInterface here because we don't want
- // to switch on a suitably constrained type parameter (for
- // now).
- // TODO(gri) Need to revisit this.
+ // TODO(gri) we may want to permit type switches on type parameter values at some point
+ if isTypeParam(x.typ) {
+ check.errorf(&x, "cannot use type switch on type parameter value %s", &x)
+ return
+ }
xtyp, _ := under(x.typ).(*Interface)
if xtyp == nil {
- check.errorf(&x, "%s is not an interface type", &x)
+ check.errorf(&x, "%s is not an interface", &x)
return
}
// 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
}
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
// cannot type-assert non-interfaces
f := 2.0
- _ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface type"
+ _ = f.(int) // ERROR "non-interface type|only valid for interface types|not an interface"
}
func noninterface() {
var i int
- switch i.(type) { // ERROR "cannot type switch on non-interface value|not an interface type"
+ switch i.(type) { // ERROR "cannot type switch on non-interface value|not an interface"
case string:
case int:
}
name string
}
var s S
- switch s.(type) { // ERROR "cannot type switch on non-interface value|not an interface type"
+ switch s.(type) { // ERROR "cannot type switch on non-interface value|not an interface"
}
}