Skip to content

Commit be3f949

Browse files
committed
[Diagnostics] Add support for new "ternary branch" element to contextual mismatch
1 parent e19ef09 commit be3f949

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
248248
bool visitSubscriptExpr(SubscriptExpr *SE);
249249
bool visitApplyExpr(ApplyExpr *AE);
250250
bool visitCoerceExpr(CoerceExpr *CE);
251-
bool visitIfExpr(IfExpr *IE);
252251
bool visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *E);
253252
};
254253
} // end anonymous namespace
@@ -1890,37 +1889,6 @@ bool FailureDiagnosis::visitCoerceExpr(CoerceExpr *CE) {
18901889
return false;
18911890
}
18921891

1893-
bool FailureDiagnosis::visitIfExpr(IfExpr *IE) {
1894-
auto typeCheckClauseExpr = [&](Expr *clause, Type contextType = Type(),
1895-
ContextualTypePurpose convertPurpose =
1896-
CTP_Unused) -> Expr * {
1897-
// Provide proper contextual type when type conversion is specified.
1898-
return typeCheckChildIndependently(clause, contextType, convertPurpose,
1899-
TCCOptions(), nullptr, false);
1900-
};
1901-
// Check all of the subexpressions independently.
1902-
auto condExpr = typeCheckClauseExpr(IE->getCondExpr());
1903-
if (!condExpr) return true;
1904-
auto trueExpr = typeCheckClauseExpr(IE->getThenExpr(), CS.getContextualType(),
1905-
CS.getContextualTypePurpose());
1906-
if (!trueExpr) return true;
1907-
auto falseExpr = typeCheckClauseExpr(
1908-
IE->getElseExpr(), CS.getContextualType(), CS.getContextualTypePurpose());
1909-
if (!falseExpr) return true;
1910-
1911-
// If the true/false values already match, it must be a contextual problem.
1912-
if (CS.getType(trueExpr)->isEqual(CS.getType(falseExpr)))
1913-
return false;
1914-
1915-
// Otherwise, the true/false result types must not be matching.
1916-
diagnose(IE->getColonLoc(), diag::if_expr_cases_mismatch,
1917-
CS.getType(trueExpr), CS.getType(falseExpr))
1918-
.highlight(trueExpr->getSourceRange())
1919-
.highlight(falseExpr->getSourceRange());
1920-
return true;
1921-
}
1922-
1923-
19241892
bool FailureDiagnosis::
19251893
visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *E) {
19261894
// Don't walk the children for this node, it leads to multiple diagnostics

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,14 @@ bool ContextualFailure::diagnoseAsError() {
19061906
break;
19071907
}
19081908

1909+
case ConstraintLocator::TernaryBranch: {
1910+
auto *ifExpr = cast<IfExpr>(getRawAnchor());
1911+
fromType = getType(ifExpr->getThenExpr());
1912+
toType = getType(ifExpr->getElseExpr());
1913+
diagnostic = diag::if_expr_cases_mismatch;
1914+
break;
1915+
}
1916+
19091917
case ConstraintLocator::ContextualType: {
19101918
if (diagnoseConversionToBool())
19111919
return true;

0 commit comments

Comments
 (0)