Skip to content

Commit 4225528

Browse files
author
Tristan Konolige
committed
[TVMScript,Fix] Fix findsource when classes are indented
Leaving class definitions was not correctly handled when recreating scoping information. The fix correctly pops scope whenever the indentation level becomes less than the current scope.
1 parent cec5f0b commit 4225528

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

python/tvm/script/parser/core/diagnostics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ def findsource(obj):
163163
name = tokens[1].split(":")[0].split("(")[0] + "<locals>"
164164
elif tokens[0] == "class":
165165
name = tokens[1].split(":")[0].split("(")[0]
166+
# pop scope if we are less indented
167+
while scope_stack and indent_info[scope_stack[-1]] >= indent:
168+
scope_stack.pop()
166169
if name:
167-
while scope_stack and indent_info[scope_stack[-1]] >= indent:
168-
scope_stack.pop()
169170
scope_stack.append(name)
170171
indent_info[name] = indent
171172
if scope_stack == qual_names:

tests/python/unittest/test_tvmscript_parser_source.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,20 @@ def test_source_ast():
8282
assert isinstance(for_block, doc.With) and len(for_block.body) == 2
8383

8484

85+
def test_nesting_parsing():
86+
class dummy:
87+
pass
88+
89+
for i in range(1):
90+
91+
@tvm.script.ir_module
92+
class Module:
93+
@T.prim_func
94+
def impl(
95+
A: T.Buffer[(12, 196, 64), "float32"],
96+
) -> None:
97+
T.evaluate(0)
98+
99+
85100
if __name__ == "__main__":
86101
tvm.testing.main()

0 commit comments

Comments
 (0)