Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 5 additions & 28 deletions mypyc/irbuild/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ class MatchVisitor(TraverserVisitor):
subject: Value
match: MatchStmt

as_pattern: AsPattern | None = None

def __init__(self, builder: IRBuilder, match_node: MatchStmt) -> None:
self.builder = builder

Expand Down Expand Up @@ -103,8 +101,6 @@ def visit_value_pattern(self, pattern: ValuePattern) -> None:

cond = self.builder.binary_op(self.subject, value, "==", pattern.expr.line)

self.bind_as_pattern(value)

self.builder.add_bool_branch(cond, self.code_block, self.next_block)

def visit_or_pattern(self, pattern: OrPattern) -> None:
Expand All @@ -115,11 +111,7 @@ def visit_or_pattern(self, pattern: OrPattern) -> None:
self.code_block = BasicBlock()
self.next_block = BasicBlock()

# Hack to ensure the as pattern is bound to each pattern in the
# "or" pattern, but not every subpattern
backup = self.as_pattern
p.accept(self)
self.as_pattern = backup

self.builder.activate_block(self.code_block)
self.builder.goto(code_block)
Expand All @@ -144,8 +136,6 @@ def visit_class_pattern(self, pattern: ClassPattern) -> None:

self.builder.add_bool_branch(cond, self.code_block, self.next_block)

self.bind_as_pattern(self.subject, new_block=True)

if pattern.positionals:
if pattern.class_ref.fullname in MATCHABLE_BUILTINS:
self.builder.activate_block(self.code_block)
Expand Down Expand Up @@ -182,12 +172,13 @@ def visit_class_pattern(self, pattern: ClassPattern) -> None:

def visit_as_pattern(self, pattern: AsPattern) -> None:
if pattern.pattern:
old_pattern = self.as_pattern
self.as_pattern = pattern
pattern.pattern.accept(self)
self.as_pattern = old_pattern

elif pattern.name:
# Bind the name to the subject only once the subpattern has matched.
self.builder.activate_block(self.code_block)
self.code_block = BasicBlock()

if pattern.name:
target = self.builder.get_assignment_target(pattern.name)

self.builder.assign(target, self.subject, pattern.line)
Expand Down Expand Up @@ -306,20 +297,6 @@ def visit_sequence_pattern(self, seq_pattern: SequencePattern) -> None:

self.builder.goto(self.code_block)

def bind_as_pattern(self, value: Value, new_block: bool = False) -> None:
if self.as_pattern and self.as_pattern.pattern and self.as_pattern.name:
if new_block:
self.builder.activate_block(self.code_block)
self.code_block = BasicBlock()

target = self.builder.get_assignment_target(self.as_pattern.name)
self.builder.assign(target, value, self.as_pattern.pattern.line)

self.as_pattern = None

if new_block:
self.builder.goto(self.code_block)

@contextmanager
def enter_subpattern(self, subject: Value) -> Generator[None]:
old_subject = self.subject
Expand Down
116 changes: 57 additions & 59 deletions mypyc/test-data/irbuild-match.test
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,21 @@ def f():
r7, r8 :: object
L0:
r0 = int_eq 246, 246
if r0 goto L1 else goto L3 :: bool
L1:
r1 = object 123
x = r1
if r0 goto L1 else goto L2 :: bool
L1:
L2:
r2 = builtins :: module
r3 = 'print'
r4 = CPyObject_GetAttr(r2, r3)
r5 = [x]
r6 = load_address r5
r7 = PyObject_Vectorcall(r4, r6, 1, 0)
keep_alive x
goto L3
L2:
goto L4
L3:
L4:
r8 = box(None, 1)
return r8

Expand All @@ -539,44 +540,41 @@ def f():
print(x)
[out]
def f():
r0 :: bit
r1, x :: object
r2 :: bit
r3, r4 :: object
r5 :: str
r6 :: object
r7 :: object[1]
r8 :: object_ptr
r9, r10 :: object
r0, r1 :: bit
r2, x, r3 :: object
r4 :: str
r5 :: object
r6 :: object[1]
r7 :: object_ptr
r8, r9 :: object
L0:
r0 = int_eq 2, 2
r1 = object 1
x = r1
if r0 goto L1 else goto L2 :: bool
L1:
goto L5
L2:
r2 = int_eq 2, 4
r3 = object 2
x = r3
if r2 goto L3 else goto L4 :: bool
r1 = int_eq 2, 4
if r1 goto L3 else goto L4 :: bool
L3:
goto L5
L4:
goto L6
L5:
r4 = builtins :: module
r5 = 'print'
r6 = CPyObject_GetAttr(r4, r5)
r7 = [x]
r8 = load_address r7
r9 = PyObject_Vectorcall(r6, r8, 1, 0)
keep_alive x
goto L7
L5:
r2 = object 1
x = r2
L6:
r3 = builtins :: module
r4 = 'print'
r5 = CPyObject_GetAttr(r3, r4)
r6 = [x]
r7 = load_address r6
r8 = PyObject_Vectorcall(r5, r7, 1, 0)
keep_alive x
goto L8
L7:
r10 = box(None, 1)
return r10
L8:
r9 = box(None, 1)
return r9

[case testMatchAsPatternOnClassPattern_python3_10]
def f():
Expand Down Expand Up @@ -897,17 +895,17 @@ def f(x):
r1 :: i32
r2 :: bit
r3 :: bool
r4, y :: __main__.C
r5 :: str
r6, r7, r8 :: object
r9 :: i32
r10 :: bit
r11 :: bool
r12 :: str
r13, r14, r15 :: object
r16 :: i32
r17 :: bit
r18 :: bool
r4 :: str
r5, r6, r7 :: object
r8 :: i32
r9 :: bit
r10 :: bool
r11 :: str
r12, r13, r14 :: object
r15 :: i32
r16 :: bit
r17 :: bool
r18, y :: __main__.C
r19 :: str
r20 :: object
r21 :: str
Expand All @@ -922,26 +920,26 @@ L0:
r3 = truncate r1: i32 to builtins.bool
if r3 goto L1 else goto L5 :: bool
L1:
r4 = cast(__main__.C, x)
y = r4
r4 = 'a'
r5 = CPyObject_GetAttr(x, r4)
r6 = object 1
r7 = PyObject_RichCompare(r5, r6, 2)
r8 = PyObject_IsTrue(r7)
r9 = r8 >= 0 :: signed
r10 = truncate r8: i32 to builtins.bool
if r10 goto L2 else goto L5 :: bool
L2:
r5 = 'a'
r6 = CPyObject_GetAttr(x, r5)
r7 = object 1
r8 = PyObject_RichCompare(r6, r7, 2)
r9 = PyObject_IsTrue(r8)
r10 = r9 >= 0 :: signed
r11 = truncate r9: i32 to builtins.bool
if r11 goto L3 else goto L5 :: bool
r11 = 'b'
r12 = CPyObject_GetAttr(x, r11)
r13 = object 2
r14 = PyObject_RichCompare(r12, r13, 2)
r15 = PyObject_IsTrue(r14)
r16 = r15 >= 0 :: signed
r17 = truncate r15: i32 to builtins.bool
if r17 goto L3 else goto L5 :: bool
L3:
r12 = 'b'
r13 = CPyObject_GetAttr(x, r12)
r14 = object 2
r15 = PyObject_RichCompare(r13, r14, 2)
r16 = PyObject_IsTrue(r15)
r17 = r16 >= 0 :: signed
r18 = truncate r16: i32 to builtins.bool
if r18 goto L4 else goto L5 :: bool
r18 = cast(__main__.C, x)
y = r18
L4:
r19 = 'matched'
r20 = builtins :: module
Expand Down
53 changes: 53 additions & 0 deletions mypyc/test-data/run-match.test
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,56 @@ for fn in (f, g):
[out]
RuntimeError boom
RuntimeError boom

[case testMatchAsPatternBindsSubject_python3_10]
from typing import Any

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

def mapping(x: Any) -> object:
match x:
case {"k": a} as z:
return z
return None

def nested(x: Any) -> object:
match x:
case [(1 as a), *_] as z:
return (a, z)
return None

def value(x: Any) -> object:
match x:
case 1.0 as z:
return z
return None

def or_pattern(x: Any) -> object:
match x:
case ([1, _] | {"k": 1} | None | 5) as z:
return ("or", z)
return None

[file driver.py]
from native import seq, mapping, nested, value, or_pattern

assert seq([1, 2]) == [1, 2]
assert seq((3, 4)) == (3, 4)
assert seq([1]) is None
assert mapping({"k": 1}) == {"k": 1}
assert mapping({}) is None
assert nested([1, 2]) == (1, [1, 2])
assert nested([2, 2]) is None
assert type(value(1)) is int
assert value(True) is True
assert type(value(1.0)) is float
assert value(2) is None
assert or_pattern([1, 9]) == ("or", [1, 9])
assert or_pattern({"k": 1}) == ("or", {"k": 1})
assert or_pattern(None) == ("or", None)
assert type(or_pattern(5.0)[1]) is float
assert or_pattern([2, 2]) is None
Loading