You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Source location of match sub-expressions are incorrect #98762
def f(x):
match x:
case a,b:
return 1
import dis
from pprint import pprint as pp
def pos(p):
return (p.lineno, p.end_lineno, p.col_offset, p.end_col_offset)
pp([(pos(x.positions), x.opname, x.argval) for x in dis.get_instructions(f)])
changed the title [-]Source location of sequence match assignments are incorrect[/-][+]Source location of match sub-expressions are incorrect[/+]on Oct 27, 2022
The issue with name stores is pretty tricky, and stems from the fact that names are only bound once the entire pattern has matched. Fixing the locations for name stores in "normal" patterns won't be too hard (just keep the original node nearby and use that location for the final store).
However, or-patterns pose a challenge. For example, what should the location be when storing a and b?
match ...:
case [True, a, b] | [False, b, a]:
...
There's only one store instruction for each (at the very end), so I still think "the whole pattern" makes sense.
Activity
iritkatriel commentedon Oct 27, 2022
FYI @nedbat .
[-]Source location of sequence match assignments are incorrect[/-][+]Source location of match sub-expressions are incorrect[/+]pythongh-98762: Fix locations of match sub-expressions
gh-98762: Fix locations of match sub-patterns (GH-98775)
brandtbucher commentedon Nov 18, 2022
The issue with name stores is pretty tricky, and stems from the fact that names are only bound once the entire pattern has matched. Fixing the locations for name stores in "normal" patterns won't be too hard (just keep the original node nearby and use that location for the final store).
However, or-patterns pose a challenge. For example, what should the location be when storing
aandb?There's only one store instruction for each (at the very end), so I still think "the whole pattern" makes sense.