Skip to content

Conversation

@amritamishra01
Copy link

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.

  • File: src/lpython/semantics/python_ast_to_asr.cpp
  • Logic: The visitor now checks if a function call argument is a direct Call to a Struct (Class) symbol.
  • Action: If detected, it raises a clear SemanticError advising 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:

  1. Error Case: tests/errors/test_inline_class_error.py

    • Contains print(Foo(10)) and my_func(Foo(20)).
    • Verified that it now raises a SemanticError immediately instead of hanging.
  2. Regression Test: tests/reference/pass_inline_class_safe.py

    • Contains a = Foo(10); print(a).
    • Verified that valid usage (assigning to a variable first) still compiles and produces correct ASR.

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.

@swamishiju
Copy link
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.

@swamishiju
Copy link
Collaborator

swamishiju commented Dec 8, 2025

#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

@amritamishra01
Copy link
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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inplace class definitions causes infinite looping

2 participants