Don't suggest adding 'as' in a pattern matching context where it will do no good.

This commit is contained in:
gregomni
2017-10-26 15:49:54 -07:00
parent cdce937c51
commit 55edab880f
2 changed files with 13 additions and 0 deletions

View File

@@ -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);

View File

@@ -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) {