Skip to content

Conversation

@RikkiGibson
Copy link
Member

Closes #79489
@CyrusNajmabadi PTAL

{
switch (pattern)
{
case BoundRecursivePattern r:
Copy link
Contributor

Choose a reason for hiding this comment

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

BoundRecursivePattern

It looks like BoundRecursivePattern is a BoundObjectPattern and whatever the case below is doing should be done for it as well.

{
switch (pattern)
{
case BoundRecursivePattern r:
Copy link
Contributor

Choose a reason for hiding this comment

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

case

Are there other "complex" pattern forms that should be tread specially here? Like: BoundListPattern, BoundSlicePattern, etc.

@AlekseyTs
Copy link
Contributor

    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?


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

@AlekseyTs
Copy link
Contributor

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

@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

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.

@AlekseyTs
Copy link
Contributor

        if (IsInside)

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


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

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