Skip to content

[CS] Ensure DeclContext is switched for if/switch expressions #79420

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 17, 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
29 changes: 24 additions & 5 deletions lib/Sema/CSStep.h
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,27 @@ class DisjunctionStep final : public BindingStep<DisjunctionChoiceProducer> {
}
};

/// Retrieves the DeclContext that a conjunction should be solved within.
static DeclContext *getDeclContextForConjunction(ConstraintLocator *loc) {
// Closures introduce a new DeclContext that needs switching into.
auto anchor = loc->getAnchor();
if (loc->directlyAt<ClosureExpr>())
return castToExpr<ClosureExpr>(anchor);

// SingleValueStmtExprs need to switch to their enclosing context. This
// is unfortunately necessary since they can be present in single-expression
// closures, which don't have their DeclContext established since they're
// solved together with the rest of the system.
if (loc->isForSingleValueStmtConjunction())
return castToExpr<SingleValueStmtExpr>(anchor)->getDeclContext();

// Do the same for TapExprs.
if (loc->directlyAt<TapExpr>())
return castToExpr<TapExpr>(anchor)->getVar()->getDeclContext();

return nullptr;
}

class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
/// Snapshot of the constraint system before conjunction.
class SolverSnapshot {
Expand All @@ -826,11 +847,9 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
: CS(cs), Conjunction(conjunction),
TypeVars(std::move(cs.TypeVariables)) {
auto *locator = Conjunction->getLocator();
// If this conjunction represents a closure, we need to
// switch declaration context over to it.
if (locator->directlyAt<ClosureExpr>()) {
DC.emplace(CS.DC, castToExpr<ClosureExpr>(locator->getAnchor()));
}
// If we need to switch into a new DeclContext for the conjunction, do so.
if (auto *newDC = getDeclContextForConjunction(locator))
DC.emplace(CS.DC, newDC);

auto &CG = CS.getConstraintGraph();
// Remove all of the current inactive constraints.
Expand Down
22 changes: 22 additions & 0 deletions test/Constraints/if_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -706,3 +706,25 @@ func testInvalidOptionalChainingInIfContext() {
let v63796 = 1
if v63796? {} // expected-error{{cannot use optional chaining on non-optional value of type 'Int'}}
}

// https://github.com/swiftlang/swift/issues/79395
_ = {
if .random() {
struct S: Error {}
throw S()
} else {
1
}
}
_ = {
if .random() {
if .random() {
struct S: Error {}
throw S()
} else {
0
}
} else {
1
}
}
25 changes: 25 additions & 0 deletions test/Constraints/switch_expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -819,3 +819,28 @@ func builderInClosure() {
}
}
}

// https://github.com/swiftlang/swift/issues/79395
_ = {
switch Bool.random() {
case true:
struct S: Error {}
throw S()
case false:
1
}
}
_ = {
switch Bool.random() {
case true:
switch Bool.random() {
case true:
struct S: Error {}
throw S()
case false:
0
}
case false:
1
}
}