@@ -2355,23 +2355,23 @@ bool TypeChecker::typeCheckBinding(
23552355 if (!initializer->getType ())
23562356 initializer->setType (ErrorType::get (Context));
23572357
2358- // If the type of the pattern is inferred, assign error types to the pattern
2359- // and its variables, to prevent it from being referenced by the constraint
2360- // system.
2358+ // Assign error types to the pattern and its variables, to prevent it from
2359+ // being referenced by the constraint system.
23612360 if (patternType->hasUnresolvedType () ||
23622361 patternType->hasUnboundGenericType ()) {
23632362 pattern->setType (ErrorType::get (Context));
2364- pattern->forEachVariable ([&](VarDecl *var) {
2365- // Don't change the type of a variable that we've been able to
2366- // compute a type for.
2367- if (var->hasInterfaceType () &&
2368- !var->getType ()->hasUnboundGenericType () &&
2369- !var->isInvalid ())
2370- return ;
2371-
2372- var->setInvalid ();
2373- });
23742363 }
2364+
2365+ pattern->forEachVariable ([&](VarDecl *var) {
2366+ // Don't change the type of a variable that we've been able to
2367+ // compute a type for.
2368+ if (var->hasInterfaceType () &&
2369+ !var->getType ()->hasUnboundGenericType () &&
2370+ !var->isInvalid ())
2371+ return ;
2372+
2373+ var->setInvalid ();
2374+ });
23752375 return true ;
23762376}
23772377
@@ -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,13 +2558,28 @@ 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);
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);
25632580 }
2581+
2582+ stmt->setCond (cond);
25642583 return false ;
25652584}
25662585
0 commit comments