[Diagnostics] Allow integer cast fix-its for assignment mismatches

This commit is contained in:
Pavel Yaskevich
2020-12-11 17:31:50 -08:00
parent 0e6260dd89
commit ec31d77fa6
3 changed files with 18 additions and 8 deletions

View File

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