Skip to content

[Sema] Remove generic environment tracking from StmtChecker #79633

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 1 commit into from
Feb 27, 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: 0 additions & 2 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2432,8 +2432,6 @@ class ConstraintSystem {
llvm::SmallDenseMap<PackElementExpr *, PackExpansionExpr *, 2>
PackElementExpansions;

llvm::SmallVector<GenericEnvironment *, 4> PackElementGenericEnvironments;

/// The set of functions that have been transformed by a result builder.
llvm::MapVector<AnyFunctionRef, AppliedBuilderTransform>
resultBuilderTransformed;
Expand Down
15 changes: 4 additions & 11 deletions include/swift/Sema/SyntacticElementTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ class SyntacticElementTarget {
ForEachStmt *stmt;
DeclContext *dc;
Pattern *pattern;
GenericEnvironment *packElementEnv;
ForEachStmtInfo info;
} forEachPreamble;

Expand Down Expand Up @@ -244,12 +243,10 @@ class SyntacticElementTarget {
uninitializedVar.type = patternTy;
}

SyntacticElementTarget(ForEachStmt *stmt, DeclContext *dc,
GenericEnvironment *packElementEnv)
SyntacticElementTarget(ForEachStmt *stmt, DeclContext *dc)
: kind(Kind::forEachPreamble) {
forEachPreamble.stmt = stmt;
forEachPreamble.dc = dc;
forEachPreamble.packElementEnv = packElementEnv;
}

/// Form a target for the initialization of a pattern from an expression.
Expand All @@ -271,8 +268,9 @@ class SyntacticElementTarget {
/// Form a target for the preamble of a for-in loop, excluding its where
/// clause and body.
static SyntacticElementTarget
forForEachPreamble(ForEachStmt *stmt, DeclContext *dc,
GenericEnvironment *packElementEnv = nullptr);
forForEachPreamble(ForEachStmt *stmt, DeclContext *dc) {
return {stmt, dc};
}

/// Form a target for a property with an attached property wrapper that is
/// initialized out-of-line.
Expand Down Expand Up @@ -550,11 +548,6 @@ class SyntacticElementTarget {
return expression.initialization.patternBindingIndex;
}

GenericEnvironment *getPackElementEnv() const {
assert(isForEachPreamble());
return forEachPreamble.packElementEnv;
}

const ForEachStmtInfo &getForEachStmtInfo() const {
assert(isForEachPreamble());
return forEachPreamble.info;
Expand Down
7 changes: 0 additions & 7 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4936,13 +4936,6 @@ bool ConstraintSystem::generateConstraints(
}

case SyntacticElementTarget::Kind::forEachPreamble: {

// Cache the outer generic environment, if it exists.
if (target.getPackElementEnv()) {
PackElementGenericEnvironments.push_back(target.getPackElementEnv());
ASSERT(!solverState && "Need to record a change");
}

// For a for-each statement, generate constraints for the pattern, where
// clause, and sequence traversal.
auto resultTarget = generateForEachPreambleConstraints(*this, target);
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,7 @@ ConstraintSystem::getPackExpansionEnvironment(PackExpansionExpr *expr) const {

GenericEnvironment *ConstraintSystem::createPackExpansionEnvironment(
PackExpansionExpr *expr, CanGenericTypeParamType shapeParam) {
auto *contextEnv = PackElementGenericEnvironments.empty()
? DC->getGenericEnvironmentOfContext()
: PackElementGenericEnvironments.back();
auto *contextEnv = DC->getGenericEnvironmentOfContext();
auto elementSig = getASTContext().getOpenedElementSignature(
contextEnv->getGenericSignature().getCanonicalSignature(), shapeParam);
auto contextSubs = contextEnv->getForwardingSubstitutionMap();
Expand Down
7 changes: 0 additions & 7 deletions lib/Sema/SyntacticElementTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,6 @@ SyntacticElementTarget::forReturn(ReturnStmt *returnStmt, Type contextTy,
return target;
}

SyntacticElementTarget
SyntacticElementTarget::forForEachPreamble(ForEachStmt *stmt, DeclContext *dc,
GenericEnvironment *packElementEnv) {
SyntacticElementTarget target(stmt, dc, packElementEnv);
return target;
}

SyntacticElementTarget SyntacticElementTarget::forPropertyWrapperInitializer(
VarDecl *wrappedVar, DeclContext *dc, Expr *initializer) {
SyntacticElementTarget target(initializer, dc, CTP_Initialization,
Expand Down
6 changes: 2 additions & 4 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,7 @@ bool TypeChecker::typeCheckPatternBinding(PatternBindingDecl *PBD,
return hadError;
}

bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt,
GenericEnvironment *packElementEnv) {
bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt) {
auto &Context = dc->getASTContext();
FrontendStatsTracer statsTracer(Context.Stats, "typecheck-for-each", stmt);
PrettyStackTraceStmt stackTrace(Context, "type-checking-for-each", stmt);
Expand All @@ -888,8 +887,7 @@ bool TypeChecker::typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt,
return true;
};

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

Expand Down
15 changes: 1 addition & 14 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,6 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {

StmtChecker(DeclContext *DC) : Ctx(DC->getASTContext()), DC(DC) { }

llvm::SmallVector<GenericEnvironment *, 4> genericSigStack;

//===--------------------------------------------------------------------===//
// Helper Functions.
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -1436,28 +1434,17 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
}

Stmt *visitForEachStmt(ForEachStmt *S) {
GenericEnvironment *genericSignature =
genericSigStack.empty() ? nullptr : genericSigStack.back();

if (TypeChecker::typeCheckForEachPreamble(DC, S, genericSignature))
if (TypeChecker::typeCheckForEachPreamble(DC, S))
return nullptr;

// Type-check the body of the loop.
auto sourceFile = DC->getParentSourceFile();
checkLabeledStmtShadowing(getASTContext(), sourceFile, S);

BraceStmt *Body = S->getBody();

if (auto packExpansion =
dyn_cast<PackExpansionExpr>(S->getParsedSequence()))
genericSigStack.push_back(packExpansion->getGenericEnvironment());

typeCheckStmt(Body);
S->setBody(Body);

if (isa<PackExpansionExpr>(S->getParsedSequence()))
genericSigStack.pop_back();

return S;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,7 @@ bool typeCheckPatternBinding(PatternBindingDecl *PBD, unsigned patternNumber,
/// together.
///
/// \returns true if a failure occurred.
bool typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt,
GenericEnvironment *packElementEnv);
bool typeCheckForEachPreamble(DeclContext *dc, ForEachStmt *stmt);

/// Compute the set of captures for the given closure.
void computeCaptures(AbstractClosureExpr *ACE);
Expand Down