Skip to content

Closure-to-fn coercion does not work for unsafe fn #58478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RalfJung opened this issue Feb 15, 2019 · 2 comments
Closed

Closure-to-fn coercion does not work for unsafe fn #58478

RalfJung opened this issue Feb 15, 2019 · 2 comments
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions

Comments

@RalfJung
Copy link
Member

RalfJung commented Feb 15, 2019

Given that non-capturing closures coerce to fn, and fn coerces to unsafe fn, I would expect the transitive step to also happen -- but it does not:

fn coerce(_x: fn()) {}
fn coerce_unsafe(_x: unsafe fn()) {}

fn foo() {}

fn main() {
    coerce(|| panic!()); // works
    coerce_unsafe(foo); // works
    coerce_unsafe(|| panic!()); // fails
}
error[E0308]: mismatched types
 --> src/main.rs:9:19
  |
9 |     coerce_unsafe(|| panic!());
  |                   ^^^^^^^^^^^ expected unsafe fn, found normal fn
  |
  = note: expected type `unsafe fn()`
             found type `[closure@src/main.rs:9:19: 9:30]`

This is particularly annoying when instantiating RawWakerVTable, because it means one cannot just use closure syntax there.

@hellow554
Copy link
Contributor

hellow554 commented Feb 15, 2019

https://doc.rust-lang.org/stable/reference/type-coercions.html#coercion-types

Coercion is allowed between the following types:

  • T_1 to T_3 where T_1 coerces to T_2 and T_2 coerces to T_3 (transitive case)
    Note that this is not fully supported yet

I guess this is one of the not supported cases? I ask myself why this is generally not supported? What's missing here?

@jonas-schievink jonas-schievink added the A-coercions Area: implicit and explicit `expr as Type` coercions label Feb 15, 2019
@Centril
Copy link
Contributor

Centril commented Feb 15, 2019

Duplicate of #57883.

@Centril Centril closed this as completed Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions
Projects
None yet
Development

No branches or pull requests

4 participants