You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for unsizing casts in const fns #64992
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team
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.
"unsizing casts are not allowed in const fn".into(),
)),
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.
Judging from the feature message, nowadays slices seem to be allowed by dyn unsizing is still disallowed -- but only in const fn. We also have no tests using this feature (just some compile-fail tests checking the feature gate).
I am not entirely sure why these casts are disallowed only in const fn, but I assume it is a matter of backwards compatibility. However, this means we already have to be sure that the corresponding Miri code is right, so -- what is blocking us from simply removing this feature gate, and allowing the corresponding code?
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 const_fn_unsize is allowed. I always then additionally get the error about "trait bounds other than Sized".
@oli-obk If we just removed this UnsizingCast in check_consts/ops.rs and all its call sites, is there any additional code that would be accepted?
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)
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 const_fn_unsize, we allow code like the above. The CTFE engine has supported such code for a long time (and this is battle-tested in standalone Miri), so I see little risk here.
#85078 turns src/test/ui/consts/const_fn_unsize.rs into a test checking correct functionality of this feature.
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team
Activity
Rollup merge of rust-lang#73862 - oli-obk:const_array_to_slice, r=Ral…
Rollup merge of rust-lang#73862 - oli-obk:const_array_to_slice, r=Ral…
Rollup merge of rust-lang#73862 - oli-obk:const_array_to_slice, r=Ral…
Rollup merge of rust-lang#73862 - oli-obk:const_array_to_slice, r=Ral…
RalfJung commentedon Apr 18, 2021
Judging from the feature message, nowadays slices seem to be allowed by
dynunsizing is still disallowed -- but only inconst fn. We also have no tests using this feature (just some compile-fail tests checking the feature gate).I am not entirely sure why these casts are disallowed only in
const fn, but I assume it is a matter of backwards compatibility. However, this means we already have to be sure that the corresponding Miri code is right, so -- what is blocking us from simply removing this feature gate, and allowing the corresponding code?RalfJung commentedon Apr 19, 2021
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
const_fn_unsizeis allowed. I always then additionally get the error about "trait bounds other thanSized".@oli-obk If we just removed this
UnsizingCastincheck_consts/ops.rsand all its call sites, is there any additional code that would be accepted?RalfJung commentedon Apr 19, 2021
Ah I found something
I am not sure why this is gated. It's not a slice directly, but it's also not a lot more complicated.
oli-obk commentedon Apr 22, 2021
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)
RalfJung commentedon Apr 22, 2021
Yeah, there is a rather aggressive check for
dyn Traitappearing anywhere that triggers "trait bounds other thanSizedare unstable".RalfJung commentedon May 8, 2021
@rust-lang/lang I would like to propose stabilizing this feature.
Stabilization report
const_fn_unsizeprevents certain unsizing casts from happening inconst fn(they were already allowed inconst/staticbodies):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
const_fn_unsize, we allow code like the above. The CTFE engine has supported such code for a long time (and this is battle-tested in standalone Miri), so I see little risk here.#85078 turns
src/test/ui/consts/const_fn_unsize.rsinto a test checking correct functionality of this feature.joshtriplett commentedon May 11, 2021
FCP started on #85078.
Mark-Simulacrum commentedon May 18, 2021
Dropping nomination, FCP proceeding smoothly.
Auto merge of rust-lang#85078 - RalfJung:const_fn_unsize, r=oli-obk