@@ -396,10 +396,10 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
396
396
RS->isImplicit ());
397
397
}
398
398
399
- auto failed = TC.typeCheckExpression (E, DC, ResultTy, CTP_ReturnStmt);
399
+ auto hadTypeError = TC.typeCheckExpression (E, DC, ResultTy, CTP_ReturnStmt);
400
400
RS->setResult (E);
401
401
402
- if (failed ) {
402
+ if (hadTypeError ) {
403
403
tryDiagnoseUnnecessaryCastOverOptionSet (TC.Context , E, ResultTy,
404
404
DC->getParentModule ());
405
405
return nullptr ;
@@ -415,57 +415,55 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
415
415
Type exnType = TC.getExceptionType (DC, TS->getThrowLoc ());
416
416
if (!exnType) return TS;
417
417
418
- auto failed = TC.typeCheckExpression (E, DC, exnType, CTP_ThrowStmt);
418
+ auto hadTypeError = TC.typeCheckExpression (E, DC, exnType, CTP_ThrowStmt);
419
419
TS->setSubExpr (E);
420
-
421
- if (failed) return nullptr ;
422
420
423
- return TS;
421
+ return hadTypeError ? nullptr : TS;
424
422
}
425
423
426
424
Stmt *visitDeferStmt (DeferStmt *DS) {
427
425
TC.typeCheckDecl (DS->getTempDecl (), /* isFirstPass*/ false );
428
426
429
427
Expr *theCall = DS->getCallExpr ();
430
- if (!TC.typeCheckExpression (theCall, DC))
431
- return nullptr ;
428
+ auto hadTypeError = TC.typeCheckExpression (theCall, DC);
432
429
DS->setCallExpr (theCall);
433
430
434
- return DS;
431
+ return hadTypeError ? nullptr : DS;
435
432
}
436
433
437
434
Stmt *visitIfStmt (IfStmt *IS) {
438
- StmtCondition C = IS-> getCond () ;
435
+ bool hadTypeError = false ;
439
436
440
- if (TC.typeCheckStmtCondition (C, DC, diag::if_always_true)) return 0 ;
437
+ StmtCondition C = IS->getCond ();
438
+ hadTypeError |= TC.typeCheckStmtCondition (C, DC, diag::if_always_true);
441
439
IS->setCond (C);
442
440
443
441
AddLabeledStmt ifNest (*this , IS);
444
442
445
443
Stmt *S = IS->getThenStmt ();
446
- if ( typeCheckStmt (S)) return 0 ;
444
+ hadTypeError |= typeCheckStmt (S);
447
445
IS->setThenStmt (S);
448
446
449
447
if ((S = IS->getElseStmt ())) {
450
- if ( typeCheckStmt (S)) return 0 ;
448
+ hadTypeError |= typeCheckStmt (S);
451
449
IS->setElseStmt (S);
452
450
}
453
451
454
- return IS;
452
+ return hadTypeError ? nullptr : IS;
455
453
}
456
454
457
455
Stmt *visitGuardStmt (GuardStmt *GS) {
456
+ bool hadTypeError = false ;
458
457
StmtCondition C = GS->getCond ();
459
- if (TC.typeCheckStmtCondition (C, DC, diag::guard_always_succeeds))
460
- return 0 ;
458
+ hadTypeError |= TC.typeCheckStmtCondition (C, DC, diag::guard_always_succeeds);
461
459
GS->setCond (C);
462
460
463
461
AddLabeledStmt ifNest (*this , GS);
464
462
465
463
Stmt *S = GS->getBody ();
466
- if ( typeCheckStmt (S)) return 0 ;
464
+ hadTypeError |= typeCheckStmt (S);
467
465
GS->setBody (S);
468
- return GS;
466
+ return hadTypeError ? nullptr : GS;
469
467
}
470
468
471
469
Stmt *visitIfConfigStmt (IfConfigStmt *ICS) {
@@ -479,69 +477,69 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
479
477
Stmt *visitDoStmt (DoStmt *DS) {
480
478
AddLabeledStmt loopNest (*this , DS);
481
479
Stmt *S = DS->getBody ();
482
- if ( typeCheckStmt (S)) return 0 ;
480
+ bool hadTypeError = typeCheckStmt (S);
483
481
DS->setBody (S);
484
- return DS;
482
+ return hadTypeError ? nullptr : DS;
485
483
}
486
484
487
485
Stmt *visitWhileStmt (WhileStmt *WS) {
486
+ bool hadTypeError = false ;
488
487
StmtCondition C = WS->getCond ();
489
- if ( TC.typeCheckStmtCondition (C, DC, diag::while_always_true)) return 0 ;
488
+ hadTypeError |= TC.typeCheckStmtCondition (C, DC, diag::while_always_true);
490
489
WS->setCond (C);
491
490
492
491
AddLabeledStmt loopNest (*this , WS);
493
492
Stmt *S = WS->getBody ();
494
- if ( typeCheckStmt (S)) return 0 ;
493
+ hadTypeError |= typeCheckStmt (S);
495
494
WS->setBody (S);
496
495
497
- return WS;
496
+ return hadTypeError ? nullptr : WS;
498
497
}
499
498
Stmt *visitRepeatWhileStmt (RepeatWhileStmt *RWS) {
499
+ bool hadTypeError = false ;
500
500
{
501
501
AddLabeledStmt loopNest (*this , RWS);
502
502
Stmt *S = RWS->getBody ();
503
- if ( typeCheckStmt (S)) return nullptr ;
503
+ hadTypeError |= typeCheckStmt (S);
504
504
RWS->setBody (S);
505
505
}
506
506
507
507
Expr *E = RWS->getCond ();
508
- if ( TC.typeCheckCondition (E, DC)) return nullptr ;
508
+ hadTypeError |= TC.typeCheckCondition (E, DC);
509
509
RWS->setCond (E);
510
- return RWS;
510
+ return hadTypeError ? nullptr : RWS;
511
511
}
512
512
Stmt *visitForStmt (ForStmt *FS) {
513
+ bool hadTypeError = false ;
513
514
// Type check any var decls in the initializer.
514
515
for (auto D : FS->getInitializerVarDecls ())
515
516
TC.typeCheckDecl (D, /* isFirstPass*/ false );
516
517
517
518
if (auto *Initializer = FS->getInitializer ().getPtrOrNull ()) {
518
- if (TC.typeCheckExpression (Initializer, DC, Type (), CTP_Unused,
519
- TypeCheckExprFlags::IsDiscarded))
520
- return nullptr ;
519
+ hadTypeError |= TC.typeCheckExpression (Initializer, DC, Type (), CTP_Unused,
520
+ TypeCheckExprFlags::IsDiscarded);
521
521
FS->setInitializer (Initializer);
522
522
TC.checkIgnoredExpr (Initializer);
523
523
}
524
524
525
525
if (auto *Cond = FS->getCond ().getPtrOrNull ()) {
526
- if (TC.typeCheckCondition (Cond, DC))
527
- return nullptr ;
526
+ hadTypeError |= TC.typeCheckCondition (Cond, DC);
528
527
FS->setCond (Cond);
529
528
}
530
529
531
530
if (auto *Increment = FS->getIncrement ().getPtrOrNull ()) {
532
- if (TC.typeCheckExpression (Increment, DC, Type (), CTP_Unused,
533
- TypeCheckExprFlags::IsDiscarded))
534
- return nullptr ;
531
+ hadTypeError |= TC.typeCheckExpression (Increment, DC, Type (), CTP_Unused,
532
+ TypeCheckExprFlags::IsDiscarded);
535
533
FS->setIncrement (Increment);
536
534
TC.checkIgnoredExpr (Increment);
537
535
}
538
536
539
537
AddLabeledStmt loopNest (*this , FS);
540
538
Stmt *S = FS->getBody ();
541
- if ( typeCheckStmt (S)) return nullptr ;
539
+ hadTypeError |= typeCheckStmt (S);
542
540
FS->setBody (S);
543
541
544
- return FS;
542
+ return hadTypeError ? nullptr : FS;
545
543
}
546
544
547
545
Stmt *visitForEachStmt (ForEachStmt *S) {
@@ -808,16 +806,15 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
808
806
}
809
807
810
808
Stmt *visitSwitchStmt (SwitchStmt *S) {
809
+ bool hadTypeError = false ;
811
810
// Type-check the subject expression.
812
811
Expr *subjectExpr = S->getSubjectExpr ();
813
- bool hadTypeError = TC.typeCheckExpression (subjectExpr, DC);
812
+ hadTypeError | = TC.typeCheckExpression (subjectExpr, DC);
814
813
subjectExpr = TC.coerceToMaterializable (subjectExpr);
815
-
816
- if (!subjectExpr)
817
- return nullptr ;
818
-
819
- S->setSubjectExpr (subjectExpr);
820
- Type subjectType = subjectExpr->getType ();
814
+ if (subjectExpr) {
815
+ S->setSubjectExpr (subjectExpr);
816
+ }
817
+ Type subjectType = S->getSubjectExpr ()->getType ();
821
818
822
819
// Type-check the case blocks.
823
820
AddSwitchNest switchNest (*this );
@@ -835,36 +832,32 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
835
832
if (auto *newPattern = TC.resolvePattern (pattern, DC,
836
833
/* isStmtCondition*/ false )) {
837
834
pattern = newPattern;
835
+ // Coerce the pattern to the subject's type.
836
+ if (TC.coercePatternToType (pattern, DC, subjectType,
837
+ TR_InExpression)) {
838
+ // If that failed, mark any variables binding pieces of the pattern
839
+ // as invalid to silence follow-on errors.
840
+ pattern->forEachVariable ([&](VarDecl *VD) {
841
+ VD->overwriteType (ErrorType::get (TC.Context ));
842
+ VD->setInvalid ();
843
+ });
844
+ hadTypeError = true ;
845
+ }
846
+ labelItem.setPattern (pattern);
838
847
} else {
839
848
hadTypeError = true ;
840
- continue ;
841
849
}
842
850
843
- // Coerce the pattern to the subject's type.
844
- if (TC.coercePatternToType (pattern, DC, subjectType, TR_InExpression)) {
845
- // If that failed, mark any variables binding pieces of the pattern
846
- // as invalid to silence follow-on errors.
847
- pattern->forEachVariable ([&](VarDecl *VD) {
848
- VD->overwriteType (ErrorType::get (TC.Context ));
849
- VD->setInvalid ();
850
- });
851
- hadTypeError = true ;
852
- }
853
- labelItem.setPattern (pattern);
854
-
855
851
// Check the guard expression, if present.
856
852
if (auto *guard = labelItem.getGuardExpr ()) {
857
- if (TC.typeCheckCondition (guard, DC))
858
- hadTypeError = true ;
859
- else
860
- labelItem.setGuardExpr (guard);
853
+ hadTypeError |= TC.typeCheckCondition (guard, DC);
854
+ labelItem.setGuardExpr (guard);
861
855
}
862
856
}
863
857
864
858
// Type-check the body statements.
865
859
Stmt *body = caseBlock->getBody ();
866
- if (typeCheckStmt (body))
867
- hadTypeError = true ;
860
+ hadTypeError |= typeCheckStmt (body);
868
861
caseBlock->setBody (body);
869
862
}
870
863
@@ -882,8 +875,9 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
882
875
}
883
876
884
877
bool checkCatchStmt (CatchStmt *S) {
878
+ bool hadTypeError = false ;
885
879
// Check the catch pattern.
886
- bool hadTypeError = TC.typeCheckCatchPattern (S, DC);
880
+ hadTypeError | = TC.typeCheckCatchPattern (S, DC);
887
881
888
882
// Check the guard expression, if present.
889
883
if (Expr *guard = S->getGuardExpr ()) {
@@ -910,11 +904,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
910
904
// Type-check the 'do' body. Type failures in here will generally
911
905
// not cause type failures in the 'catch' clauses.
912
906
Stmt *newBody = S->getBody ();
913
- if (typeCheckStmt (newBody)) {
914
- hadTypeError = true ;
915
- } else {
916
- S->setBody (newBody);
917
- }
907
+ hadTypeError |= typeCheckStmt (newBody);
908
+ S->setBody (newBody);
918
909
919
910
// Check all the catch clauses independently.
920
911
for (auto clause : S->getCatches ()) {
0 commit comments