Skip to content

Code with return statment in closure does not compile #41

@stepinto

Description

@stepinto

The below function does not compile.

fn foo(f: impl Fn(i32) -> i32) {
    // no-op
}

#[requires(true)]
fn bar() {
    foo(|x: i32| {
        if x < 0 {
            return 0;
        }
        x
    })
}

Macro is expanded into the following code.

    fn foo(f: impl Fn(i32) -> i32) {}
    ///# Contracts
    ///Pre-condition: `true`
    ///
    fn bar() {
        #[allow(clippy::nonminimal_bool)]
        {
            if !true {
                {
                    ::std::rt::panic_display(&"Pre-condition of bar violated: true");
                }
            }
        }
        let ret = 'run: {
            foo(|x: i32| {
                if x < 0 {
                    break 'run 0;
                }
                x
            })
        };
        ret
    }
}

The reason is that, the return statement inside closure should not be replaced to break 'run.

I will post a fix soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions