Skip to content

Commit 80408d6

Browse files
committed
[QoI] Prevent a crash during diagnostics of ternary/if statements
While trying to diagnose problems with ternary/if statements don't allow clauses, when type-checked separately, to have unresolved type variables because that doesn't help to find errors.
1 parent 2e5817e commit 80408d6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7172,13 +7172,18 @@ bool FailureDiagnosis::visitBindOptionalExpr(BindOptionalExpr *BOE) {
71727172
}
71737173

71747174
bool FailureDiagnosis::visitIfExpr(IfExpr *IE) {
7175+
auto typeCheckClauseExpr = [&](Expr *clause) -> Expr * {
7176+
return typeCheckChildIndependently(clause, Type(), CTP_Unused, TCCOptions(),
7177+
nullptr, false);
7178+
};
7179+
71757180
// Check all of the subexpressions independently.
7176-
auto condExpr = typeCheckChildIndependently(IE->getCondExpr());
7181+
auto condExpr = typeCheckClauseExpr(IE->getCondExpr());
71777182
if (!condExpr) return true;
7178-
auto trueExpr = typeCheckChildIndependently(IE->getThenExpr());
7183+
auto trueExpr = typeCheckClauseExpr(IE->getThenExpr());
71797184
if (!trueExpr) return true;
71807185

7181-
auto falseExpr = typeCheckChildIndependently(IE->getElseExpr());
7186+
auto falseExpr = typeCheckClauseExpr(IE->getElseExpr());
71827187
if (!falseExpr) return true;
71837188

71847189
// If the true/false values already match, it must be a contextual problem.

test/Constraints/diagnostics.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func r18800223(_ i : Int) {
281281

282282

283283
var buttonTextColor: String?
284-
_ = (buttonTextColor != nil) ? 42 : {$0}; // expected-error {{result values in '? :' expression have mismatching types 'Int' and '(_) -> _'}}
284+
_ = (buttonTextColor != nil) ? 42 : {$0}; // expected-error {{type of expression is ambiguous without more context}}
285285
}
286286

287287
// <rdar://problem/21883806> Bogus "'_' can only appear in a pattern or on the left side of an assignment" is back
@@ -1091,3 +1091,14 @@ func sr5081() {
10911091
var b = [String]()
10921092
b = a[2...4] // expected-error {{cannot assign value of type 'ArraySlice<String>' to type '[String]'}}
10931093
}
1094+
1095+
func rdar17170728() {
1096+
var i: Int? = 1
1097+
var j: Int?
1098+
var k: Int? = 2
1099+
1100+
let _ = [i, j, k].reduce(0 as Int?) {
1101+
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1102+
// expected-error@-1 {{type of expression is ambiguous without more context}}
1103+
}
1104+
}

0 commit comments

Comments
 (0)