[CS] Diagnose misuse of CheckedCastExpr with ~=
Issue
When applying unwrap operator to 'as' or using 'is' in a type-cast pattern (specifically one that binds a value), the diagnostic for the resulting error could provide more guidance and suggest a fix-it.
Example with current diagnostic:
let maybeInt: Any = 1
if case let intValue as? Int = maybeInt {}
`- error: pattern variable binding cannot appear in an expression
Fix
- Added diagnostic
invalid_cast_in_patternand an associated fix-it to remove !/? or replace 'is' with 'as'. - Modified test/Constraints/rdar106598067.swift to account for the new diagnostic.
Example with new diagnostic:
if case let intValue as? Int = maybeInt {}
~~~~~~~~ ^ ~~~~~
//`- error: cannot conditionally downcast in a type-casting pattern
// - fix-it: remove '?'
if case let intValue as! Int = maybeInt {}
~~~~~~~~ ^ ~~~~~
//`- error: cannot force downcast in a type-casting pattern
// - fix-it: remove '!'
if case let intValue is Int = maybeInt {}
~~~~~~~~ ^ ~~~~
//`- error: use 'as' keyword to bind a matched value
// - fix-it: replace 'is' with 'as'
Resolves https://github.com/swiftlang/swift/issues/44631.
@xedin Thanks for the review! Will address your comments shortly.
Please re-request a review once you are done to help me keep track of this.
@swift-ci please test
@xedin Thanks for your help with this! It seems like the test failures should be fixed by https://github.com/swiftlang/swift/pull/76679.
@swift-ci please test
@swift-ci please test Windows platform
https://github.com/swiftlang/swift-testing/pull/731 @swift-ci please test Windows platform
@swift-ci please test macOS platform