Skip to content

MGCA: Support literals as direct const arguments #150618

Closed

Description

@BoxyUwU

Currently we have a parser special case under MGCA that Foo::<1> is allowed without an explicit const block. E.g. normally we require Foo::<const { 1 + 1 }> for non-direct const arguments, but literals are special.

This special case only applies to the "top level" of a const argument, not to field initialisers of struct expressions or in the future element expressions of arrays/tuples or tuple constructors. It would maybe be possible to extend this special case to those positions but it would be involved and kind of annoying.

Instead I would like to support literals as direct const arguments without an anon const. ie a hir::ConstArgKind::Literal. This should let us clean up some of the parser code for mgca and make using the feature nicer as literals will be less boilerplate.

These should lower directly to a ConstKind::Value. However, this isn't possible if we dont have type information for what the literal is. This in theory is only a problem for integer and float literals. I expect we should:

  • Use the explicit disambiguator on the literal, ie 1_i32
  • Look at the type of the const parameter we're lowering the const argument for. E.g. struct Foo<const N: usize>; type Bar = Foo<1>; We can't normalize this type and it may be an inference variable so this doesn't work 100% of the time.

And if we can't get a type from there we can emit an error during HIR ty lowering to explicitly annotate the type. If we wanted to not require an explicit type this would motivate addition of a ConstKind::Expr(Expr::Literal, but I would like to avoid that if possible.

We may actually want to use this representation on stable to get rid of the perf optimization where we lower directly from a ConstArgKind::Anon to a ConstKind::Value. Not worth doing in the first implementation of this however as this will have the same issues as #149774.

Activity

  1. added
    E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.
    on Jan 2, 2026
  2. added
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    A-const-genericsArea: const generics (parameters and arguments)
    F-gca_min_const_items`#![feature(gca_min_const_items)]` (previously: `min_generic_const_args`)
    on Jan 2, 2026
  3. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 2, 2026
  4. removed
    E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.
    on Jan 2, 2026
  5. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 2, 2026
  6. Human9000-bit commented on Jan 3, 2026

    @Human9000-bit
    Member

    Seems interesting to me. I'll try to do this

    @rustbot claim

  7. BoxyUwU commented on Jan 4, 2026

    @BoxyUwU
    MemberAuthor

    The commits from when support for Struct expressions was added will probably be useful for you to use as a reference for how to go about implementing this:

    Feel free to reach out on zulip in the project-const-generics channel if you need any help, or just open a draft PR and assign me :)

  8. added 8 commits that reference this issue on Jan 8, 2026
  9. added a commit that references this issue on Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-const-genericsArea: const generics (parameters and arguments)E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.F-gca_min_const_items`#![feature(gca_min_const_items)]` (previously: `min_generic_const_args`)

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions