Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8489,6 +8489,32 @@ we will have to use something else.
void visit_Call(const AST::Call_t &x) {
std::string call_name = "";
Vec<ASR::call_arg_t> args;
for (size_t i = 0; i < x.n_args; i++) {
AST::expr_t *arg_expr = x.m_args[i];

if (arg_expr->type == AST::exprType::Call) {
AST::Call_t *call_node = (AST::Call_t*)arg_expr;

if (call_node->m_func->type == AST::exprType::Name) {
AST::Name_t *func_name = (AST::Name_t*)call_node->m_func;
std::string name = std::string(func_name->m_id);

// Resolve symbol
ASR::symbol_t *sym = current_scope->resolve_symbol(name);

if (sym) {
sym = ASRUtils::symbol_get_past_external(sym);

// FIX: Only check for Struct (Python classes are Structs in ASR)
if (sym->type == ASR::symbolType::Struct) {
throw SemanticError("Inline class instantiation (e.g. print(Foo(10))) is not yet supported. "
"Please assign it to a variable first.",
arg_expr->base.loc);
}
}
}
}
}
if (AST::is_a<AST::Name_t>(*x.m_func)) {
AST::Name_t *n = AST::down_cast<AST::Name_t>(x.m_func);
call_name = n->m_id;
Expand Down
15 changes: 15 additions & 0 deletions tests/errors/test_inline_class_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Foo:
def __init__(self: "Foo", x: i32):
self.x: i32 = x

def my_func(f: Foo):
pass

def main():
# This should trigger the SemanticError immediately
print(Foo(10))

# This checks the other case (function params)
my_func(Foo(20))

main()
9 changes: 9 additions & 0 deletions tests/reference/pass_inline_class_safe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Foo:
def __init__(self: "Foo", x: i32):
self.x: i32 = x

def main():
a: Foo = Foo(10)
print(a)

main()
Loading