Skip to content

Decide about future of rustc_legacy_const_generics #146613

Description

@traviscross

Back in 2021, we converted stdarch intrinsics to use const generics:

However, we seem to have left open the question of how strongly we want to deprecate rustc_legacy_const_generics and whether it's appropriate to use rustc_legacy_const_generics on new APIs.

Following guidance (in #83167 (comment)) related to the experience for people on older MSRVs, our documentation still points people toward this "legacy" syntax (e.g.).

On the other hand, we recently made this legacy syntax significantly less functional than the const generic equivalent in:

Our discussion on that proceeded as follows (2024-10-30):

TC: This proposes to make an error out of:

std::arch::x86_64::_mm_blend_ps(loop {}, loop {}, foo::<3>());

...and similar cases that lean on legacy_const_generics.

This is not believed to break anything in practice and is being done for implementation reasons.

TC: This is marked as an easy decision for us. Do we agree?

Josh: We've thought about trying to make this a first-class feature. How difficult are the bugs in question to fix?

CE: If we ever wanted this as a first-class feature, we'd need to tear this out anyway and reimplement it. What's in the compiler now cannot architecturally support extending this feature more generally.

NM: We may in fact want this first-class feature. But I would want it to be approached as an equality constraint being introduced, not a "rewrite".

CE: It could certainly be done; just not how it was done.

TC: This is now in FCP.

For my part, I had taken from our decision and discussion that we mean to phase out as much as possible of what relies on the current implementation and concept of rustc_legacy_const_generics while at the same time being open to proposals for how we might approach this in a more appealing way. However, given the open questions we had left in 2021, it seems less clear to me now what our plan is.

Perhaps without noticing, we recently stabilized the AVX512 intrinsics with rustc_legacy_const_generics:

@RalfJung has proposed that, maybe, as this only went out in Rust 1.89, we could see about clawing this back. Conversely, there's a proposal, for consistently, to extend rustc_legacy_const_generics to all intrinsics with const generic parameters:

What do we want to do? The questions for us include:

  • Should rustc_legacy_const_generics be applied to new APIs? If not, should we mark it as "deprecated"?
  • Should rustc_legacy_const_generics be applied to existing APIs for consistency?
  • Should we see if we can claw back rustc_legacy_const_generics from AVX512 intrinsics?
  • Should we be lang FCPing the PRs for intrinsic stabilizations such as AVX512 so as to maybe catch this sort of thing? (We only FCPed the AVX512 target feature.)
  • Should rustdoc be suggesting the legacy syntax or the const generic syntax to people?
  • Do we have appetite to see about issuing FCWs or moving people off of rustc_legacy_const_generics over an edition?

cc @RalfJung @BoxyUwU @Amanieu @sayantn @rust-lang/project-const-generics @rust-lang/wg-const-eval @rust-lang/lang

Activity

  1. added
    T-langRelevant to the language team
    C-discussionCategory: Discussion or questions that doesn't represent real issues.
    I-lang-nominatedNominated for discussion during a lang team meeting.
    I-lang-radarItems that are on lang's radar and will need eventual work or consideration.
    P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang
    on Sep 15, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 15, 2025
  3. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 15, 2025
  4. RalfJung commented on Sep 16, 2025

    @RalfJung
    Member

    FWIW I fully agree that ergonomically speaking, the legacy syntax is much nicer. OTOH, from what I understood, we currently can't really support syntax in the compiler where it is not syntactically obvious (before name resolution!) whether a term is a constant or a normal runtime expression. That's what leads to odd issues such as #130443. (Well, we do have promotion. But that has its own set of curses and the decision is made based on the expression itself, not its surrounding context.)

    But what we have now is really strange -- an ad-hoc half-supported compiler hack with very special behavior seen nowhere else in the language that stdarch and nobody else gets to use.

  5. BoxyUwU commented on Sep 16, 2025

    @BoxyUwU
    Member

    More than just "before name res" it also only works in cross-crate scenarios- you can't use legacy const generics call syntax from within the same crate as the function.

    While I do agree that its more ergonomic- that's really a thing for lang to go figure out how to make generic arguments more ergonomic. This is far from a problem with const generics, it's also annoying to turbofish type arguments.

    I'm reminded of a convo a while ago in the T-lang zulip streams about a dedicated feature for "compile time function arguments" that was distinct from const generics. I can't remember what the outcome of that was.

  6. nbdd0121 commented on Sep 21, 2025

    @nbdd0121
    Member

    I think having const function arguments is desirable. This was heavily desirable feature that RfL want for example (e.g. for compile-time bound checking in https://rust.docs.kernel.org/kernel/io/struct.Io.html#method.write8). It is also something that we want for kernel atomics (for Ordering).

    I'd wish if we evolve the language so that the "legacy" syntax becomes a new language feature.

  7. joshtriplett commented on Oct 8, 2025

    @joshtriplett
    Member

    I think having const function arguments is desirable. This was heavily desirable feature that RfL want for example (e.g. for compile-time bound checking in https://rust.docs.kernel.org/kernel/io/struct.Io.html#method.write8). It is also something that we want for kernel atomics (for Ordering).

    That's a really good point! Considering we want this for constants, I wonder to what extent a combination of a const function argument, an inline function, and const if or similar, would be sufficient to solve this?

    OTOH, from what I understood, we currently can't really support syntax in the compiler where it is not syntactically obvious (before name resolution!) whether a term is a constant or a normal runtime expression.

    Yeah, if we added something like const function arguments, I'd expect that to require anything more complex than 42 or Enum::Variant to be put in an explicit const block.

  8. added
    P-lang-drag-3Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.
    and removed
    P-lang-drag-1Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang
    on Oct 15, 2025
  9. RalfJung commented on Oct 17, 2025

    @RalfJung
    Member

    @BoxyUwU

    rustc_legacy_const_generics shouldn't be added to any newly added functions, we should go further and make it a hard error to rely on as a caller on newer editions. this syntax is more limited than actually providing the generic argument as a generic argument and is very much a backwards compatibility hack not a feature

    If we lint against all uses of the attribute except those that are syntactically already constants (literals, paths, and const { ... } blocks), would that work? Currently, a const block as such an argument does trigger the "invalid argument to a legacy const generic" lint; is that truly needed for something that's already clearly identifiable as a constant?

  10. Amanieu commented on Dec 4, 2025

    @Amanieu
    Member

    Proposal for allowing the stabilization of new rustc_legacy_const_generics intrinsics only for targets which already have such intrinsics on stable: #149654

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

    C-discussionCategory: Discussion or questions that doesn't represent real issues.I-lang-nominatedNominated for discussion during a lang team meeting.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.P-lang-drag-3Lang team prioritization drag level 3.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions