Skip to content

[mypyc] Fix binding of "as" patterns in match statements - #22572

Open
andriisem wants to merge 4 commits into
python:masterfrom
andriisem:mypyc-match-as-capture
Open

andriisem wants to merge 4 commits into
python:masterfrom
andriisem:mypyc-match-as-capture

Conversation

@andriisem

Copy link
Copy Markdown

compiled as patterns in match statements bind the name wrong

def f(x: object) -> object:
    match x:
        case [a, b] as z:
            return z
    return None

f([1, 2])  # UnboundLocalError, expected [1, 2]
  • sequence or mapping subpattern: name is never bound
  • case [1, *_] as z: z is the item 1, not the subject
  • case 1.0 as z: z is the literal, so f(1) returns 1.0

only value and class patterns bound the name, and the value pattern used the literal. now visit_as_pattern binds the subject after the subpattern matches. this removes bind_as_pattern and the workaround in visit_or_pattern

The name in `case <pattern> as z` was only bound by value and class
patterns, via a pending `as_pattern` that the subpattern visitor had to
pick up. Sequence and mapping patterns never bound it, so `z` raised
UnboundLocalError. Value patterns bound the pattern value instead of
the subject, so `case 1.0 as z` gave `z == 1.0` for subject `1` or `True`.

Bind the name in visit_as_pattern itself, to the subject, in the block
reached when the subpattern matched. This also removes the or-pattern
workaround.
@hauntsaninja

Copy link
Copy Markdown
Collaborator

Thank you! I think you have issues with:

def rebind(x: Any) -> object:
    match x:
        case list([x]) as z:
            return z
    return None

rebind([0])

def floatint(x: object) -> object:
    match x:
        case 1 as z:
            return z
    return None

floatint(1.0)

When the subject of a match statement is a local variable, the visitor
uses that variable's register directly. A capture pattern or a walrus
in a guard that rebinds the variable therefore changes what the rest
of the match sees, e.g. `case [x, y]` reads `x[1]` from the new `x`,
and `case list([x]) as z` binds `z` to the new `x`.
When the subject was a local variable, the match visitor used the
variable's register directly, so a capture pattern or a walrus in a
guard that rebinds the variable changed what the rest of the match
saw. Collect the variables assigned in the patterns and guards, and
copy the subject to a new register if it's one of them, like for loops
do for their iterable.
It's now also used for the subject of match statements, so give it a
name and docstring that aren't specific to for loops.
@andriisem
andriisem force-pushed the mypyc-match-as-capture branch from af6f98b to 967fcda Compare November 30, 2026 13:25
@andriisem

Copy link
Copy Markdown
Author

@hauntsaninja
thanks for catching these

rebind fixed. the visitor used the subject variable's register directly so any pattern or guard that rebinds it broke the rest of the match (also case [x, y] and walrus in guard were broken on master). now the subject gets copied when a pattern or guard assigns to it, reusing the helper from the for loop fix (#22137). added tests

floatint I think it's out of scope PR, wdyt?

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