diff --git a/src/attributes/type_system.md b/src/attributes/type_system.md index dd3ea9874..d3ea63233 100644 --- a/src/attributes/type_system.md +++ b/src/attributes/type_system.md @@ -163,12 +163,35 @@ match message { } ``` -It's also not allowed to cast non-exhaustive types from foreign crates. -```rust, ignore -use othercrate::NonExhaustiveEnum; +It's also not allowed to use numeric casts (`as`) on enums that contain any non-exhaustive variants. + +For example, the following enum can be cast because it doesn't contain any non-exhaustive variants: + +```rust +#[non_exhaustive] +pub enum Example { + First, + Second +} +``` + +However, if the enum contains even a single non-exhaustive variant, casting will result in an error. Consider this modified version of the same enum: + +```rust +#[non_exhaustive] +pub enum EnumWithNonExhaustiveVariants { + First, + #[non_exhaustive] + Second +} +``` + + +```rust,ignore +use othercrate::EnumWithNonExhaustiveVariants; -// Cannot cast a non-exhaustive enum outside of its defining crate. -let _ = NonExhaustiveEnum::default() as u8; +// Error: cannot cast an enum with a non-exhaustive variant when it's defined in another crate +let _ = EnumWithNonExhaustiveVariants::First as u8; ``` Non-exhaustive types are always considered inhabited in downstream crates.