bevy/crates/bevy_ecs/src/never.rs
Alice Cecile 9254297acd
Use never_say_never hack to work around Rust 2024 regression for fn traits (#18804)
# Objective

After #17967, closures which always panic no longer satisfy various Bevy
traits. Principally, this affects observers, systems and commands.

While this may seem pointless (systems which always panic are kind of
useless), it is distinctly annoying when using the `todo!` macro, or
when writing tests that should panic.

Fixes #18778.

## Solution

- Add failing tests to demonstrate the problem
- Add the trick from
[`never_say_never`](https://docs.rs/never-say-never/latest/never_say_never/)
to name the `!` type on stable Rust
- Write looots of docs explaining what the heck is going on and why
we've done this terrible thing

## To do

Unfortunately I couldn't figure out how to avoid conflicting impls, and
I am out of time for today, the week and uh the week after that.
Vacation! If you feel like finishing this for me, please submit PRs to
my branch and I can review and press the button for it while I'm off.

Unless you're Cart, in which case you have write permissions to my
branch!

- [ ] fix for commands
- [ ] fix for systems
- [ ] fix for observers
- [ ] revert https://github.com/bevyengine/bevy-website/pull/2092/

## Testing

I've added a compile test for these failure cases and a few adjacent
non-failing cases (with explicit return types).

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2025-04-14 19:59:48 +00:00

40 lines
1.6 KiB
Rust

//! A workaround for the `!` type in stable Rust.
//!
//! This approach is taken from the [`never_say_never`] crate,
//! reimplemented here to avoid adding a new dependency.
//!
//! This module exists due to a change in [never type fallback inference] in the Rust 2024 edition.
//! This caused failures in `bevy_ecs`'s traits which are implemented for functions
//! (like [`System`](crate::system::System)) when working with panicking closures.
//!
//! Note that using this hack is not recommended in general;
//! by doing so you are knowingly opting out of rustc's stability guarantees.
//! Code that compiles due to this hack may break in future versions of Rust.
//!
//! Please read [issue #18778](https://github.com/bevyengine/bevy/issues/18778) for an explanation of why
//! Bevy has chosen to use this workaround.
//!
//! [`never_say_never`]: https://crates.io/crates/never_say_never
//! [never type fallback inference]: https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html
mod fn_ret {
/// A helper trait for naming the ! type.
#[doc(hidden)]
pub trait FnRet {
/// The return type of the function.
type Output;
}
/// This blanket implementation allows us to name the never type,
/// by using the associated type of this trait for `fn() -> !`.
impl<R> FnRet for fn() -> R {
type Output = R;
}
}
/// A hacky type alias for the `!` (never) type.
///
/// This knowingly opts out of rustc's stability guarantees.
/// Read the module documentation carefully before using this!
pub type Never = <fn() -> ! as fn_ret::FnRet>::Output;