mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Using an unwrap operator with 'as' or the wrong keyword (i.e. `is`) when already checking a cast via ~= results in error: 'pattern variable binding cannot appear in an expression'. Add a diagnostic that provides more guidance and a fix-it for the removal of ?/! or replacement of 'is' with 'as'.
25 lines
1.0 KiB
Swift
25 lines
1.0 KiB
Swift
// RUN: %target-typecheck-verify-swift
|
||
|
||
enum E: Error { case e }
|
||
|
||
// rdar://106598067 – Make sure we don't crash.
|
||
let fn = {
|
||
do {} catch let x as? E {}
|
||
// expected-error@-1 {{cannot conditionally downcast in a type-casting pattern}}{{23-24=}}
|
||
// expected-error@-2 {{expression pattern of type 'E?' cannot match values of type 'any Error'}}
|
||
// expected-warning@-3 {{'catch' block is unreachable because no errors are thrown in 'do' block}}
|
||
}
|
||
|
||
// https://github.com/swiftlang/swift/issues/44631
|
||
let maybeInt: Any = 1
|
||
switch maybeInt {
|
||
case let intValue as? Int: _ = intValue
|
||
// expected-error@-1 {{cannot conditionally downcast in a type-casting pattern}}{{21-22=}}
|
||
// expected-error@-2 {{expression pattern of type 'Int?' cannot match values of type 'Any'}}
|
||
case let intValue as! Int: _ = intValue
|
||
// expected-error@-1 {{cannot force downcast in a type-casting pattern}}{{21-22=}}
|
||
case let intValue is Int: _ = intValue
|
||
// expected-error@-1 {{use 'as' keyword to bind a matched value}}{{19-21=as}}
|
||
default: break
|
||
}
|