Skip to content

Conversation

@BoxyUwU
Copy link
Member

@BoxyUwU BoxyUwU commented Nov 20, 2025

r? oli-obk

tracking issue: #132980

Syntactically distinguishing anon consts

Implementation wise the first thing this PR does is require that anon const const args are syntactically distinguishable from other kinds of args under mgca. We now use const { ... } in const argument position to denote an anon const:

#![feature(min_generic_const_args)]

// no longer allowed as `1 + 1` is represented via an anon const and
// there is no syntactic marker
type Foo = [(); 1 + 1];

// allowed, `const { ... }` indicates an anon const representation
type Foo = [(); const { 1 + 1 }];

This restriction is only placed when mgca is enabled. There should be no effect on stable.

This restriction allows us to create DefIds for anon consts only when actually required. When it is syntactically ambiguous whether a const argument is an anon const or not we are forced to conservatively create a DefId for every const argument even if it doesn't wind up needing one.

This works fine on stable but under mgca we can wind up with anon consts nested inside non-anon-const const arguments resulting in a broken DefId tree. See #148838 where an anon const arg inside of a path arg winds up with a parent of a conservatively created DefId that doesn't actually correspond to an anon const, resulting in an ICE.

With #149114 every field initialiser in a const argument would become a place where there could possibly be an anon const. This would also get worse once we support tuple constructors- now every function argument is a place where there could possibly be an anon const.

We introduce this restriction to avoid creating massive amounts of unused DefIds that make the parent tree significantly more complicated, and to avoid having to paper over this issue in things like generics_of.

Fixes #148838

It also must be syntactically clear from context whether '_ means an inference lifetime or an elided lifetime parameter. This restriction will allow us to properly resolve '_ in const arguments in mgca. This PR doesn't actually fix handle this, but we could do so trivially after this lands.

@rustbot
Copy link
Collaborator

rustbot commented Nov 20, 2025

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Nov 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 20, 2025

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
45 
+ error: generic parameters may not be used in const operations
+   --> $DIR/explicit_anon_consts.rs:64:58
+    |
+ LL | struct Default3<const N: usize, const M: usize = const { N }>;
+    |                                                          ^ cannot perform const operation using `N`
+    |
+    = help: const parameters may only be used as standalone arguments here, i.e. `N`
+    = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
+ 
46 error: complex const arguments must be placed inside of a `const` block
---
76 error: complex const arguments must be placed inside of a `const` block
-   --> $DIR/explicit_anon_consts.rs:67:50
+   --> $DIR/explicit_anon_consts.rs:66:50
78    |
79 LL | struct Default4<const N: usize, const M: usize = { 1 + 1 }>;
80    |                                                  ^^^^^^^^^

81 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:8:41
-    |
- LL | type Adt3<const N: usize> = Foo<const { N }>;
-    |                                         ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:17:42
-    |
- LL | type Arr3<const N: usize> = [(); const { N }];
-    |                                          ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:38:46
-    |
- LL | const ITEM3<const N: usize>: usize = const { N };
-    |                                              ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:56:31
-    |
- LL |     T3: Trait<ASSOC = const { N }>,
-    |                               ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:65:58
-    |
- LL | struct Default3<const N: usize, const M: usize = const { N }>;
-    |                                                          ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:26:27
-    |
- LL |     let _3 = [(); const { N }];
-    |                           ^
- 
- error: generic parameters may not be used in const operations
-   --> $DIR/explicit_anon_consts.rs:8:41
-    |
- LL | type Adt3<const N: usize> = Foo<const { N }>;
-    |                                         ^
-    |
-    = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
- 
- error: aborting due to 13 previous errors
+ error: aborting due to 12 previous errors
127 
128 

Note: some mismatched output was normalized before being compared
-   --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:64:58
-   --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:66:50
+ error: generic parameters may not be used in const operations
+   --> $DIR/explicit_anon_consts.rs:64:58
+    |
+ LL | struct Default3<const N: usize, const M: usize = const { N }>;
+    |                                                          ^ cannot perform const operation using `N`
+    |
+    = help: const parameters may only be used as standalone arguments here, i.e. `N`
+    = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
+ 
+   --> $DIR/explicit_anon_consts.rs:66:50
---
To only update this specific test, also pass `--test-args const-generics/mgca/explicit_anon_consts.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/const-generics/mgca/explicit_anon_consts" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:8:41
   |
LL | type Adt3<const N: usize> = Foo<const { N }>;
   |                                         ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:16:42
   |
LL | type Arr3<const N: usize> = [(); const { N }];
   |                                          ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:25:27
   |
LL |     let _3 = [(); const { N }];
   |                           ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:37:46
   |
LL | const ITEM3<const N: usize>: usize = const { N };
   |                                              ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:55:31
   |
LL |     T3: Trait<ASSOC = const { N }>,
   |                               ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:64:58
   |
LL | struct Default3<const N: usize, const M: usize = const { N }>;
   |                                                          ^ cannot perform const operation using `N`
   |
   = help: const parameters may only be used as standalone arguments here, i.e. `N`
   = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:10:33
   |
LL | type Adt4<const N: usize> = Foo<{ 1 + 1 }>;
   |                                 ^^^^^^^^^

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:18:34
   |
LL | type Arr4<const N: usize> = [(); 1 + 1];
   |                                  ^^^^^

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:27:19
   |
LL |     let _4 = [(); 1 + 1];
   |                   ^^^^^

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:40:38
   |
LL | const ITEM4<const N: usize>: usize = { 1 + 1 };
   |                                      ^^^^^^^^^

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:57:23
   |
LL |     T4: Trait<ASSOC = { 1 + 1 }>,
   |                       ^^^^^^^^^

error: complex const arguments must be placed inside of a `const` block
##[error]  --> /checkout/tests/ui/const-generics/mgca/explicit_anon_consts.rs:66:50
   |
LL | struct Default4<const N: usize, const M: usize = { 1 + 1 }>;
   |                                                  ^^^^^^^^^

error: aborting due to 12 previous errors
------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE: generics_of: unexpected node kind ConstArg(ConstArg

4 participants