[pysrc2cpg] Lower match pattern bindings into proper AST assignments#5918
Open
allsmog wants to merge 1 commit into
Open
[pysrc2cpg] Lower match pattern bindings into proper AST assignments#5918allsmog wants to merge 1 commit into
allsmog wants to merge 1 commit into
Conversation
Replaces the string-only pattern representation in match/case blocks with proper AST nodes that encode destructuring semantics, enabling data flow tracking through pattern-bound variables. For each match pattern type, generate assignment nodes using the same index access and assignment primitives as tuple unpacking: - MatchSequence [a, b]: a = subject[0], b = subject[1] - MatchAs (catch-all): x = subject - MatchAs (alias): recurse + whole = subject - MatchMapping: name = subject[key] - MatchClass: positional index + keyword field access - MatchOr: process first alternative (all bind same names) - MatchStar: rest = subject (simplified flow) - Complex subjects: temp variable to avoid re-evaluation JumpTarget nodes are preserved for CfgCreator compatibility. Nested patterns use temp variables like tuple unpacking does.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to the discussion in #5910 where the reviewer correctly noted that bare pattern variables as statements don't encode the destructuring semantics.
This PR lowers match patterns into assignment nodes using the same index access and assignment primitives that tuple unpacking already uses (
createIndexAccess,createAssignmentToIdentifier,getUnusedName).Lowering examples
case [a, b]:a = subject[0],b = subject[1]case x:(catch-all)x = subjectcase [a, b] as whole:whole = subjectcase {"key": val}:val = subject[key]case Point(x=a, y=b):a = subject.x,b = subject.ycase 42:/case _:JumpTarget nodes are preserved for CfgCreator compatibility. Nested patterns use temp variables following the same convention as nested tuple unpacking in
createValueToTargetsDecomposition.Test plan