-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Fn item/fn pointer distinction breaks fn pointer generics #20178
Comments
This is actually expected fallout from #19891, and you can patch it up with: fn accepts_optional_fn_ptr(_test: Option<fn(int) -> int>) {}
fn test_int(_: int) -> int { 0i }
fn main() {
accepts_optional_fn_ptr(Some(test_int as fn(int) -> int));
} It is unfortunate, however, that the coercions don't happen deeply through |
@alexcrichton yea, I updated my issue to reflect that. The problem I'm reporting is really one of ergonomics. Adding explicit casts everywhere is a little messy especially if the callbacks have lots of arguments. Then you start adding newtypes for each callback... Even something like an |
fn item/fn pointer are now different things. see: rust-lang/rust#20178 Derefing on MessageItems doesn't work anymore.
The real issue is a more subtle one, as the following program compiles: fn accepts_optional_fn_ptr(_test: Option<fn(int) -> int>) {}
fn test_int(_: int) -> int { 0i }
fn main() {
accepts_optional_fn_ptr(Some::<fn(_) -> _>(test_int));
} As it turns out, we propagate expected types down, we do coercions inside of function calls, but we don't do something which I have no idea what to call so I'm just going to name it "polymorphic unpacking". As I was writing this, @nikomatsakis has provided me with a straight-forward way of doing this. I also realized that my original plan of extracting |
More things are affected by this; here’s another example, one which #20415 does not appear to have fixed: fn a() { }
fn b() { }
fn main() {
let c = if true { a } else { b };
c();
}
Is this a separate issue? Should I file it as a new issue? |
Separate issue please (I believe that sort of situation where the compiler has to implicitly construct some "supertype" without any type annotations is harder). |
@chris-morgan Did you file an additional issue for that? (Can't find one via search) |
This is broken now:
because it expects
Option<fn(int) -> int>
and getsOption<fn(int) -> int {test_int}>
This is an issue for FFI specifically because
Option<fn(...) -> ...>
is the idiomatic way (mentioned in the FFI guide) to represent C function pointers as it takes advantage of nullable pointer optimization.I'll add that for anyone encountering this issue, an explicit cast fixes it:
The text was updated successfully, but these errors were encountered: