Skip to content

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

@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.

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 5, 2024
cyrgani

cyrgani commented on Nov 5, 2024

@cyrgani
Contributor

Note that this compiles if you explicitly state what the closure should return:

let clo = || -> *const i32 {
    ...
};

I think that the unannotated closure does not compile because the compiler will first see that x should be an *mut i32 and then try to coerce &i32 to *mut i32, which it cannot. With the annotated closure or explicit function, it understands to coerce into *const i32 and succeed.

added
A-closuresArea: Closures (`|…| { … }`)
A-coercionsArea: implicit and explicit `expr as Type` coercions
D-confusingDiagnostics: Confusing error or lint that should be reworked.
on Nov 5, 2024
added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @lolbinarycat@pazmank@jieyouxu@rustbot@cyrgani

        Issue actions

          LUB coercions works for a function but fails for a closure · Issue #132643 · rust-lang/rust