FuelLabs / sway

🌴 Empowering everyone to build reliable and efficient smart contracts.
https://docs.fuel.network/docs/sway/
Apache License 2.0
62.58k stars 5.37k forks source link

Compiler stack-overflows on a combination of recursive `const` definitions #6540

Open ironcev opened 2 months ago

ironcev commented 2 months ago

The below example will result in the following stack-overflow:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted

Note that the example contains two recursive const definitions, MOD_FN and S_ASSOC/MOD_CONST:

library;

pub const MOD_FN: u8 = mod_fn();

fn mod_fn() -> u8 {
    MOD_FN
}

struct S {}

impl S {
    const S_ASSOC: u8 = MOD_CONST;
}

const MOD_CONST: u8 = S::S_ASSOC;

The overflow happens only if both definitions are present. E.g., if we have only:

library;

pub const MOD_FN: u8 = mod_fn();

fn mod_fn() -> u8 {
    MOD_FN
}

the compiler will emit errors.

Errors will be emitted also in this case:

library;

struct S {}

impl S {
    const S_ASSOC: u8 = MOD_CONST;
}

const MOD_CONST: u8 = S::S_ASSOC;

The overflow happens only when both recursive definitions are present.

When fixing this issue, enable and adjust the recursive_const_stack_overflow test which is linked to this issue via GitHub link in the test TODO.

petertonysmith94 commented 1 month ago

@FuelLabs/sway-compiler We've experienced this issue with the following PR. We do have a really big contract, so maybe a workaround would be to break down this contract into smaller chunks, but it would be good to get some advise on workarounds.