reference icon indicating copy to clipboard operation
reference copied to clipboard

Least-Upper-Bound coercions don't document special behaviour of coercing to fnptrs

Open BoxyUwU opened this issue 2 months ago • 1 comments

fn foo() {}
fn bar() {}

let a = match true {
    true => foo,
    false => bar,
};

Here we wind up with a LUB coercion between two different function item types. This succeeds by coercing both types to function pointers.

The current documentation for type coercions defines LUB coercions as simply trying one-to-one coercion both ways and then trying to compute a mutual supertype if that fails. It should also document that when we have a LUB coercion between function item types and closures we'll coerce to a function pointer if there's no mutual supertype.

BoxyUwU avatar Nov 21 '25 10:11 BoxyUwU

This is one area where the FLS has moved on from the Reference. It says (§4.12.3:54, fls_92pwnd1xbp5r):

if TC and U are non-capturing closure types, function item types, function pointer types, or a combination of those types, and a function pointer type exists that both TC and U can coerce to, make that function pointer type be target type U.

See https://github.com/rust-lang/fls/pull/366.

I think what it claims is a little too strong. See discussion at https://github.com/rust-lang/reference/issues/1525#issuecomment-2952373463.

mattheww avatar Nov 21 '25 13:11 mattheww