From 15d08109c4ab3414ad761aba86f61e59566d8092 Mon Sep 17 00:00:00 2001 From: Boxy Uwu Date: Wed, 9 Sep 2026 16:04:34 +0100 Subject: [PATCH] split `macroless_generic_const_args` in two --- compiler/rustc_ast_lowering/src/lib.rs | 2 +- compiler/rustc_feature/src/unstable.rs | 4 + compiler/rustc_span/src/symbol.rs | 1 + ...macroless-const-item-generic-const-args.md | 74 +++++++++++++++++++ .../macroless-generic-const-args.md | 6 +- ...onst-pattern-field-type-mismatch-162394.rs | 1 + ...-pattern-field-type-mismatch-162394.stderr | 2 +- .../gca/non-type-equality-ok.rs | 1 + ...st_item_generic_const_args.macroful.stderr | 11 +++ ...macroless_const_item_generic_const_args.rs | 28 +++++++ 10 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/doc/unstable-book/src/language-features/macroless-const-item-generic-const-args.md create mode 100644 tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.macroful.stderr create mode 100644 tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.rs diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index a27dc47bf27..09ef6abeb3e 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2685,7 +2685,7 @@ impl<'hir> LoweringContext<'_, 'hir> { match (body, kind) { (body, ConstItemKind::Body) => { let is_direct = |body| { - if self.tcx.features().macroless_generic_const_args() { + if self.tcx.features().macroless_const_item_generic_const_args() { self.can_lower_expr_to_const_arg_direct( body, DirectConstArgContext::MacrolessMinGenericConstArgs, diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs index ca703898151..7d495b3262b 100644 --- a/compiler/rustc_feature/src/unstable.rs +++ b/compiler/rustc_feature/src/unstable.rs @@ -603,6 +603,9 @@ declare_features! ( (unstable, macro_metavar_expr, "1.61.0", Some(83527)), /// Provides a way to concatenate identifiers using metavariable expressions. (unstable, macro_metavar_expr_concat, "1.81.0", Some(124225)), + /// Allows directly represented generic_const_args as the rhs of const items without the + /// `direct_const_arg!` macro. + (incomplete, macroless_const_item_generic_const_args, "CURRENT_RUSTC_VERSION", Some(162540)), /// Allows directly represented generic_const_args without the `direct_const_arg!` macro. (incomplete, macroless_generic_const_args, "1.99.0", Some(159006)), /// Allows `#[marker]` on certain traits allowing overlapping implementations. @@ -854,5 +857,6 @@ pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] = &[ pub const DEPENDENT_FEATURES: &[(Symbol, &[Symbol])] = &[ (sym::generic_const_args, &[sym::min_generic_const_args]), (sym::macroless_generic_const_args, &[sym::min_generic_const_args]), + (sym::macroless_const_item_generic_const_args, &[sym::min_generic_const_args]), (sym::unsized_const_params, &[sym::adt_const_params]), ]; diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c8fa04e08f7..f3135fefe8d 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1252,6 +1252,7 @@ symbols! { macro_reexport, macro_use, macro_vis_matcher, + macroless_const_item_generic_const_args, macroless_generic_const_args, macros_in_extern, main, diff --git a/src/doc/unstable-book/src/language-features/macroless-const-item-generic-const-args.md b/src/doc/unstable-book/src/language-features/macroless-const-item-generic-const-args.md new file mode 100644 index 00000000000..d208d324322 --- /dev/null +++ b/src/doc/unstable-book/src/language-features/macroless-const-item-generic-const-args.md @@ -0,0 +1,74 @@ +# macroless_generic_const_args + +Enables implementing const items under `#![feature(min_generic_const_args)]` and `#![feature(generic_const_args)]` without the `direct_const_arg!` macro. + +The tracking issue for this feature is: [#162540] + +[#162540]: https://github.com/rust-lang/rust/issues/162540 + +------------------------ + +Warning: This feature is incomplete; its design and syntax may change. + +Related features: +- [min_generic_const_args]. See that doc for what the `direct_const_arg!` is. This feature enables +support for directly represented const arguments as the rhs of const items without the macro. +- [macroless_generic_const_args]. For a version of this feature that works for const arguments in other +positions + +[min_generic_const_args]: min-generic-const-args.md +[macroless_generic_const_args]: macroless-generic-const-args.md + +## Examples + +Here is an example from [min_generic_const_args]: + +[min_generic_const_args]: min-generic-const-args.md + +```rust,ignore (needs new solver) +#![allow(incomplete_features)] +#![feature( + min_generic_const_args, + generic_const_args, + macroless_generic_const_args, + generic_const_items, +)] + +trait Trait { + const ASSOC: usize; +} + +impl Trait for () { + const ASSOC: usize = core::direct_const_arg!(N); +} + +fn foo() { + let a: [(); <() as Trait>::ASSOC::] + = [(); N]; +} +``` + +Using `#![feature(macroless_const_item_generic_const_args)]` enables you to write the above without the macro: + +```rust,ignore (needs new solver) +#![allow(incomplete_features)] +#![feature( + min_generic_const_args, + generic_const_args, + macroless_generic_const_args, + generic_const_items, +)] + +trait Trait { + const ASSOC: usize; +} + +impl Trait for () { + const ASSOC: usize = N; +} + +fn foo() { + let a: [(); <() as Trait>::ASSOC::] + = [(); N]; +} +``` diff --git a/src/doc/unstable-book/src/language-features/macroless-generic-const-args.md b/src/doc/unstable-book/src/language-features/macroless-generic-const-args.md index 18f4e5cd97a..6b1205a006d 100644 --- a/src/doc/unstable-book/src/language-features/macroless-generic-const-args.md +++ b/src/doc/unstable-book/src/language-features/macroless-generic-const-args.md @@ -10,10 +10,14 @@ The tracking issue for this feature is: [#159006] Warning: This feature is incomplete; its design and syntax may change. -Related features: [min_generic_const_args]. See that doc for what the `direct_const_arg!` is. This feature enables +Related features: +- [min_generic_const_args]. See that doc for what the `direct_const_arg!` is. This feature enables support for directly represented const arguments without the macro. +- [macroless_const_item_generic_const_args]. For a version of this feature that works for const arguments +as the right hand side of a const item. [min_generic_const_args]: min-generic-const-args.md +[macroless_const_item_generic_const_args]: macroless-const-item-generic-const-args.md ## Examples diff --git a/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.rs b/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.rs index 9557c9da807..181693c3652 100644 --- a/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.rs +++ b/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.rs @@ -2,6 +2,7 @@ #![allow(incomplete_features)] #![feature(macroless_generic_const_args)] +#![feature(macroless_const_item_generic_const_args)] #![feature(generic_const_args, min_generic_const_args)] #![feature(min_adt_const_params)] diff --git a/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.stderr b/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.stderr index afa00eaeed7..9dd229cc46a 100644 --- a/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.stderr +++ b/tests/ui/const-generics/gca/const-pattern-field-type-mismatch-162394.stderr @@ -1,5 +1,5 @@ error: the constant `"foo"` is not of type `()` - --> $DIR/const-pattern-field-type-mismatch-162394.rs:16:5 + --> $DIR/const-pattern-field-type-mismatch-162394.rs:17:5 | LL | const A2: Foo = Self::FooA("foo"); | ^^^^^^^^^^^^^ expected `()`, found `&'static str` diff --git a/tests/ui/const-generics/gca/non-type-equality-ok.rs b/tests/ui/const-generics/gca/non-type-equality-ok.rs index 45dcef1f2dc..ae7c069ea55 100644 --- a/tests/ui/const-generics/gca/non-type-equality-ok.rs +++ b/tests/ui/const-generics/gca/non-type-equality-ok.rs @@ -4,6 +4,7 @@ #![feature( min_generic_const_args, macroless_generic_const_args, + macroless_const_item_generic_const_args, generic_const_args, generic_const_items )] diff --git a/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.macroful.stderr b/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.macroful.stderr new file mode 100644 index 00000000000..d49f65acd5a --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.macroful.stderr @@ -0,0 +1,11 @@ +error[E0308]: mismatched types + --> $DIR/feature-gate-macroless_const_item_generic_const_args.rs:24:11 + | +LL | let a: [(); <() as Trait>::ASSOC::] + | ------------------------------- expected due to this +LL | = [(); N]; + | ^^^^^^^ expected an array with a size of <() as Trait>::ASSOC::, found one with a size of N + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.rs b/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.rs new file mode 100644 index 00000000000..a967dffcd39 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-macroless_const_item_generic_const_args.rs @@ -0,0 +1,28 @@ +//@ revisions: macroless macroful +//@[macroless] check-pass +//@ compile-flags: -Znext-solver + +#![feature( + min_generic_const_args, + generic_const_args, + macroless_generic_const_args, + generic_const_items, +)] + +#![cfg_attr(macroless, feature(macroless_const_item_generic_const_args))] + +trait Trait { + const ASSOC: usize; +} + +impl Trait for () { + const ASSOC: usize = N; +} + +fn foo() { + let a: [(); <() as Trait>::ASSOC::] + = [(); N]; + //[macroful]~^ ERROR: mismatched types +} + +fn main() {}