Skip to content

Conversation

hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Sep 7, 2025

  • Wire up the parent VarDecl chain when we create a CaseStmt rather than during type-checking
  • Introduce CaseStmt::createImplicit and re-use the existing implementation for creating the case body variables
  • Simplify the implementations of VarDecl::getParentPattern and VarDecl::getRecursiveParentPatternStmt and remove findParentPatternCaseStmtAndPattern
  • Improve handling of CaseStmts where the canonical VarDecl isn't in the first pattern in VarDeclUsageChecker
  • Avoid walking a few more expression nodes in WalkToVarDecls
  • Ensure we set the naming pattern in markInvalid to avoid double-type-checking

Avoid walking TapExprs, SingleValueStmtExprs, and key paths. The latter
is important since they can contain invalid VarDecls that will no
longer be visited by the ASTWalker after key path resolution, so we
don't want to create case body vars for them.
We don't wire up the parent variables until after type-checking, and
`recordInferredSwitchCasePatternVars` already handles joining the
pattern types, so we can remove this.
We want to call the `bodyEmitter`, since that has the extra logic
necessary to handle `do-catch` statements. Previously this didn't
cause any issues since `hasCaseBodyVariables` would have always been
true for parsed `do-catch`s, but I'm planning on changing that.
We don't need to store a `MutableArrayRef`.
We don't really care about the distinction between empty and nil here.
Rather than waiting until type-checking, we can set the parents
immediately when we create the CaseStmt. This requires fixing up
NamingPatternRequest to look at the recursive parent statement as
now the VarDecl may have a variable parent.
This allows us to re-use the same logic to create the case body
variables.
…tyMismatch`

Now that we wire up the parents up-front, this no longer needs to
set the parents. As such, remove the logic and rename to reflect the
fact that it now just diagnoses mutability mismatches.
`findParentPatternCaseStmtAndPattern` does more work than is necessary
for this, we just want the parent of the canonical var.
Add the extra logic to `VarDecl::getParentPattern` necessary to
handle fallthrough and case body variables instead. This also changes
the behavior for case body vars - previously we would return the first
pattern in the CaseStmt, but that's not necessarily correct. Instead,
return the first pattern that actually binds the variable.
We don't want to just check the first pattern, we're interested in
the first pattern that binds the given variable. That can be determined
by checking if it's canonical or not.
This normally gets populated by successful type-checking, we still want
to populate it if we fail though to avoid attempting to type-check the
parent statement again.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please test source compatibility

@hamishknight
Copy link
Contributor Author

@swift-ci please SourceKit stress test

@hamishknight hamishknight changed the title A few CaseStmt pattern cleanups + fixes A few pattern cleanups + fixes Sep 9, 2025
@hamishknight hamishknight merged commit 872176b into swiftlang:main Sep 10, 2025
6 of 8 checks passed
@hamishknight hamishknight deleted the case-and-pat branch September 10, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants