}
}
seen[T] = e
- if T != nil {
+ if T != nil && xtyp != nil {
check.typeAssertion(e, x, xtyp, T, true)
}
}
if x.mode == invalid {
return
}
+
// TODO(gri) we may want to permit type switches on type parameter values at some point
+ var xtyp *Interface
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", &x)
- return
+ } else {
+ xtyp, _ = under(x.typ).(*Interface)
+ if xtyp == nil {
+ check.errorf(&x, "%s is not an interface", &x)
+ }
}
check.multipleSwitchDefaults(s.Body)
_ = y
}
- switch x := i /* ERROR "not an interface" */ .(type) {}
+ switch /* ERROR "x declared but not used" */ x := i /* ERROR "not an interface" */ .(type) {}
switch t := x.(type) {
case nil:
case T2 /* ERROR "wrong type for method m" */ :
case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561)
}
+
+
+ {
+ x := 1
+ v := 2
+ switch v /* ERROR "v [(]variable of type int[)] is not an interface" */ .(type) {
+ case int:
+ println(x)
+ println(x / /* ERROR "invalid operation: division by zero" */ 0)
+ case /* ERROR "1 is not a type" */ 1:
+ }
+ }
}
// Test that each case clause uses the correct type of the variable
}
}
seen[T] = e
- if T != nil {
+ if T != nil && xtyp != nil {
check.typeAssertion(e, x, xtyp, T)
}
}
return
}
// TODO(gri) we may want to permit type switches on type parameter values at some point
+ var xtyp *Interface
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)
- return
+ } else {
+ xtyp, _ = under(x.typ).(*Interface)
+ if xtyp == nil {
+ check.errorf(&x, _InvalidTypeSwitch, "%s is not an interface", &x)
+ }
}
check.multipleDefaults(s.Body.List)
_ = y
}
- switch x := i /* ERROR "not an interface" */ .(type) {}
+ switch x /* ERROR "x declared but not used" */ := i /* ERROR "not an interface" */ .(type) {}
switch t := x.(type) {
case nil:
case T2 /* ERROR "wrong type for method m" */ :
case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561)
}
+
+
+ {
+ x := 1
+ v := 2
+ switch v /* ERROR "v [(]variable of type int[)] is not an interface" */ .(type) {
+ case int:
+ println(x)
+ println(x / 0 /* ERROR "invalid operation: division by zero" */)
+ case 1 /* ERROR "expected type, found 1" */:
+ }
+ }
}
// Test that each case clause uses the correct type of the variable