Skip to content

[Fixit] Add a fixit to add forced cast to the contextual expected type. #3749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3353,8 +3353,10 @@ static bool isIntegerToStringIndexConversion(Type fromType, Type toType,
/// expected, by wrapping the expression in a call to the rawValue
/// accessor
///
/// - Return true on the fixit is added, false otherwise.
///
/// This helps migration with SDK changes.
static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
static bool tryRawRepresentableFixIts(InFlightDiagnostic &diag,
ConstraintSystem *CS,
Type fromType,
Type toType,
Expand Down Expand Up @@ -3405,7 +3407,7 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
convWrapAfter += ")";
}
fixIt(convWrapBefore, convWrapAfter);
return;
return true;
}
}

Expand All @@ -3419,9 +3421,10 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
convWrapAfter += ")";
}
fixIt(convWrapBefore, convWrapAfter);
return;
return true;
}
}
return false;
}

/// Attempts to add fix-its for these two mistakes:
Expand All @@ -3432,14 +3435,16 @@ static void tryRawRepresentableFixIts(InFlightDiagnostic &diag,
/// - Passing an integer but expecting different integer type. The fixit adds
/// a wrapping cast.
///
/// - Return true on the fixit is added, false otherwise.
///
/// This helps migration with SDK changes.
static void tryIntegerCastFixIts(InFlightDiagnostic &diag,
static bool tryIntegerCastFixIts(InFlightDiagnostic &diag,
ConstraintSystem *CS,
Type fromType,
Type toType,
Expr *expr) {
if (!isIntegerType(fromType, CS) || !isIntegerType(toType, CS))
return;
return false;

auto getInnerCastedExpr = [&]() -> Expr* {
CallExpr *CE = dyn_cast<CallExpr>(expr);
Expand All @@ -3459,7 +3464,7 @@ static void tryIntegerCastFixIts(InFlightDiagnostic &diag,
// Remove the unnecessary cast.
diag.fixItRemoveChars(expr->getLoc(), innerE->getStartLoc())
.fixItRemove(expr->getEndLoc());
return;
return true;
}
}

Expand All @@ -3470,6 +3475,24 @@ static void tryIntegerCastFixIts(InFlightDiagnostic &diag,
SourceRange exprRange = expr->getSourceRange();
diag.fixItInsert(exprRange.Start, convWrapBefore);
diag.fixItInsertAfter(exprRange.End, convWrapAfter);
return true;
}

static bool
addTypeCoerceFixit(InFlightDiagnostic &diag, ConstraintSystem *CS,
Type fromType, Type toType, Expr *expr) {
if (CS->getTypeChecker().typeCheckCheckedCast(fromType, toType, CS->DC,
SourceLoc(), SourceRange(), SourceRange(), [](Type T) { return false; },
true) != CheckedCastKind::Unresolved) {
SmallString<32> buffer;
llvm::raw_svector_ostream OS(buffer);
toType->print(OS);
diag.fixItInsert(Lexer::getLocForEndOfToken(CS->DC->getASTContext().SourceMgr,
expr->getEndLoc()),
(llvm::Twine(" as! ") + OS.str()).str());
return true;
}
return false;
}

bool FailureDiagnosis::diagnoseContextualConversionError() {
Expand Down Expand Up @@ -3746,11 +3769,12 @@ bool FailureDiagnosis::diagnoseContextualConversionError() {
case CTP_DictionaryValue:
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
KnownProtocolKind::ExpressibleByIntegerLiteral,
expr);
expr) ||
tryRawRepresentableFixIts(diag, CS, exprType, contextualType,
KnownProtocolKind::ExpressibleByStringLiteral,
expr);
tryIntegerCastFixIts(diag, CS, exprType, contextualType, expr);
expr) ||
tryIntegerCastFixIts(diag, CS, exprType, contextualType, expr) ||
addTypeCoerceFixit(diag, CS, exprType, contextualType, expr);
break;

default:
Expand Down
19 changes: 19 additions & 0 deletions test/Sema/diag_type_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ func foo3 () {
foo2(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafePointer<Int>'}} {{none}}
foo4(1) // expected-error {{cannot convert value of type 'Int' to expected argument type 'UnsafeMutablePointer<Int>'}} {{none}}
}

class A {}
class B : A {}
func foo5(b : B) {}
func foo6(a : A) {
foo5(b : a) // expected-error {{cannot convert value of type 'A' to expected argument type 'B'}} {{13-13= as! B}}
}

func foo7(b : [B]) {}
func foo8(a : [A]) {
foo7(b : a) // expected-error {{cannot convert value of type '[A]' to expected argument type '[B]'}} {{13-13= as! [B]}}
}

protocol P1 {}
struct S1 : P1 {}
func foo9(s : S1) {}
func foo10(p : P1) {
foo9(s : p) // expected-error {{cannot convert value of type 'P1' to expected argument type 'S1'}} {{13-13= as! S1}}
}