Skip to content

Commit d1ced9b

Browse files
committed
feat: close the documented limits on patterns, nonlocal, local classes and while conditions
1 parent 851535f commit d1ced9b

8 files changed

Lines changed: 150 additions & 29 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ synthetic local, set literals, chained assignments, assignments to `global` name
169169
lambdas and nested function definitions, analysed as functions of their own whose
170170
captured variables are implicit parameters so taint flows through closures like through
171171
any call, `del`, loop `else` clauses, and `match` over literal, singleton, capture, wildcard,
172-
or, sequence, mapping and keyword class patterns with guards, laid out as an `if` chain
173-
over length, index, membership, attribute and `isinstance` tests. Not yet: positional
174-
class patterns, `nonlocal` assignments, classes defined inside functions, and a
175-
conditional expression or comprehension inside a `while` condition. Blocks inside a `try`
172+
or, sequence, mapping and class patterns with guards, laid out as an `if` chain over
173+
length, index, membership, attribute and `isinstance` tests, positional class patterns
174+
using the class's `__match_args__` or a dataclass's field order when the class is in the
175+
module; `nonlocal` assignments, whose writes stay inside the nested function; classes
176+
defined inside functions, whose methods are analysed as nested functions; and control flow
177+
inside a `while` condition, recomputed every iteration. Left: a function using syntax
178+
the frontend rejects is reported as a `syntax-error` for its file. Blocks inside a `try`
176179
body carry exception edges to the handlers; `finally` is modelled on the normal path only. Methods of module-level classes are analysed like functions; other module-level
177180
code is skipped. An import inside a function is shown where it runs, as an `import`
178181
instruction naming the module, the bound name and the canonical symbol. A function using syntax outside this subset is reported by `--check` as

‎src/coretrace_python/cfg/builder.py‎

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Mapping
56
from dataclasses import fields, is_dataclass, replace
67
from typing import Any, ClassVar
78

@@ -31,7 +32,10 @@ def __init__(self, block_id: BlockId) -> None:
3132

3233

3334
class _Builder:
34-
def __init__(self, function: nodes.Function) -> None:
35+
def __init__(
36+
self, function: nodes.Function, match_args: Mapping[str, tuple[str, ...]] | None = None
37+
) -> None:
38+
self.match_args = dict(match_args or {})
3539
self.function = function
3640
self.blocks: dict[BlockId, BasicBlock] = {}
3741
self.counters: dict[str, int] = {}
@@ -59,12 +63,28 @@ def desugared(self, node: nodes.Statement, block: _Open) -> tuple[nodes.Statemen
5963

6064
pending: list[nodes.Statement] = []
6165
if isinstance(node, nodes.While):
62-
if _has_control_flow(node.condition):
63-
raise CFGError(
64-
f"{node.span.display()}: conditional expressions and comprehensions in a "
65-
"loop condition are not supported yet"
66-
)
67-
return node, block
66+
if not _has_control_flow(node.condition):
67+
return node, block
68+
# ``while <control flow>:`` recomputes its condition every iteration: it
69+
# becomes ``while True`` with the condition laid out at the top of the body
70+
# and a ``break``; the ``else`` clause runs before that ``break``.
71+
inner: list[nodes.Statement] = []
72+
condition = self.hoist(node.condition, inner)
73+
stop = nodes.If(
74+
nodes.UnaryOp("not", condition, node.span),
75+
(*node.orelse, nodes.Break(node.span)),
76+
(),
77+
node.span,
78+
)
79+
return (
80+
replace(
81+
node,
82+
condition=nodes.Constant(True, node.span),
83+
body=(*inner, stop, *node.body),
84+
orelse=(),
85+
),
86+
block,
87+
)
6888
if isinstance(node, nodes.Assign | nodes.AugAssign):
6989
node = replace(node, target=self.hoist(node.target, pending), value=self.hoist(node.value, pending))
7090
elif isinstance(node, nodes.ExpressionStatement):
@@ -430,16 +450,17 @@ def class_pattern(
430450
"""``Cls(name=pattern)``: an instance of the class whose attributes match."""
431451

432452
span = node.span
433-
if node.patterns:
434-
raise CFGError(
435-
f"{span.display()}: positional class patterns need the class's __match_args__ "
436-
"and are not supported yet"
437-
)
438453
conditions: list[nodes.Expression] = [
439454
nodes.Call(nodes.Name("isinstance", span), (subject, node.cls), (), span)
440455
]
441456
bindings: list[nodes.Statement] = []
442-
for name, sub in zip(node.keyword_names, node.keyword_patterns, strict=True):
457+
# Positional sub-patterns match the attributes ``__match_args__`` names, known
458+
# for the module's classes; an unknown class gets a conservative position.
459+
known = self.match_args.get(node.cls.identifier, ()) if isinstance(node.cls, nodes.Name) else ()
460+
positional = [
461+
(known[i] if i < len(known) else f"_match_arg_{i}", sub) for i, sub in enumerate(node.patterns)
462+
]
463+
for name, sub in (*positional, *zip(node.keyword_names, node.keyword_patterns, strict=True)):
443464
condition, inner = self.pattern(sub, nodes.Attribute(subject, name, span))
444465
conditions.append(condition)
445466
bindings.extend(inner)
@@ -521,13 +542,48 @@ def _rename_target(target: nodes.Target, renames: dict[str, str]) -> nodes.Targe
521542
return renamed
522543

523544

524-
def build_cfg(function: nodes.Function) -> CFG:
525-
return _Builder(function).build()
545+
def build_cfg(function: nodes.Function, match_args: Mapping[str, tuple[str, ...]] | None = None) -> CFG:
546+
return _Builder(function, match_args).build()
547+
548+
549+
def match_args_of(module: nodes.Module) -> dict[str, tuple[str, ...]]:
550+
"""``__match_args__`` of the module's classes: explicit, or the field order of a
551+
dataclass (bare annotated declarations and assignments, in order)."""
552+
553+
found: dict[str, tuple[str, ...]] = {}
554+
for statement in module.body:
555+
if not isinstance(statement, nodes.Class):
556+
continue
557+
explicit = None
558+
fields: list[str] = []
559+
for member in statement.body:
560+
if isinstance(member, nodes.Assign) and isinstance(member.target, nodes.Name):
561+
if member.target.identifier == "__match_args__" and isinstance(member.value, nodes.Tuple | nodes.List):
562+
explicit = tuple(
563+
e.value for e in member.value.elements if isinstance(e, nodes.Constant) and isinstance(e.value, str)
564+
)
565+
elif not member.target.identifier.startswith("_"):
566+
fields.append(member.target.identifier)
567+
elif isinstance(member, nodes.Declaration):
568+
fields.append(member.name)
569+
if explicit is not None:
570+
found[statement.name] = explicit
571+
elif any(_is_dataclass(d) for d in statement.decorators):
572+
found[statement.name] = tuple(fields)
573+
return found
574+
575+
576+
def _is_dataclass(decorator: nodes.Expression) -> bool:
577+
if isinstance(decorator, nodes.Call):
578+
decorator = decorator.callee
579+
if isinstance(decorator, nodes.Name):
580+
return decorator.identifier == "dataclass"
581+
return isinstance(decorator, nodes.Attribute) and decorator.name == "dataclass"
526582

527583

528584
class CFGAnalysis(FunctionAnalysis[CFG]):
529585
name: ClassVar[str] = "cfg.function"
530586

531587
@classmethod
532588
def compute(cls, ctx: AnalysisContext, function: nodes.Function) -> CFG:
533-
return build_cfg(function)
589+
return build_cfg(function, match_args_of(ctx.module))

‎src/coretrace_python/frontend/ast_adapter.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,16 @@ def statement(self, node: ast.stmt) -> nodes.Statement:
217217
if isinstance(node, ast.Assign):
218218
return nodes.Assign(self.target(node.targets[0]), self.expression(node.value), span)
219219
if isinstance(node, ast.AnnAssign):
220-
# The annotation never affects behaviour; a bare declaration does nothing.
220+
# The annotation never affects behaviour; a bare declaration binds nothing
221+
# but names a dataclass field or an annotated local.
221222
if node.value is None:
222-
return nodes.Pass(span)
223+
annotation: nodes.Expression | None = None
224+
try:
225+
annotation = self.expression(node.annotation)
226+
except HIRBuildError:
227+
annotation = None
228+
name = node.target.id if isinstance(node.target, ast.Name) else None
229+
return nodes.Declaration(name, annotation, span) if name is not None else nodes.Pass(span)
223230
return nodes.Assign(self.target(node.target), self.expression(node.value), span)
224231
if isinstance(node, ast.AugAssign):
225232
operator = _BINARY_OPERATORS.get(type(node.op))

‎src/coretrace_python/hir/nodes.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ class Delete:
266266
span: SourceSpan
267267

268268

269+
@dataclass(frozen=True, slots=True)
270+
class Declaration:
271+
"""``name: annotation`` without a value: a dataclass field or an annotated local."""
272+
273+
name: str
274+
annotation: Expression | None
275+
span: SourceSpan
276+
277+
269278
@dataclass(frozen=True, slots=True)
270279
class ValuePattern:
271280
"""``case <expression>``: equality with a value."""
@@ -545,6 +554,7 @@ class Class:
545554
| Pass
546555
| Delete
547556
| Match
557+
| Declaration
548558
| Assert
549559
| If
550560
| While

‎src/coretrace_python/ir/lowering.py‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
Instruction,
4747
Jump,
4848
LoadLocal,
49+
MakeClass,
4950
MakeFunction,
5051
ModuleIR,
5152
Raise,
@@ -65,6 +66,7 @@
6566
)
6667
from coretrace_python.semantic import SEMANTIC_ANALYSES
6768
from coretrace_python.semantic.scopes import (
69+
BindingKind,
6870
Resolution,
6971
ResolutionKind,
7072
Scope,
@@ -134,7 +136,10 @@ def expression(self, node: nodes.Expression) -> Value:
134136
if isinstance(node, nodes.Name):
135137
resolution = self.resolve(node.identifier)
136138
if resolution.kind is ResolutionKind.FREE:
137-
# A captured variable is an implicit parameter of the nested function.
139+
# A captured variable is an implicit parameter of the nested function; a
140+
# ``nonlocal`` one lives in a local slot initialised from it.
141+
if node.identifier in self.nonlocals:
142+
return self.emit(LoadLocal(self.new_value(), node.span, node.identifier))
138143
if node.identifier in self.parameters:
139144
return self.parameters[node.identifier]
140145
self.fail(node, "closures are not supported yet")
@@ -223,8 +228,8 @@ def store(self, target: nodes.Target, value: Value) -> None:
223228
if resolution.kind is ResolutionKind.GLOBAL:
224229
self.emit_effect(SetGlobal(None, target.span, target.identifier, value))
225230
return
226-
if resolution.kind is not ResolutionKind.LOCAL:
227-
self.fail(target, "assignment to a nonlocal name is not supported yet")
231+
if resolution.kind is not ResolutionKind.LOCAL and target.identifier not in self.nonlocals:
232+
self.fail(target, "assignment to a free variable that is not declared nonlocal")
228233
self.emit_effect(StoreLocal(None, target.span, target.identifier, value))
229234
elif isinstance(target, nodes.Attribute):
230235
object_value = self.expression(target.value)
@@ -244,6 +249,18 @@ def statement(self, node: nodes.Statement) -> None:
244249
if isinstance(node, nodes.Assign):
245250
self.store(node.target, self.expression(node.value))
246251
return
252+
if isinstance(node, nodes.Declaration):
253+
return
254+
if isinstance(node, nodes.Class):
255+
# A local class is a value bound to its name; bases and decorators run here,
256+
# its methods are analysed as nested functions.
257+
for decorator in node.decorators:
258+
self.expression(decorator)
259+
for base in node.bases:
260+
self.expression(base)
261+
made_class = self.emit(MakeClass(self.new_value(), node.span, node.name))
262+
self.store(nodes.Name(node.name, node.span), made_class)
263+
return
247264
if isinstance(node, nodes.Delete):
248265
for target in node.targets:
249266
if isinstance(target, nodes.Subscript):
@@ -308,7 +325,7 @@ def statement(self, node: nodes.Statement) -> None:
308325
written = alias.name if isinstance(node, nodes.Import) else module
309326
self.emit_effect(Import(None, alias.span, written, symbol, bound))
310327
return
311-
if isinstance(node, nodes.Pass | nodes.Global):
328+
if isinstance(node, nodes.Pass | nodes.Global | nodes.Nonlocal):
312329
# Declarations are already applied by the semantic analyses.
313330
return
314331
self.fail(node)
@@ -365,9 +382,15 @@ def function(self, node: nodes.Function) -> FunctionIR:
365382
for parameter, value in zip(node.parameters, parameter_values, strict=False)
366383
if parameter.name not in reassigned
367384
}
368-
# Captured variables come after the explicit parameters; they are never
369-
# reassigned, an assignment would have made them local.
370-
self.parameters.update(zip(captured, parameter_values[len(node.parameters) :], strict=True))
385+
# Captured variables come after the explicit parameters. A ``nonlocal`` one is
386+
# assigned in this body, so it lives in a local slot initialised from the
387+
# captured value; the others are never reassigned.
388+
bindings = self.scopes.scope_for(node).bindings
389+
self.nonlocals = frozenset(
390+
name for name in captured if name in bindings and bindings[name].kind is BindingKind.NONLOCAL
391+
)
392+
captured_values = dict(zip(captured, parameter_values[len(node.parameters) :], strict=True))
393+
self.parameters.update({n: v for n, v in captured_values.items() if n not in self.nonlocals})
371394
blocks: list[BasicBlock] = []
372395
for cfg_block in self.cfg.blocks.values():
373396
self.instructions = []
@@ -376,6 +399,8 @@ def function(self, node: nodes.Function) -> FunctionIR:
376399
for parameter, value in zip(node.parameters, parameter_values, strict=False):
377400
if parameter.name in reassigned:
378401
self.emit_effect(StoreLocal(None, parameter.span, parameter.name, value))
402+
for name in sorted(self.nonlocals):
403+
self.emit_effect(StoreLocal(None, node.span, name, captured_values[name]))
379404
for statement in cfg_block.statements:
380405
self.statement(statement)
381406
terminator = self.terminator(cfg_block)
@@ -508,6 +533,10 @@ def walk(node: Node) -> None:
508533
_collect(lambda_function(node), into)
509534
return
510535
if isinstance(node, nodes.Class):
536+
# The methods of a class defined inside a function are nested functions.
537+
for member in node.body:
538+
if isinstance(member, nodes.Function):
539+
_collect(member, into)
511540
return
512541
for child in children(node):
513542
walk(child)

‎src/coretrace_python/ir/model.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ class MakeFunction(Operands):
176176
captured: tuple[Value, ...] = ()
177177

178178

179+
@dataclass(frozen=True)
180+
class MakeClass(Operands):
181+
"""A class defined inside a function, bound to its name; its methods are analysed
182+
as nested functions."""
183+
184+
result: Value
185+
location: SourceSpan
186+
name: str
187+
188+
179189
@dataclass(frozen=True)
180190
class BuildString(Operands):
181191
"""An f-string: its constant and formatted parts, in order."""
@@ -374,6 +384,7 @@ class Import(Operands):
374384
| BuildString
375385
| BuildSlice
376386
| MakeFunction
387+
| MakeClass
377388
| WithEnter
378389
| Catch
379390
| Await

‎src/coretrace_python/ir/printer.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
Instruction,
2828
Jump,
2929
LoadLocal,
30+
MakeClass,
3031
MakeFunction,
3132
ModuleIR,
3233
Phi,
@@ -102,6 +103,8 @@ def _instruction(instruction: Instruction) -> str:
102103
return f"del_item {_value(instruction.object)}, {_value(instruction.key)}"
103104
if isinstance(instruction, DelAttr):
104105
return f'del_attr {_value(instruction.object)}, "{instruction.attribute}"'
106+
if isinstance(instruction, MakeClass):
107+
return f'{_value(instruction.result)} = make_class "{instruction.name}"'
105108
if isinstance(instruction, SetGlobal):
106109
return f'set_global "{instruction.name}", {_value(instruction.value)}'
107110
if isinstance(instruction, BuildString):

‎src/coretrace_python/semantic/scopes.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ def statement(self, node: nodes.Statement, scope: _ScopeBuilder) -> None:
317317
elif isinstance(node, nodes.Delete):
318318
for target in node.targets:
319319
self.target(target, scope)
320+
elif isinstance(node, nodes.Declaration):
321+
scope.bind(node.name, BindingKind.LOCAL, node.span)
320322
elif not isinstance(node, nodes.Pass | nodes.Break | nodes.Continue):
321323
raise TypeError(f"unknown statement: {node!r}")
322324

0 commit comments

Comments
 (0)