-
Notifications
You must be signed in to change notification settings - Fork 13.3k
!
cannot be used as impl Trait
when !
implements Trait
#105284
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
Comments
It does compile if you add If I am not mistaken, this issue is essentially a duplicate of the tracking issue of |
#65992 was closed in favor of #35121, if that is the issue you are referring to. // compiler/rustc_hir_typeck/fallback.rs:130
// ...
/// "Diverging" type variables are variables created when we
/// coerce a `!` type into an unbound type variable `?X`.
// ... Do I understand correctly that this (currently) means any coercion of an expression of type |
Workaround for traits such that #![feature(never_type)]
#[allow(unreachable_code)]
pub trait Trait {}
impl Trait for ! {}
impl<T: Trait + ?Sized> Trait for Box<T> {}
pub fn test() -> impl Trait {
let x: ! = panic!();
let x: Box<!> = Box::new(x);
x
} |
I am not deeply familiar with the type inference process, so I sadly cannot answer your question.
That's unfortunate :/ I don't know of any better workaround. |
If the trait doesn't require ownership, you can also rely on static promotion to avoid needing #![feature(never_type)]
pub trait Trait {}
impl Trait for ! {}
impl<T: Trait + ?Sized> Trait for &T {}
pub fn test() -> impl Trait {
#[allow(unreachable_code)]
&panic!()
} |
@rustbot label +requires-nightly |
I tried this code (playground link):
I expected to see this happen: The code should compile. Because
!
implementsTrait
,!
should be a valid return type forfn test
(and no fallback should occur).Instead, this happened: The code does not compile.
x
"falls back" to()
, (which does not implementTrait
), so the function does not typecheck, giving the following error message:See also:
!
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.)impl Trait
where!
did not implementTrait
.Meta
rustc +nightly --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: