-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add info about !
and impl Trait
#76099
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
Changes from 6 commits
4aae781
fd985e2
0d9a2ab
26eab6a
80dcad9
bd31962
7e2548f
37ea97c
e13a701
cdd6f11
c4c058c
913354b
55637f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,14 +194,47 @@ mod prim_bool {} | |
/// # `!` and traits | ||
/// | ||
/// When writing your own traits, `!` should have an `impl` whenever there is an obvious `impl` | ||
/// which doesn't `panic!`. As it turns out, most traits can have an `impl` for `!`. Take [`Debug`] | ||
/// which doesn't `panic!`. The reason is that functions returning an `impl Trait` where `!` | ||
/// does not have an `impl` of `Trait` cannot diverge as their only possible code path. In other | ||
/// words, they can't return `!` from every code path. As an example, this code doesn't compile: | ||
/// | ||
/// ```compile_fail | ||
/// use core::ops::Add; | ||
/// | ||
/// fn foo() -> impl Add<u32> { | ||
/// unimplemented!() | ||
/// } | ||
/// ``` | ||
/// | ||
/// But this code does: | ||
/// | ||
/// ``` | ||
/// use core::ops::Add; | ||
/// | ||
/// fn foo() -> impl Add<u32> { | ||
/// if true { | ||
/// unimplemented!() | ||
/// } else { | ||
/// 0 | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// The reason is that, in the first example, there are many possible types that `!` could coerce | ||
/// to, because the function can return one of many concrete types. However, in the second example, | ||
/// the `else` branch returns a `0` of type `u32`, which is a concrete type that `!` can be coerced | ||
camelid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// to. See issue [#36375] for more information on this quirk of `!`. | ||
/// | ||
/// [#36375]: https://github.com/rust-lang/rust/issues/36375 | ||
/// | ||
/// As it turns out, though, most traits can have an `impl` for `!`. Take [`Debug`] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most -> all? Not sure if that's true but it seems like it should be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I thought too; I would think you would be able to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even simpler, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean because it can be coerced to any concrete type, so it can just return itself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
/// for example: | ||
/// | ||
/// ``` | ||
/// #![feature(never_type)] | ||
/// # use std::fmt; | ||
/// # trait Debug { | ||
/// # fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result; | ||
/// # fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result; | ||
/// # } | ||
Comment on lines
236
to
238
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to delete this redefinition and import it instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it's because of the orphan rule or something, so I had to revert my change :( |
||
/// impl Debug for ! { | ||
/// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
|
Uh oh!
There was an error while loading. Please reload this page.