Skip to content

Conversation

@RikkiGibson
Copy link
Member

Closes #79489
@CyrusNajmabadi PTAL

CyrusNajmabadi
CyrusNajmabadi previously approved these changes Oct 26, 2025
@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 27, 2025

    private void NoteDeclaredPatternVariables(BoundPattern pattern)

Instead of having a helper that must know how to dive deep into patterns and rely on containing nodes to call it, can we override appropriate Visit... methods for various bound patterns and rely on regular visit process to record information for them? #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs:70 in e745150. [](commit_id = e745150, deletion_comment = False)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 27, 2025

Done with review pass (commit 2), tests were not looked at. #Closed

@RikkiGibson
Copy link
Member Author

can we override appropriate Visit... methods for various bound patterns and rely on regular visit process to record information for them?

I don't think this could be solved, for example, by only overriding Visit for the specific BoundPattern kinds, which directly contain variables. Instead you would have to declare all the Visit methods for all the pattern subtypes, and write the code to traverse the parts of the patterns which could contain variables. This is because the base type Visit methods already don't do this, to the best of my understanding. I think this approach, as well as the approach in iteration 2, would also end up requiring re-implementing "safe" traversal of binary patterns (because of the need to find variables in expr is > 0 and var variable, for example.)

A few alternatives I considered:

  1. Declare an inner class PatternVisitor : BoundTreeWalkerWithStackGuardWithoutRecursionOnTheLeftOfBinaryOperator, which we call into to visit patterns, which adds the variables it finds to VariablesDeclaredWalker._variablesDeclared. This would assume that we can't enter/exit the region in the middle of a pattern.
  2. Change the base type class VariablesDeclaredWalker : AbstractRegionDataFlowPass and override some virtual method on DefiniteAssignment to allow reusing the definite assignment logic which traverses patterns in order to mark them as assigned.

It's possible I missed something that would make fixing this easier to accomplish. Let me know if you have any further thoughts.

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 27, 2025

I don't think this could be solved, for example, by only overriding Visit for the specific BoundPattern kinds, which directly contain variables. Instead you would have to declare all the Visit methods for all the pattern subtypes, and write the code to traverse the parts of the patterns which could contain variables.

I guess it would be fine for NoteDeclaredPatternVariables to implement its own traversal, but let's do it similar to the way DefiniteAssignmentPass.VisitPattern does it. I.e. it should throw for default case. #Closed

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Oct 27, 2025

        if (IsInside)

Are there situations when we need to adjust the IsInside state while traversing the pattern? #Closed


Refers to: src/Compilers/CSharp/Portable/FlowAnalysis/VariablesDeclaredWalker.cs:72 in e745150. [](commit_id = e745150, deletion_comment = False)

@RikkiGibson RikkiGibson marked this pull request as draft November 1, 2025 20:08
@RikkiGibson
Copy link
Member Author

Are there situations when we need to adjust the IsInside state while traversing the pattern?

It looks like it is possible to AnalyzeDataFlow with the Expression inside a pattern which takes constant values. I think only in error cases can you declare variables in such expressions, but, I added test RegionWithinSubpattern03 to make sure the API indicates that the variable declaration is present.

@RikkiGibson RikkiGibson marked this pull request as ready for review November 2, 2025 17:44
@AlekseyTs
Copy link
Contributor

Done with review pass (commit 9)

@RikkiGibson RikkiGibson requested a review from AlekseyTs November 3, 2025 20:55
private void NoteDeclaredPatternVariables(BoundPattern pattern)
{
if (IsInside)
switch (pattern)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This walk is roughly based on DefiniteAssignment.VisitPattern.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (commit 10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Extract Method" handles variables defined from pattern matching poorly

3 participants