Skip to content

Commit 0eca304

Browse files
authored
Merge pull request #83633 from hamishknight/pat-pat
[Sema] Sink Pattern invalidation into `SyntacticElementTarget::markInvalid`
2 parents 09edc47 + dad7c26 commit 0eca304

File tree

3 files changed

+11
-41
lines changed

3 files changed

+11
-41
lines changed

lib/AST/Stmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ Type DoCatchStmt::getCaughtErrorType() const {
493493
->getCaseLabelItems()
494494
.front()
495495
.getPattern();
496-
if (firstPattern->hasType())
496+
if (firstPattern->hasType() && !firstPattern->getType()->hasError())
497497
return firstPattern->getType();
498498

499499
return Type();

lib/Sema/SyntacticElementTarget.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,15 @@ void SyntacticElementTarget::markInvalid() const {
302302
InvalidationWalker(ASTContext &ctx) : Ctx(ctx) {}
303303

304304
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
305-
if (!E->getType())
306-
E->setType(ErrorType::get(Ctx));
307-
305+
E->setType(ErrorType::get(Ctx));
308306
return Action::Continue(E);
309307
}
310308

309+
PreWalkResult<Pattern *> walkToPatternPre(Pattern *P) override {
310+
P->setType(ErrorType::get(Ctx));
311+
return Action::Continue(P);
312+
}
313+
311314
PreWalkAction walkToDeclPre(Decl *D) override {
312315
// Mark any VarDecls and PatternBindingDecls as invalid.
313316
if (auto *VD = dyn_cast<VarDecl>(D)) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -852,30 +852,8 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
852852
return false;
853853
}
854854

855-
auto &Context = DC->getASTContext();
856855
initializer = target.getAsExpr();
857-
858-
if (!initializer->getType())
859-
initializer->setType(ErrorType::get(Context));
860-
861-
// Assign error types to the pattern and its variables, to prevent it from
862-
// being referenced by the constraint system.
863-
if (patternType->hasUnresolvedType() ||
864-
patternType->hasPlaceholder() ||
865-
patternType->hasUnboundGenericType()) {
866-
pattern->setType(ErrorType::get(Context));
867-
}
868-
869-
pattern->forEachVariable([&](VarDecl *var) {
870-
// Don't change the type of a variable that we've been able to
871-
// compute a type for.
872-
if (var->hasInterfaceType() &&
873-
!var->getTypeInContext()->hasUnboundGenericType() &&
874-
!var->isInvalid())
875-
return;
876-
877-
var->setInvalid();
878-
});
856+
pattern = target.getInitializationPattern();
879857
return true;
880858
}
881859

@@ -927,25 +905,14 @@ bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt) {
927905
FrontendStatsTracer statsTracer(Context.Stats, "typecheck-for-each", stmt);
928906
PrettyStackTraceStmt stackTrace(Context, "type-checking-for-each", stmt);
929907

930-
auto failed = [&]() -> bool {
931-
// Invalidate the pattern and the var decl.
932-
stmt->getPattern()->setType(ErrorType::get(Context));
933-
stmt->getPattern()->forEachVariable([&](VarDecl *var) {
934-
if (var->hasInterfaceType() && !var->isInvalid())
935-
return;
936-
var->setInvalid();
937-
});
938-
return true;
939-
};
940-
941908
auto target = SyntacticElementTarget::forForEachPreamble(stmt, dc);
942909
if (!typeCheckTarget(target))
943-
return failed();
910+
return true;
944911

945912
if (auto *where = stmt->getWhere()) {
946913
auto boolType = dc->getASTContext().getBoolType();
947914
if (!boolType)
948-
return failed();
915+
return true;
949916

950917
SyntacticElementTarget whereClause(where, dc, {boolType, CTP_Condition},
951918
/*isDiscarded=*/false);
@@ -959,7 +926,7 @@ bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt) {
959926
// Check to see if the sequence expr is throwing (in async context),
960927
// if so require the stmt to have a `try`.
961928
if (diagnoseUnhandledThrowsInAsyncContext(dc, stmt))
962-
return failed();
929+
return true;
963930

964931
return false;
965932
}

0 commit comments

Comments
 (0)