Skip to content

Support struct initialization with named arguments #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2023

Conversation

ubaidsk
Copy link
Collaborator

@ubaidsk ubaidsk commented May 16, 2023

fixes #1800.

Comment on lines -6615 to -6619
// Keyword arguments handled in make_call_helper
#define parse_args() if( x.n_keywords == 0 ) { \
args.reserve(al, x.n_args); \
visit_expr_list(x.m_args, x.n_args, args); \
} \
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I was overlooking the first line if( x.n_keywords == 0 ) { \. Hence, I refactored this into a function, which I am hoping is more readable.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented May 16, 2023

I added some general error situations in tests/errors.

The working of named arguments is not yet perfect or exactly similar to cpython. The AST generated contains the m_args and m_keywords separately, even if the user source code contains them as mixed.

For example:

s = S(1, c=2, 3)

For the above, the ast contains args as [1, 3] and keywords as [(c, 2)]. This case in cpython would throw error that named argument is before positional argument, but the same example works in lpython. We are currently unable to catch such errors as the ordering information about args and keywords is not present in the AST, I think.

$ cat examples/expr2.py 
from lpython import i32, dataclass

@dataclass
class S:
    a: i32
    b: i32
    c: i32

def main0():
    s:S = S(1,c=2,3)
    print(s.a)
    print(s.b)
    print(s.c)

main0()
$ python examples/expr2.py                       
  File "/Users/ubaid/Desktop/OpenSource/lpython/examples/expr2.py", line 10
    s:S = S(1,c=2,3)
                   ^
SyntaxError: positional argument follows keyword argument
$ lpython examples/expr2.py                      
1
3
2

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented May 16, 2023

Ready.

@ubaidsk ubaidsk requested a review from certik May 16, 2023 12:24
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful, thank you!

@certik certik merged commit c45140d into lcompilers:main May 16, 2023
@ubaidsk ubaidsk deleted the struct_init_name_args branch May 16, 2023 13:43
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.

Struct initialization segfaults with name arguments
3 participants