mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Diagnostics] Allow integer cast fix-its for assignment mismatches
This commit is contained in:
@@ -2120,7 +2120,9 @@ bool ContextualFailure::diagnoseAsError() {
|
||||
}
|
||||
|
||||
if (isExpr<AssignExpr>(anchor)) {
|
||||
emitDiagnostic(diag::cannot_convert_assign, getFromType(), getToType());
|
||||
auto diagnostic = emitDiagnostic(diag::cannot_convert_assign,
|
||||
getFromType(), getToType());
|
||||
tryIntegerCastFixIts(diagnostic);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2729,6 +2731,15 @@ bool ContextualFailure::tryIntegerCastFixIts(
|
||||
auto fromType = getFromType();
|
||||
auto toType = getToType();
|
||||
|
||||
auto anchor = getAnchor();
|
||||
auto exprRange = getSourceRange();
|
||||
|
||||
if (auto *assignment = getAsExpr<AssignExpr>(anchor)) {
|
||||
toType = toType->lookThroughAllOptionalTypes();
|
||||
anchor = assignment->getSrc();
|
||||
exprRange = assignment->getSrc()->getSourceRange();
|
||||
}
|
||||
|
||||
if (!isIntegerType(fromType) || !isIntegerType(toType))
|
||||
return false;
|
||||
|
||||
@@ -2747,8 +2758,8 @@ bool ContextualFailure::tryIntegerCastFixIts(
|
||||
return parenE->getSubExpr();
|
||||
};
|
||||
|
||||
if (auto *anchor = getAsExpr(getAnchor())) {
|
||||
if (Expr *innerE = getInnerCastedExpr(anchor)) {
|
||||
if (auto *expr = getAsExpr(anchor)) {
|
||||
if (Expr *innerE = getInnerCastedExpr(expr)) {
|
||||
Type innerTy = getType(innerE);
|
||||
if (TypeChecker::isConvertibleTo(innerTy, toType, getDC())) {
|
||||
// Remove the unnecessary cast.
|
||||
@@ -2763,7 +2774,6 @@ bool ContextualFailure::tryIntegerCastFixIts(
|
||||
std::string convWrapBefore = toType.getString();
|
||||
convWrapBefore += "(";
|
||||
std::string convWrapAfter = ")";
|
||||
SourceRange exprRange = getSourceRange();
|
||||
diagnostic.fixItInsert(exprRange.Start, convWrapBefore);
|
||||
diagnostic.fixItInsertAfter(exprRange.End, convWrapAfter);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user