Repository: rust-lang/rust
Pull request: mGCA: Lower const generic args to infer when needed

close: rust-lang/rust#153198

r? BoxyUwU

Changed files:
filename: compiler/rustc_ast_lowering/src/lib.rs
patch: @@ -1310,9 +1310,13 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
                 }
                 GenericArg::Type(self.lower_ty_alloc(ty, itctx).try_as_ambig_ty().unwrap())
             }
-            ast::GenericArg::Const(ct) => GenericArg::Const(
-                self.lower_anon_const_to_const_arg_and_alloc(ct).try_as_ambig_ct().unwrap(),
-            ),
+            ast::GenericArg::Const(ct) => {
+                let ct = self.lower_anon_const_to_const_arg_and_alloc(ct);
+                match ct.try_as_ambig_ct() {
+                    Some(ct) => GenericArg::Const(ct),
+                    None => GenericArg::Infer(hir::InferArg { hir_id: ct.hir_id, span: ct.span }),
+                }
+            }
         }
     }
 

filename: tests/ui/const-generics/mgca/braced-const-infer.rs
patch: @@ -0,0 +1,9 @@
+//! Regression test for https://github.com/rust-lang/rust/issues/153198
+#![feature(min_generic_const_args)]
+#![allow(incomplete_features, rust_2021_compatibility)]
+
+trait Trait<T> {}
+
+impl dyn Trait<{_}> {} //~ ERROR: the placeholder `_` is not allowed within types on item signatures
+
+fn main() {}

filename: tests/ui/const-generics/mgca/braced-const-infer.stderr
patch: @@ -0,0 +1,9 @@
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for implementations
+  --> $DIR/braced-const-infer.rs:7:17
+   |
+LL | impl dyn Trait<{_}> {}
+   |                 ^ not allowed in type signatures
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0121`.

filename: tests/ui/const-generics/mgca/macro-const-arg-infer.rs
patch: @@ -0,0 +1,20 @@
+//! Regression test for https://github.com/rust-lang/rust/issues/153198
+#![feature(min_generic_const_args)]
+#![allow(incomplete_features)]
+macro_rules! y {
+    ( $($matcher:tt)*) => {
+        _ //~ ERROR: the placeholder `_` is not allowed within types on item signatures
+    };
+}
+
+struct A<T>; //~ ERROR: type parameter `T` is never used
+
+const y: A<
+    {
+        y! {
+            x
+        }
+    },
+> = 1; //~ ERROR: mismatched types
+
+fn main() {}

filename: tests/ui/const-generics/mgca/macro-const-arg-infer.stderr
patch: @@ -0,0 +1,44 @@
+error[E0392]: type parameter `T` is never used
+  --> $DIR/macro-const-arg-infer.rs:10:10
+   |
+LL | struct A<T>;
+   |          ^ unused type parameter
+   |
+   = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
+   = help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
+
+error[E0308]: mismatched types
+  --> $DIR/macro-const-arg-infer.rs:18:5
+   |
+LL |   const y: A<
+   |  __________-
+LL | |     {
+LL | |         y! {
+LL | |             x
+LL | |         }
+LL | |     },
+LL | | > = 1;
+   | | -   ^ expected `A<_>`, found integer
+   | |_|
+   |   expected because of the type of the constant
+   |
+   = note: expected struct `A<_>`
+                found type `{integer}`
+
+error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
+  --> $DIR/macro-const-arg-infer.rs:6:9
+   |
+LL |           _
+   |           ^ not allowed in type signatures
+...
+LL | /         y! {
+LL | |             x
+LL | |         }
+   | |_________- in this macro invocation
+   |
+   = note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 3 previous errors
+
+Some errors have detailed explanations: E0121, E0308, E0392.
+For more information about an error, try `rustc --explain E0121`.