-
Notifications
You must be signed in to change notification settings - Fork 13.4k
#[non_exhaustive]
on variant should block cross-crate as
casts
#91161
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Across different crates the code does not compile, the
|
#[non_exhaustive]
on variants block cross-crate as
casts?#[non_exhaustive]
on variant should block cross-crate as
casts
Confirmed this is a problem: #91281 Nominating for lang to confirm that this should be fixed. Presumably via a forward-compat error at first. |
…Mark-Simulacrum Add demonstration test for rust-lang#91161 Since cross-crate things are hard to demonstrate in playground, here's a test showing that something currently works that shouldn't. cc rust-lang#91161 that tracks fixing the problem (and updating this test)
…askrgr Rollup of 5 pull requests Successful merges: - rust-lang#91049 (Add a caveat to std::os::windows::fs::symlink_file) - rust-lang#91281 (Add demonstration test for rust-lang#91161) - rust-lang#91327 (Delete an unreachable codepath from format_args implementation) - rust-lang#91336 (Remove unused root_parent.) - rust-lang#91349 (Accumulate all values of `-C remark` option) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
We discussed this in today's @rust-lang/lang meeting, and we agree that this is a bug. We should fix this if possible. We'll need a crater run to confirm that this won't break things. |
And a clarification from the meeting: This should error in the downstream crate, saying something along the lines of "enums with I don't know how hard this is; hopefully it should be relatively straight-forward following all the other |
@rustbot claim |
…eign-variants, r=scottmcm Check if enum from foreign crate has any non exhaustive variants when attempting a cast Fixes rust-lang#91161 As stated in the issue, this will require a crater run as it might break other people's stuff.
I tried the code in the first post and it still compiles with rustc 1.77.2 for thumbv7em-none-eabi target. |
In all compiler versions since 1.65 (containing #92744) the code at the top of this issue produces the following diagnostic. error[E0606]: casting `Foo` as `u8` is invalid
--> crate2.rs:3:5
|
3 | f as u8
| ^^^^^^^
|
= note: cannot cast an enum with a non-exhaustive variant when it's defined in another crate |
Uh oh!
There was an error while loading. Please reload this page.
This currently compiles
However, that
as
cast would be an erroneous "non-primitive cast" if the variant ever got any fields, as it's allowed to do by the#[non_exhaustive]
.Thus we need to fix the compiler so that the cast is rejected outside the crate that owns
Foo
.The text was updated successfully, but these errors were encountered: