-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for unsizing casts in const fns #64992
Comments
…fJung Stabilize casts and coercions to `&[T]` in const fn Part of rust-lang#64992 There was never a reason to not stabilize this, we just accidentally prevented them when we implemented the `min_const_fn` feature that gave us `const fn` on stable. This PR stabilizes these casts (which are already stable in `const` outside `const fn`), while keeping all other unsizing casts (so `T` -> `dyn Trait`) unstable within const fn. These casts have no forward compatibility concerns with any future features for const eval and users were able to use them under the `const_fn` feature gate already since at least the miri merger, possibly longer. r? @rust-lang/lang
…fJung Stabilize casts and coercions to `&[T]` in const fn Part of rust-lang#64992 There was never a reason to not stabilize this, we just accidentally prevented them when we implemented the `min_const_fn` feature that gave us `const fn` on stable. This PR stabilizes these casts (which are already stable in `const` outside `const fn`), while keeping all other unsizing casts (so `T` -> `dyn Trait`) unstable within const fn. These casts have no forward compatibility concerns with any future features for const eval and users were able to use them under the `const_fn` feature gate already since at least the miri merger, possibly longer. r? @rust-lang/lang
…fJung Stabilize casts and coercions to `&[T]` in const fn Part of rust-lang#64992 There was never a reason to not stabilize this, we just accidentally prevented them when we implemented the `min_const_fn` feature that gave us `const fn` on stable. This PR stabilizes these casts (which are already stable in `const` outside `const fn`), while keeping all other unsizing casts (so `T` -> `dyn Trait`) unstable within const fn. These casts have no forward compatibility concerns with any future features for const eval and users were able to use them under the `const_fn` feature gate already since at least the miri merger, possibly longer. r? @rust-lang/lang
…fJung Stabilize casts and coercions to `&[T]` in const fn Part of rust-lang#64992 There was never a reason to not stabilize this, we just accidentally prevented them when we implemented the `min_const_fn` feature that gave us `const fn` on stable. This PR stabilizes these casts (which are already stable in `const` outside `const fn`), while keeping all other unsizing casts (so `T` -> `dyn Trait`) unstable within const fn. These casts have no forward compatibility concerns with any future features for const eval and users were able to use them under the `const_fn` feature gate already since at least the miri merger, possibly longer. r? @rust-lang/lang
Judging from the feature message, nowadays slices seem to be allowed by I am not entirely sure why these casts are disallowed only in |
In fact while trying to write a gate test for #84310, I was unable to find any example of a piece of code that compiles once @oli-obk If we just removed this |
Ah I found something const fn test() {
let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
} I am not sure why this is gated. It's not a slice directly, but it's also not a lot more complicated. |
Slices are OK, trait objects aren't. If the same code snippet but with trait objects fails even with the feature gate, just removing that check is fine by me (needs stab report and lang team approval on the stab PR) |
Yeah, there is a rather aggressive check for |
@rust-lang/lang I would like to propose stabilizing this feature. Stabilization report
const fn test() {
let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
} Unsizing casts where the type itself has a slice as "unsized tail" were already allowed (since #73862), but when the type is a pointer to something that has a slice as unsized tail, it was forbidden. By stabilizing #85078 turns |
FCP started on #85078. |
Dropping nomination, FCP proceeding smoothly. |
Right now we don't allow unsizing casts (array -> slice or type -> dyn Trait) in const fns on stable.
While we can't allow unsizing to
dyn Trait
until we've figured out trait bounds in const fns, we can easily allow unsizing casts for slices, there's no reason not to have them. The only reason we didn't have them in the initial min_const_fn feature gate was the fact that we preferred overly aggressive rules over accidental stabilization.The relevant code is
rust/src/librustc_mir/transform/qualify_min_const_fn.rs
Lines 162 to 165 in dc45735
We need to check the destination and source types for slice and array respectively and permit the cast in that case. We also need to recurse with
check_operand
on the value that is being casted.The text was updated successfully, but these errors were encountered: