Fix: Raise SemanticError for unsupported inline class instantiation #2826 #2873
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Hi @swamishiju Fixes #2826 where the compiler hangs (infinite loop) when encountering inline class instantiation, such as
print(Foo(10)).The Issue
Currently, the compiler attempts to lower temporary class objects created inline but fails to handle the lifecycle correctly in
python_ast_to_asr.cpp, resulting in infinite recursion/hanging during the lowering phase.The Fix
I implemented a check in the semantic analysis phase (
visit_Call) to detect this unsupported pattern before it reaches the lowering stage.src/lpython/semantics/python_ast_to_asr.cppCallto aStruct(Class) symbol.SemanticErroradvising the user to assign the object to a variable first.Verification
I added two new test cases to ensure the fix works and is safe:
Error Case:
tests/errors/test_inline_class_error.pyprint(Foo(10))andmy_func(Foo(20)).SemanticErrorimmediately instead of hanging.Regression Test:
tests/reference/pass_inline_class_safe.pya = Foo(10); print(a).Future Work
This is a safeguard fix. Full support for inline class instantiation will require implementing proper lowering logic for temporary class objects, likely by separating symbol registration from body lowering to handle recursive resolution.