-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Traits on function pointers #100116
Comments
This is two things.
|
I spent a bunch of time banging my head against what looks like the same problem. Minimized example: fn f1() {}
fn f2() {}
struct S {}
impl From<fn()> for S {
fn from(f: fn()) -> S {
S {}
}
}
fn main() {
// Example code did this, which works:
let s1 = S::from(if true { f1 } else { f2 });
// My code did this, which does not work:
let s2 = S::from(f1);
// I also tried this, which also does not work:
let s3 = S::from(if true { f1 } else { f1 });
} This gives the following error:
(and the same for I think two things contributed to my confusion:
|
...instead of just from function pointers. I'm making this change not because I actually want to pass a closure, but to make passing a single fixed function work. This commit also simplifies the scrollable example slightly, and without the other half of this change that simplified example fails to compile with: ``` error[E0277]: the trait bound `iced::theme::ProgressBar: From<for<'a> fn(&'a Theme) -> iced::widget::progress_bar::Appearance {progress_bar_custom_style}>` is not satisfied --> examples/scrollable/src/main.rs:292:28 | 292 | .style(progress_bar_custom_style) | ----- ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<for<'a> fn(&'a Theme) -> iced::widget::progress_bar::Appearance {progress_bar_custom_style}>` is not implemented for `iced::theme::ProgressBar` | | | required by a bound introduced by this call | = help: the trait `From<for<'a> fn(&'a Theme) -> iced::widget::progress_bar::Appearance>` is implemented for `iced::theme::ProgressBar` = note: required for `for<'a> fn(&'a Theme) -> iced::widget::progress_bar::Appearance {progress_bar_custom_style}` to implement `Into<iced::theme::ProgressBar>` note: required by a bound in `iced::widget::ProgressBar::<Renderer>::style` --> /home/marienz/src/iced/widget/src/progress_bar.rs:77:21 | 77 | style: impl Into<<Renderer::Theme as StyleSheet>::Style>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ProgressBar::<Renderer>::style` ``` This happens because `progress_bar_custom_style` by itself is a function item, which is typically coerced to a function pointer when one is needed, but not in this case. It is possible to work around this on the caller's side, but especially since the compiler diagnostic for this is a bit rough (see rust-lang/rust#100116) let's try to make it work out of the box.
First of all, I'm not yet sure if this is a problem with just diagnostics, or with the entire type system.
The following is a minimal example of an issue I got working on a larger project. The example may seem like a weird thing to do
but that's because it's minimized.
(https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5f3e7f2b710cec461b0f4f824583fa69)
I expected this to work, but it doesn't. It gives the following (weird) error:
What's weird about it is this: it doesn't seem to think MmyTrait2 is implemented for
fn(Box<MyEnum>) -> MyEnum
however, the help messages says: look, it is implemented for
fn(Box<T>) -> T
which my type conforms to.I started digging a bit. Maybe it's because of the enum variant not actually being a fn:
But this gives a similar error.
Alright, maybe it's the function pointer. What happens if I ask for a closure? (I'd quite like the function pointers since with closures I need a generic leading to overlapping impls, and I never expect to get a closure anyway. I thought that was the problem at first, distinguishing an overlapping impl with the fn pointer but the example seems to prove that it goes wrong even if there aren't any other implementors of
MyTrait2
)This seems to work. So it specifically has to do with function pointers.
Now one more thing, if I insert an explicit cast to the function pointer type:
Everything works.
breaks in a similar manner:
Meta
rustc --version --verbose
:(though I also tried nightly, and it's not fixed yet there)
(there is no backtrace here since there's no program that crashed. Code just didn't compile)
I'm mentioning @estebank since I can't add the A-diagnostics tag myself on a more generic bug report. That only happens if I choose the diagnostics template and I think this bug may be broader than that. I hope you don't mind :)
The text was updated successfully, but these errors were encountered: