Skip to content
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

COMPILATION_FAILED in starknet declare when a recursive function is used in the contract #158

Open
Lukasz2891 opened this issue Apr 6, 2023 · 1 comment

Comments

@Lukasz2891
Copy link

#[contract]
mod HelloStarknet {
    use array::ArrayTrait;

    #[external]
    fn test() {
        let mut arr = ArrayTrait::new();
        arr.append(233);
        arr.append(124);

        rec(arr, 0_usize);
    }

    fn rec(arr: Array<felt252>, index: usize) {
        if (index == arr.len()) {
            return ();
        }

        rec(arr, index + 1_usize);
    }
}

causes

Error: StarkException: (500, {'code': <StarknetErrorCode.COMPILATION_FAILED: 2>, 'message': 'Compilation failed. Error: Compilation failed.\n\nCaused by:\n    failed solving the symbol tables\n'})

for starknet declare.

Checked starknet versions: 0.11.0.1 as well as 0.11.0.2.
Cairo versions: main:9c3cb963 and also 1.0.0-alpha-6:439da05a

@Lukasz2891
Copy link
Author

It needs to have:

        match gas::withdraw_gas_all(get_builtin_costs()) {
            Option::Some(_) => {},
            Option::None(_) => panic(...),
        };

at the top of the recursive function

so

    fn rec(arr: Array<felt252>, index: usize) {
        match gas::withdraw_gas_all(get_builtin_costs()) {
            Option::Some(_) => {},
            Option::None(_) => panic(out_of_gas_array()),
        };

        if (index == arr.len()) {
            return ();
        }

        rec(arr, index + 1_usize);
    }

    fn out_of_gas_array() -> Array<felt252> {
        let mut arr = ArrayTrait::new();
        arr.append('Out of gas');

        arr
    }

declares properly, but it's generally unclear, what the compiler says and why.

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

No branches or pull requests

1 participant