Skip to content

[Sema] Sink Pattern invalidation into SyntacticElementTarget::markInvalid #83633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ Type DoCatchStmt::getCaughtErrorType() const {
->getCaseLabelItems()
.front()
.getPattern();
if (firstPattern->hasType())
if (firstPattern->hasType() && !firstPattern->getType()->hasError())
return firstPattern->getType();

return Type();
Expand Down
9 changes: 6 additions & 3 deletions lib/Sema/SyntacticElementTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,15 @@ void SyntacticElementTarget::markInvalid() const {
InvalidationWalker(ASTContext &ctx) : Ctx(ctx) {}

PreWalkResult<Expr *> walkToExprPre(Expr *E) override {
if (!E->getType())
E->setType(ErrorType::get(Ctx));

E->setType(ErrorType::get(Ctx));
return Action::Continue(E);
}

PreWalkResult<Pattern *> walkToPatternPre(Pattern *P) override {
P->setType(ErrorType::get(Ctx));
return Action::Continue(P);
}

PreWalkAction walkToDeclPre(Decl *D) override {
// Mark any VarDecls and PatternBindingDecls as invalid.
if (auto *VD = dyn_cast<VarDecl>(D)) {
Expand Down
41 changes: 4 additions & 37 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -852,30 +852,8 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
return false;
}

auto &Context = DC->getASTContext();
initializer = target.getAsExpr();

if (!initializer->getType())
initializer->setType(ErrorType::get(Context));

// Assign error types to the pattern and its variables, to prevent it from
// being referenced by the constraint system.
if (patternType->hasUnresolvedType() ||
patternType->hasPlaceholder() ||
patternType->hasUnboundGenericType()) {
pattern->setType(ErrorType::get(Context));
}

pattern->forEachVariable([&](VarDecl *var) {
// Don't change the type of a variable that we've been able to
// compute a type for.
if (var->hasInterfaceType() &&
!var->getTypeInContext()->hasUnboundGenericType() &&
!var->isInvalid())
return;

var->setInvalid();
});
pattern = target.getInitializationPattern();
return true;
}

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

auto failed = [&]() -> bool {
// Invalidate the pattern and the var decl.
stmt->getPattern()->setType(ErrorType::get(Context));
stmt->getPattern()->forEachVariable([&](VarDecl *var) {
if (var->hasInterfaceType() && !var->isInvalid())
return;
var->setInvalid();
});
return true;
};

auto target = SyntacticElementTarget::forForEachPreamble(stmt, dc);
if (!typeCheckTarget(target))
return failed();
return true;

if (auto *where = stmt->getWhere()) {
auto boolType = dc->getASTContext().getBoolType();
if (!boolType)
return failed();
return true;

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

return false;
}
Expand Down