Allow to initialize nested structs using "Transactions(transactions=[], accounts=[])"
With the following definitions:
from lpython import dataclass, i64
@dataclass
class TransactionItem:
account_idx: i64
commodity: str
amount: i64
@dataclass
class Transaction:
date: str
name: str
cleared: bool
comments: str
items: list[TransactionItem]
@dataclass
class Account:
name: str
@dataclass
class Transactions:
transactions: list[Transaction]
accounts: list[Account]
This does not work:
asr: Transactions = Transactions(transactions=[], accounts=[])
This workaround works:
transactions: list[Transaction] = []
accounts: list[Account] = []
asr: Transactions = Transactions(transactions=transactions, accounts=accounts)
The workaround above gives ASR, but fails to compile:
$ lpython -I. process.py
ASR verify pass error: ASR verify: The symbol table was not found in the scope of `symtab`.
ASR verify pass error: ASR verify: Struct::m_derived_type 'Transaction' cannot point outside of its symbol table, owner: model
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 1869
err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir,
File "/Users/ondrej/repos/lpython/src/bin/lpython.cpp", line 828
res = fe.get_llvm3(*asr, pass_manager, diagnostics, infile);
File "/Users/ondrej/repos/lpython/src/lpython/python_evaluator.cpp", line 71
run_fn, infile);
File "/Users/ondrej/repos/lpython/src/libasr/codegen/asr_to_llvm.cpp", line 8933
pass_manager.apply_passes(al, &asr, pass_options, diagnostics);
File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_manager.h", line 305
apply_passes(al, asr, _passes, pass_options, diagnostics);
File "/Users/ondrej/repos/lpython/src/libasr/pass/pass_manager.h", line 169
+ passes[i]);
LCompilersException: Verify failed in the pass: class_constructor
Hi @certik , I tried to reproduce the above issue for the work around code.
from lpython import dataclass, i64
@dataclass
class TransactionItem:
account_idx: i64
commodity: str
amount: i64
@dataclass
class Transaction:
date: str
name: str
cleared: bool
comments: str
items: list[TransactionItem]
@dataclass
class Account:
name: str
@dataclass
class Transactions:
transactions: list[Transaction]
accounts: list[Account]
transactions: list[Transaction] = []
accounts: list[Account] = []
asr: Transactions = Transactions(transactions=transactions, accounts=accounts)
I am able to reproduce the issue for the workaround code when I use two different modules/files. One contains the definition of the classes and in the other file i import the classes and use the workaround initialization but when I put definition and initialization in a single file I am not able to reproduce it.
Yes, I was using two different files.
To fix this, we need to do https://github.com/lfortran/lfortran/issues/2957.