-
Notifications
You must be signed in to change notification settings - Fork 14k
Open
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-coercionsArea: implicit and explicit `expr as Type` coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
cyrgani commentedon Nov 5, 2024
Note that this compiles if you explicitly state what the closure should return:
I think that the unannotated closure does not compile because the compiler will first see that
xshould be an*mut i32and then try to coerce&i32to*mut i32, which it cannot. With the annotated closure or explicit function, it understands to coerce into*const i32and succeed.