Fix: Raise SemanticError for unsupported inline class instantiation #2826#2873
Open
amritamishra01 wants to merge 2 commits intolcompilers:mainfrom
Open
Fix: Raise SemanticError for unsupported inline class instantiation #2826#2873amritamishra01 wants to merge 2 commits intolcompilers:mainfrom
amritamishra01 wants to merge 2 commits intolcompilers:mainfrom
Conversation
Collaborator
|
Hello, @amritamishra01 the MacOS CI is currently broken it needs to be fixed. A lot of work is currently being focused on the back end over at the LFortran repo. I suggest you contact @certik on zulip. |
Collaborator
|
#2875 Should fix it, its a small change so I merged it. In regards to the other PRs you have up you can ask on zulip. You can rebase your branches on top of main to get the CIs working |
Author
|
Update: Rebased on the latest main and all CI checks are now passing (18/18). I have verified the fix locally and confirmed the tests pass on the CI. This is ready for final review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.