Skip to content

Commit e02caca

Browse files
committed
Fail generation if a non-class statement sits between generated classes
The define-before-use reorder reassembles a generated wire package from its class blocks plus the text before the first class and after the last, so a module-level statement emitted between two classes would silently disappear. codegen emits none today; assert it stays that way.
1 parent 986d3b9 commit e02caca

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

‎scripts/gen_surface_types.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,22 @@ def emit(ci: int) -> None:
323323
}
324324
assert not unresolved, f"classes still name a later-defined class: {sorted(unresolved)}"
325325

326+
# The reorder reassembles the file from the class blocks, the text before
327+
# the first class and the text after the last: any other statement between
328+
# two classes would silently vanish, so codegen must never emit one there.
329+
first_line = classes[0].lineno
330+
last_line = max(node.end_lineno or 0 for node in classes)
331+
interstitial = [
332+
node.lineno
333+
for node in tree.body
334+
if not isinstance(node, ast.ClassDef) and first_line <= node.lineno <= last_line
335+
]
336+
assert not interstitial, f"non-class statements between generated classes would be dropped: lines {interstitial}"
337+
326338
node_of = {node.name: node for node in classes}
327339
blocks = ["".join(lines[node_of[name].lineno - 1 : node_of[name].end_lineno]) for name in emitted]
328-
head = "".join(lines[: classes[0].lineno - 1])
329-
tail = "".join(lines[max(node.end_lineno or 0 for node in classes) :])
340+
head = "".join(lines[: first_line - 1])
341+
tail = "".join(lines[last_line:])
330342
return head + "\n\n\n".join(block.strip("\n") + "\n" for block in blocks) + tail
331343

332344

0 commit comments

Comments
 (0)