mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Don't suggest adding 'as' in a pattern matching context where it will do no good.
This commit is contained in:
@@ -7544,6 +7544,16 @@ bool ConstraintSystem::applySolutionFix(Expr *expr,
|
||||
if (!useAs && !useAsBang)
|
||||
return false;
|
||||
|
||||
// If we're performing pattern matching, "as" means something completely different...
|
||||
if (auto binOpExpr = dyn_cast<BinaryExpr>(expr)) {
|
||||
auto overloadedFn = dyn_cast<OverloadedDeclRefExpr>(binOpExpr->getFn());
|
||||
if (overloadedFn && overloadedFn->getDecls().size() > 0) {
|
||||
ValueDecl *decl0 = overloadedFn->getDecls()[0];
|
||||
if (decl0->getBaseName() == decl0->getASTContext().Id_MatchOperator)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool needsParensInside = exprNeedsParensBeforeAddingAs(TC, DC, affected);
|
||||
bool needsParensOutside = exprNeedsParensAfterAddingAs(TC, DC, affected,
|
||||
expr);
|
||||
|
||||
@@ -53,6 +53,9 @@ func testAmbiguousStringComparisons(s: String) {
|
||||
let a10 = nsString <= s // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}} {{21-21= as String}}
|
||||
let a11 = nsString >= s // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}} {{21-21= as String}}
|
||||
let a12 = nsString > s // expected-error{{'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?}} {{21-21= as String}}
|
||||
|
||||
// Shouldn't suggest 'as' in a pattern-matching context, as opposed to all these other situations
|
||||
if case nsString = "" {} // expected-error{{expression pattern of type 'NSString' cannot match values of type 'String'}}
|
||||
}
|
||||
|
||||
func testStringDeprecation(hello: String) {
|
||||
|
||||
Reference in New Issue
Block a user