@@ -2492,18 +2492,20 @@ bool TypeChecker::typeCheckCondition(Expr *&expr, DeclContext *dc) {
2492
2492
return !resultTy;
2493
2493
}
2494
2494
2495
- bool TypeChecker::typeCheckStmtCondition (StmtCondition &cond, DeclContext *dc ,
2496
- Diag<> diagnosticForAlwaysTrue ) {
2495
+ bool TypeChecker::typeCheckConditionForStatement (LabeledConditionalStmt *stmt ,
2496
+ DeclContext *dc ) {
2497
2497
auto &Context = dc->getASTContext ();
2498
2498
bool hadError = false ;
2499
2499
bool hadAnyFalsable = false ;
2500
+ auto cond = stmt->getCond ();
2500
2501
for (auto &elt : cond) {
2501
2502
if (elt.getKind () == StmtConditionElement::CK_Availability) {
2502
2503
hadAnyFalsable = true ;
2503
2504
continue ;
2504
2505
}
2505
2506
2506
2507
if (auto E = elt.getBooleanOrNull ()) {
2508
+ assert (!E->getType () && " the bool condition is already type checked" );
2507
2509
hadError |= typeCheckCondition (E, dc);
2508
2510
elt.setBoolean (E);
2509
2511
hadAnyFalsable = true ;
@@ -2528,8 +2530,10 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
2528
2530
};
2529
2531
2530
2532
// Resolve the pattern.
2533
+ assert (!elt.getPattern ()->hasType () &&
2534
+ " the pattern binding condition is already type checked" );
2531
2535
auto *pattern = TypeChecker::resolvePattern (elt.getPattern (), dc,
2532
- /* isStmtCondition*/ true );
2536
+ /* isStmtCondition*/ true );
2533
2537
if (!pattern) {
2534
2538
typeCheckPatternFailed ();
2535
2539
continue ;
@@ -2554,37 +2558,29 @@ bool TypeChecker::typeCheckStmtCondition(StmtCondition &cond, DeclContext *dc,
2554
2558
hadAnyFalsable |= pattern->isRefutablePattern ();
2555
2559
}
2556
2560
2557
-
2558
2561
// If the binding is not refutable, and there *is* an else, reject it as
2559
2562
// unreachable.
2560
2563
if (!hadAnyFalsable && !hadError) {
2561
2564
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);
2582
2580
}
2583
2581
2584
- StmtCondition cond = stmt->getCond ();
2585
- bool result = typeCheckStmtCondition (cond, dc, diagnosticForAlwaysTrue);
2586
2582
stmt->setCond (cond);
2587
- return result ;
2583
+ return false ;
2588
2584
}
2589
2585
2590
2586
// / Find the '~=` operator that can compare an expression inside a pattern to a
0 commit comments