degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

Passing struct self as a parameter can result in invalid IL #1118

Closed degory closed 8 months ago

degory commented 8 months ago

Passing self as a parameter to a function or method can result in IL that throws an invalid program exception or otherwise fails at runtime, if self is a struct.

The issue is that ldarg.0 loads the address of the current instance not that value:

struct THING is
    init() is
        // the string interpolator expects self to be passed by value here
        // but we wrongly pass a reference to `self` via `ldarg.0`
        IO.Std.write_line("{self}"); 
    si
    to_string() -> string => "a thing";
si 

The fix is to generate an appropriately type ldobj to load the value from the reference, where needed