Repository navigation
Description
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
Human9000-bit commentedon Jan 3, 2026
Seems interesting to me. I'll try to do this
@rustbot claim
BoxyUwU commentedon Jan 4, 2026
The commits from when support for
Structexpressions 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-genericschannel if you need any help, or just open a draft PR and assign me :)Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Rollup merge of rust-lang#150699 - literals-as-direct-const-args, r=B…
Unrolled build for #150699
Rollup merge of #150699 - literals-as-direct-const-args, r=BoxyUwU