-
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
impl Trait
fails to resolve when returning !
#36375
Comments
Currently, |
Considering that |
The issue is which type you coerce to. There can be multiple, and the choice can be observed if there are static methods: https://play.rust-lang.org/?gist=f8679a05f75f3db559870ad2389713bb&version=nightly |
@rkruppe: Ah, I see; that's more awkward. The "non-static implementations only" causes issues too, though, because you'd always be able to get around it with this unintuitive |
|
Yeah, it's like explicitly casting the |
If use std::fmt::Debug;
fn main() {
println!("{:?}", frob());
}
fn frob() -> impl Debug {
unimplemented!()
} |
I recently got bitten by this while working with The
I understand that returning As one possible approach, #![feature(never_type)]
impl Iterator for ! {
type Item = u32;
fn next(&mut self) -> Option<u32> {
None
}
}
fn my_func() -> impl Iterator<Item = u32> {
todo!()
} I have not figured out a way to implement Thanks for all your hard work making Rust awesome! |
How about this? When
|
As stated, that would violate trait coherence, since the crate defining A more serious surprise would be action-at-a-distance panics: if the return type gets unified with some other type variable, then the impl's associated functions could be called, and panic, even if the #![feature(never_type)]
trait Foo {
fn recommended_quantity() -> usize;
}
fn do_thing<T: Foo>(initialize: bool, f: fn() -> T) -> Vec<T> {
let mut v = Vec::with_capacity(T::recommended_quantity());
if initialize {
v.resize_with(T::recommended_quantity(), f);
}
v
}
impl Foo for ! {
fn recommended_quantity() -> usize {
panic!("hypothetically, this implementation is implicit");
}
}
fn main() {
fn never() -> ! { loop {} }
do_thing(false, never);
} Notice in particular that the One way to describe this problem would be that it violates the suggested principle of implementing for
This would implicitly make impls which observably do panic. |
I couldn't think of a better title.
This code doesn't compile on nightly :
while this does :
The text was updated successfully, but these errors were encountered: