-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix VariablesDeclaredWalker with recursive patterns #80910
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
RikkiGibson
merged 11 commits into
dotnet:main
from
RikkiGibson:extract-method-pattern-var
Nov 4, 2025
+743
−23
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d2edc44
Fix VariablesDeclaredWalker with recursive patterns
RikkiGibson e745150
Add extract method tests
RikkiGibson cf10803
Implement visit pass, add few more tests
RikkiGibson 4fc8ba3
add tests
RikkiGibson 412c5fc
fix conditional state handling
RikkiGibson 38210e5
fix formatting
RikkiGibson 595fb20
Remove unnecessary nesting
RikkiGibson 93e941f
Merge remote-tracking branch 'upstream/main' into extract-method-patt…
RikkiGibson ad398ca
fix nullable warning
RikkiGibson cc490dc
Fix test
RikkiGibson 913b6ee
Add a comment per offline feedback
RikkiGibson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,13 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.Collections; | ||
| using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.PooledObjects; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Roslyn.Utilities; | ||
|
|
||
|
|
@@ -45,13 +44,13 @@ internal VariablesDeclaredWalker(CSharpCompilation compilation, Symbol member, B | |
| protected override void Free() | ||
| { | ||
| base.Free(); | ||
| _variablesDeclared = null; | ||
| _variablesDeclared = null!; | ||
| } | ||
|
|
||
| public override void VisitPattern(BoundPattern pattern) | ||
| { | ||
| base.VisitPattern(pattern); | ||
| NoteDeclaredPatternVariables(pattern); | ||
| base.VisitPattern(pattern); | ||
| } | ||
|
|
||
| protected override void VisitSwitchSection(BoundSwitchSection node, bool isLastSection) | ||
|
|
@@ -69,26 +68,104 @@ protected override void VisitSwitchSection(BoundSwitchSection node, bool isLastS | |
| /// </summary> | ||
| private void NoteDeclaredPatternVariables(BoundPattern pattern) | ||
| { | ||
| if (IsInside) | ||
| switch (pattern) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This walk is roughly based on DefiniteAssignment.VisitPattern. |
||
| { | ||
| switch (pattern) | ||
| { | ||
| case BoundObjectPattern p: | ||
| case BoundDeclarationPattern declarationPattern: | ||
| noteOneVariable(declarationPattern.Variable); | ||
| break; | ||
|
|
||
| case BoundRecursivePattern recursivePattern: | ||
| foreach (var subpattern in recursivePattern.Deconstruction.NullToEmpty()) | ||
| NoteDeclaredPatternVariables(subpattern.Pattern); | ||
|
|
||
| foreach (var subpattern in recursivePattern.Properties.NullToEmpty()) | ||
| NoteDeclaredPatternVariables(subpattern.Pattern); | ||
|
|
||
| noteOneVariable(recursivePattern.Variable); | ||
| break; | ||
|
|
||
| case BoundITuplePattern ituplePattern: | ||
| foreach (var subpattern in ituplePattern.Subpatterns) | ||
| NoteDeclaredPatternVariables(subpattern.Pattern); | ||
|
|
||
| break; | ||
|
|
||
| case BoundListPattern listPattern: | ||
| foreach (var elementPattern in listPattern.Subpatterns) | ||
| NoteDeclaredPatternVariables(elementPattern); | ||
|
|
||
| noteOneVariable(listPattern.Variable); | ||
| break; | ||
|
|
||
| case BoundConstantPattern constantPattern: | ||
| // It is possible for the region to be the expression within a pattern. | ||
| VisitRvalue(constantPattern.Value); | ||
| break; | ||
|
|
||
| case BoundRelationalPattern relationalPattern: | ||
| // It is possible for the region to be the expression within a pattern. | ||
| VisitRvalue(relationalPattern.Value); | ||
| break; | ||
|
|
||
| case BoundNegatedPattern negatedPattern: | ||
| NoteDeclaredPatternVariables(negatedPattern.Negated); | ||
| break; | ||
|
|
||
| case BoundSlicePattern slicePattern: | ||
| if (slicePattern.Pattern != null) | ||
| NoteDeclaredPatternVariables(slicePattern.Pattern); | ||
|
|
||
| break; | ||
|
|
||
| case BoundDiscardPattern or BoundTypePattern: | ||
| // Does not contain variables or expressions. Nothing to visit. | ||
| break; | ||
|
|
||
| case BoundBinaryPattern: | ||
| { | ||
| var binaryPattern = (BoundBinaryPattern)pattern; | ||
| if (binaryPattern.Left is not BoundBinaryPattern) | ||
| { | ||
| // The variable may be null if it is a discard designation `_`. | ||
| if (p.Variable?.Kind == SymbolKind.Local) | ||
| { | ||
| // Because this API only returns local symbols and parameters, | ||
| // we exclude pattern variables that have become fields in scripts. | ||
| _variablesDeclared.Add(p.Variable); | ||
| } | ||
| NoteDeclaredPatternVariables(binaryPattern.Left); | ||
| NoteDeclaredPatternVariables(binaryPattern.Right); | ||
| break; | ||
| } | ||
|
|
||
| // Users (such as ourselves) can have many, many nested binary patterns. To avoid crashing, do left recursion manually. | ||
| var stack = ArrayBuilder<BoundBinaryPattern>.GetInstance(); | ||
| do | ||
| { | ||
| stack.Push(binaryPattern); | ||
| binaryPattern = binaryPattern.Left as BoundBinaryPattern; | ||
| } while (binaryPattern is not null); | ||
|
|
||
| binaryPattern = stack.Pop(); | ||
| NoteDeclaredPatternVariables(binaryPattern.Left); | ||
|
|
||
| do | ||
| { | ||
| NoteDeclaredPatternVariables(binaryPattern.Right); | ||
| } while (stack.TryPop(out binaryPattern)); | ||
|
|
||
| stack.Free(); | ||
| break; | ||
| } | ||
| default: | ||
| throw ExceptionUtilities.UnexpectedValue(pattern.Kind); | ||
| } | ||
|
|
||
| void noteOneVariable(Symbol? symbol) | ||
| { | ||
| if (IsInside && symbol?.Kind == SymbolKind.Local) | ||
| { | ||
| // Because this API only returns local symbols and parameters, | ||
| // we exclude pattern variables that have become fields in scripts. | ||
| _variablesDeclared.Add(symbol); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override BoundNode VisitLocalDeclaration(BoundLocalDeclaration node) | ||
| public override BoundNode? VisitLocalDeclaration(BoundLocalDeclaration node) | ||
| { | ||
| if (IsInside) | ||
| { | ||
|
|
@@ -98,7 +175,7 @@ public override BoundNode VisitLocalDeclaration(BoundLocalDeclaration node) | |
| return base.VisitLocalDeclaration(node); | ||
| } | ||
|
|
||
| public override BoundNode VisitLambda(BoundLambda node) | ||
| public override BoundNode? VisitLambda(BoundLambda node) | ||
| { | ||
| if (IsInside && !node.WasCompilerGenerated) | ||
| { | ||
|
|
@@ -111,7 +188,7 @@ public override BoundNode VisitLambda(BoundLambda node) | |
| return base.VisitLambda(node); | ||
| } | ||
|
|
||
| public override BoundNode VisitLocalFunctionStatement(BoundLocalFunctionStatement node) | ||
| public override BoundNode? VisitLocalFunctionStatement(BoundLocalFunctionStatement node) | ||
| { | ||
| if (IsInside && !node.WasCompilerGenerated) | ||
| { | ||
|
|
@@ -142,7 +219,7 @@ public override void VisitForEachIterationVariables(BoundForEachStatement node) | |
| } | ||
| } | ||
|
|
||
| public override BoundNode VisitCatchBlock(BoundCatchBlock catchBlock) | ||
| public override BoundNode? VisitCatchBlock(BoundCatchBlock catchBlock) | ||
| { | ||
| if (IsInside) | ||
| { | ||
|
|
@@ -159,11 +236,11 @@ public override BoundNode VisitCatchBlock(BoundCatchBlock catchBlock) | |
| return null; | ||
| } | ||
|
|
||
| public override BoundNode VisitQueryClause(BoundQueryClause node) | ||
| public override BoundNode? VisitQueryClause(BoundQueryClause node) | ||
| { | ||
| if (IsInside) | ||
| { | ||
| if ((object)node.DefinedSymbol != null) | ||
| if ((object?)node.DefinedSymbol != null) | ||
| { | ||
| _variablesDeclared.Add(node.DefinedSymbol); | ||
| } | ||
|
|
@@ -177,7 +254,7 @@ protected override void VisitLvalue(BoundLocal node) | |
| VisitLocal(node); | ||
| } | ||
|
|
||
| public override BoundNode VisitLocal(BoundLocal node) | ||
| public override BoundNode? VisitLocal(BoundLocal node) | ||
| { | ||
| if (IsInside && node.DeclarationKind != BoundLocalDeclarationKind.None) | ||
| { | ||
|
|
@@ -187,4 +264,4 @@ public override BoundNode VisitLocal(BoundLocal node) | |
| return null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.