← 输入框|github.com|刷新
Skip to content

C backend: failure to pass struct by reference #2059

Closed

Description

@emoise-gsi

The following code fails to generate valid c code:

from lpython import ccallback, ccall, packed, dataclass, i32, c_p_pointer, Pointer, CPtr, u16, pointer, InOut

@packed
@dataclass
class inner_struct:
    a: i32

@packed
@dataclass
class outer_struct:
    b: inner_struct = inner_struct(0)

def update_my_inner_struct(my_inner_struct: InOut[inner_struct]) -> None:
    my_inner_struct.a = 99999

def main() -> None:
    my_outer_struct: outer_struct = outer_struct()

    update_my_inner_struct(my_outer_struct.b)
    assert my_outer_struct.b.a == 99999

main()

However the following code generates valid c. Note that in this case we assign the inner_struct to a temp variable.

from lpython import ccallback, ccall, packed, dataclass, i32, c_p_pointer, Pointer, CPtr, u16, pointer, InOut

@packed
@dataclass
class inner_struct:
    a: i32

@packed
@dataclass
class outer_struct:
    b: inner_struct = inner_struct(0)

def update_my_inner_struct(my_inner_struct: InOut[inner_struct]) -> None:
    my_inner_struct.a = 99999

def main() -> None:
    my_outer_struct: outer_struct = outer_struct()
    my_inner_struct: inner_struct = my_outer_struct.b

    update_my_inner_struct(my_inner_struct)
    assert my_inner_struct.a == 99999

main()

Activity

  1. certik commented on Jun 29, 2023

    @certik
    Contributor

    Thanks @emoise-gsi! We'll look into this.

  2. added
    c
    asr
    on Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    asrc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions