Files
swift-mirror/test/Constraints/rdar106598067.swift
James Brown f0eef0f583 [CS] Diagnose misuse of CheckedCastExpr with ~=
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'.
2024-09-24 01:45:51 -04:00

25 lines
1.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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
}