Skip to content

Commit 759d1dd

Browse files
committed
[TypeChecker] Merge typeCheckConditionForStmt() with
typeCheckStmtCondition()
1 parent ebc99c6 commit 759d1dd

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,18 +2492,20 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
24922492
return !resultTy;
24932493
}
24942494

2495-
bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
2496-
Diag<> diagnosticForAlwaysTrue) {
2495+
bool TypeChecker::typeCheckConditionForStatement(LabeledConditionalStmt *stmt,
2496+
DeclContext *dc) {
24972497
auto &Context = dc->getASTContext();
24982498
bool hadError = false;
24992499
bool hadAnyFalsable = false;
2500+
auto cond = stmt->getCond();
25002501
for (auto &elt : cond) {
25012502
if (elt.getKind() == StmtConditionElement::CK_Availability) {
25022503
hadAnyFalsable = true;
25032504
continue;
25042505
}
25052506

25062507
if (auto E = elt.getBooleanOrNull()) {
2508+
assert(!E->getType() && "the bool condition is already type checked");
25072509
hadError |= typeCheckCondition(E, dc);
25082510
elt.setBoolean(E);
25092511
hadAnyFalsable = true;
@@ -2528,8 +2530,10 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
25282530
};
25292531

25302532
// Resolve the pattern.
2533+
assert(!elt.getPattern()->hasType() &&
2534+
"the pattern binding condition is already type checked");
25312535
auto *pattern = TypeChecker::resolvePattern(elt.getPattern(), dc,
2532-
/*isStmtCondition*/true);
2536+
/*isStmtCondition*/ true);
25332537
if (!pattern) {
25342538
typeCheckPatternFailed();
25352539
continue;
@@ -2554,37 +2558,29 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
25542558
hadAnyFalsable |= pattern->isRefutablePattern();
25552559
}
25562560

2557-
25582561
// If the binding is not refutable, and there *is* an else, reject it as
25592562
// unreachable.
25602563
if (!hadAnyFalsable && !hadError) {
25612564
auto &diags = dc->getASTContext().Diags;
2562-
diags.diagnose(cond[0].getStartLoc(), diagnosticForAlwaysTrue);
2563-
}
2564-
return false;
2565-
}
2566-
2567-
bool TypeChecker::typeCheckConditionForStatement(LabeledConditionalStmt *stmt,
2568-
DeclContext *dc) {
2569-
Diag<> diagnosticForAlwaysTrue = diag::invalid_diagnostic;
2570-
switch (stmt->getKind()) {
2571-
case StmtKind::If:
2572-
diagnosticForAlwaysTrue = diag::if_always_true;
2573-
break;
2574-
case StmtKind::While:
2575-
diagnosticForAlwaysTrue = diag::while_always_true;
2576-
break;
2577-
case StmtKind::Guard:
2578-
diagnosticForAlwaysTrue = diag::guard_always_succeeds;
2579-
break;
2580-
default:
2581-
llvm_unreachable("unknown LabeledConditionalStmt kind");
2565+
Diag<> msg = diag::invalid_diagnostic;
2566+
switch (stmt->getKind()) {
2567+
case StmtKind::If:
2568+
msg = diag::if_always_true;
2569+
break;
2570+
case StmtKind::While:
2571+
msg = diag::while_always_true;
2572+
break;
2573+
case StmtKind::Guard:
2574+
msg = diag::guard_always_succeeds;
2575+
break;
2576+
default:
2577+
llvm_unreachable("unknown LabeledConditionalStmt kind");
2578+
}
2579+
diags.diagnose(cond[0].getStartLoc(), msg);
25822580
}
25832581

2584-
StmtCondition cond = stmt->getCond();
2585-
bool result = typeCheckStmtCondition(cond, dc, diagnosticForAlwaysTrue);
25862582
stmt->setCond(cond);
2587-
return result;
2583+
return false;
25882584
}
25892585

25902586
/// Find the '~=` operator that can compare an expression inside a pattern to a

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -763,16 +763,6 @@ bool typeCheckCondition(Expr *&expr, DeclContext *dc);
763763
bool typeCheckConditionForStatement(LabeledConditionalStmt *stmt,
764764
DeclContext *dc);
765765

766-
/// Type check the given 'if', 'while', or 'guard' statement condition, which
767-
/// either converts an expression to a logic value or bind variables to the
768-
/// contents of an Optional.
769-
///
770-
/// \param cond The condition to type-check, which will be modified in place.
771-
///
772-
/// \returns true if an error occurred, false otherwise.
773-
bool typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
774-
Diag<> diagnosticForAlwaysTrue);
775-
776766
/// Determine the semantics of a checked cast operation.
777767
///
778768
/// \param fromType The source type of the cast.

0 commit comments

Comments
 (0)