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

bug: Failed calculating gas usage in closure #7234

Open
enitrat opened this issue Feb 6, 2025 · 1 comment · May be fixed by #7258
Open

bug: Failed calculating gas usage in closure #7234

enitrat opened this issue Feb 6, 2025 · 1 comment · May be fixed by #7258
Labels
bug Something isn't working

Comments

@enitrat
Copy link
Contributor

enitrat commented Feb 6, 2025

Bug Report

Cairo version:
scarb 2.9.2+nightly-2025-02-01 (3a752f93d 2025-02-01)
cairo: 2.9.2 (99f0528)
sierra: 1.6.0

On the following code:

// A function which takes a closure as an argument and calls it.
// <F> denotes that F is a "Generic type parameter"
fn apply<F, +Drop<F>, impl func: core::ops::FnOnce<F, ()>, +Drop<func::Output>>(f: F) {
    // ^ TODO: Try changing this to `Fn`.

    f();
}

fn main() {
    // A non-copy type.
    let greeting: ByteArray = "hello";
    let farewell: ByteArray = "goodbye";

    // Capture 2 variables: `greeting` by snapshot and
    // `farewell` by value.
    let diary = || {
        // `greeting` is by snapshot: requires `Fn`.
        println!("I said {}.", greeting);

        // Using farewell by value requires `FnOnce`.
        // Convert farewell to uppercase to demonstrate value capture through `into_iter`
        let mut iter = farewell.into_iter();
        let uppercase: ByteArray = iter
            .map(|c| if c >= 'a' {
                c - 32
            } else {
                c
            })
            .collect();
        println!("Then I screamed {}!", uppercase);
    };

    // Call the function which applies the closure.
    apply(diary);
}

error: Failed calculating gas usage, it is likely a call for `gas::withdraw_gas` is missing. Inner error: error from the program registry

Caused by:
    0: error from the program registry
    1: Error during libfunc specialization of function_call<user@Generated core::ops::function::FnOnce::<{closure@/Users/msaug/workspace/cairo-by-example/listings/functions/closures/input_parameters/src/lib.cairo:22:17: 22:19}, ()>::call>: Could not specialize libfunc `function_call` with generic_args: [user@Generated core::ops::function::FnOnce::<{closure@/Users/msaug/workspace/cairo-by-example/listings/functions/closures/input_parameters/src/lib.cairo:22:17: 22:19}, ()>::call]. Error: Could not find the requested function.
@enitrat enitrat added the bug Something isn't working label Feb 6, 2025
@enitrat enitrat changed the title bug: Failed calculating gas usag bug: Failed calculating gas usage in closure Feb 6, 2025
@a-zmuth
Copy link

a-zmuth commented Feb 6, 2025

Hello! I'd like to help with this issue. Looks like an interesting problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants