Skip to content

LUB coercions works for a function but fails for a closure #132643

Open
@pazmank

Description

@pazmank

I tried the following code (playground), following the examples for LUB coercion in the Reference, chapter 10.7.

The closure does not compile

    let clo = || {
        if true {
            let x: *mut i32 = &mut 1;
            x
        } else if false {
            let x: &i32 = &1;
            x
        } else {
            let x: &mut i32 = &mut 1;
            x
        }
    };
    let _: *const i32 = clo();

with the error:

error[E0308]: `if` and `else` have incompatible types

A similar function does compile:

fn f() -> *const i32 {
    if true {
        let x: *mut i32 = &mut 1;
        x
    } else if false {
        let x: &i32 = &1;
        x
    } else {
        let x: &mut i32 = &mut 1;
        x
    }
}

According to the Reference, I expected that either both the closure and the function compiles or neither of them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions