Skip to content

Commit 06eb669

Browse files
authored
Merge pull request #1607 from mixy1/patch-1
Fix improper documentation on casting non_exhaustive enums
2 parents 687faf9 + 7c94f29 commit 06eb669

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/attributes/type_system.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,35 @@ match message {
163163
}
164164
```
165165

166-
It's also not allowed to cast non-exhaustive types from foreign crates.
167-
```rust, ignore
168-
use othercrate::NonExhaustiveEnum;
166+
It's also not allowed to use numeric casts (`as`) on enums that contain any non-exhaustive variants.
167+
168+
For example, the following enum can be cast because it doesn't contain any non-exhaustive variants:
169+
170+
```rust
171+
#[non_exhaustive]
172+
pub enum Example {
173+
First,
174+
Second
175+
}
176+
```
177+
178+
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:
179+
180+
```rust
181+
#[non_exhaustive]
182+
pub enum EnumWithNonExhaustiveVariants {
183+
First,
184+
#[non_exhaustive]
185+
Second
186+
}
187+
```
188+
189+
<!-- ignore: needs multiple crates -->
190+
```rust,ignore
191+
use othercrate::EnumWithNonExhaustiveVariants;
169192
170-
// Cannot cast a non-exhaustive enum outside of its defining crate.
171-
let _ = NonExhaustiveEnum::default() as u8;
193+
// Error: cannot cast an enum with a non-exhaustive variant when it's defined in another crate
194+
let _ = EnumWithNonExhaustiveVariants::First as u8;
172195
```
173196

174197
Non-exhaustive types are always considered inhabited in downstream crates.

0 commit comments

Comments
 (0)