Skip to content

Tracking issue for unsizing casts in const fns #64992

Closed

Description

@oli-obk

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

Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => Err((
span,
"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.

Activity

  1. added
    C-tracking-issue
    on Oct 2, 2019
  2. added
    T-lang
    on Oct 2, 2019
  3. added
    B-unstable
    on Nov 26, 2019
  4. added 4 commits that reference this issue on Jul 10, 2020
  5. RalfJung commented on Apr 18, 2021

    @RalfJung
    Member

    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?

  6. RalfJung commented on Apr 19, 2021

    @RalfJung
    Member

    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?

  7. RalfJung commented on Apr 19, 2021

    @RalfJung
    Member

    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.

  8. oli-obk commented on Apr 22, 2021

    @oli-obk
    ContributorAuthor

    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)

  9. RalfJung commented on Apr 22, 2021

    @RalfJung
    Member

    Yeah, there is a rather aggressive check for dyn Trait appearing anywhere that triggers "trait bounds other than Sized are unstable".

  10. RalfJung commented on May 8, 2021

    @RalfJung
    Member

    @rust-lang/lang I would like to propose stabilizing this feature.

    Stabilization report

    const_fn_unsize prevents certain unsizing casts from happening in const fn (they were already allowed in const/static bodies):

    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 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.

  11. joshtriplett commented on May 11, 2021

    @joshtriplett
    Member

    FCP started on #85078.

  12. Mark-Simulacrum commented on May 18, 2021

    @Mark-Simulacrum
    Member

    Dropping nomination, FCP proceeding smoothly.

  13. added a commit that references this issue on May 22, 2021
  14. added
    A-const-eval
    on Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalB-unstableC-tracking-issueT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions