Skip to content

Declaring a variable via out breaks when the type of the variable changes #16645

@straight-shoota

Description

@straight-shoota

When a variable declared as an out parameter of lib call is later assigned a different type, we get memory corruption:

lib LibC
  fun modf(value : Float64, iptr : Float64*) : Float64
end
 
LibC.modf(32.14, out foo)
foo = {foo, foo}
foo # => {0.0, 0.0}

If we change the assignment to a different variable than foo, everything works as expected (value is {32.0, 32.0}).

The behaviour appears in both the compiler and interpreter, so I presume it's a semantic error.

This error came up in #16555 (comment).

I suppose a simple solution would be to disallow changing the type of an out var: out should act like a type declaration and freeze the type of the variable, making it impossible to produce any confusion.


Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.topic:compiler:semantic

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions