Open
Description
I tried this code (playground link):
#![feature(never_type)]
#![allow(unreachable_code)]
pub trait Trait {}
impl Trait for ! {}
pub fn test() -> impl Trait {
let x: ! = panic!();
x
}
I expected to see this happen: The code should compile. Because !
implements Trait
, !
should be a valid return type for fn test
(and no fallback should occur).
Instead, this happened: The code does not compile. x
"falls back" to ()
, (which does not implement Trait
), so the function does not typecheck, giving the following error message:
error[E0277]: the trait bound `(): Trait` is not satisfied
--> src/lib.rs:7:18
|
7 | pub fn test() -> impl Trait {
| ^^^^^^^^^^ the trait `Trait` is not implemented for `()`
|
= help: the trait `Trait` is implemented for `!`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
See also:
- Tracking issue for making
!
a type: Tracking issue for promoting!
to a type (RFC 1216) #35121. (I looked through the tracking issue and did not see this issue mentioned.) - This is different from Issue with impl Trait and panic!() #55022, which concerned an
impl Trait
where!
did not implementTrait
.
Meta
rustc +nightly --version --verbose
:
rustc 1.67.0-nightly (32e613bba 2022-12-02)
binary: rustc
commit-hash: 32e613bbaafee1bcabba48a2257b838f8d1c03d3
commit-date: 2022-12-02
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Backtrace
<backtrace>