Allow parenthesis around inferred array lengths

This commit is contained in:
Boxy 2025-04-10 17:48:33 +01:00
parent 69b3959afe
commit 8f00b1fdad
3 changed files with 75 additions and 2 deletions

View file

@ -2034,7 +2034,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
fn lower_array_length_to_const_arg(&mut self, c: &AnonConst) -> &'hir hir::ConstArg<'hir> {
match c.value.kind {
// We cannot just match on `ExprKind::Underscore` as `(_)` is represented as
// `ExprKind::Paren(ExprKind::Underscore)` and should also be lowered to `GenericArg::Infer`
match c.value.peel_parens().kind {
ExprKind::Underscore => {
if !self.tcx.features().generic_arg_infer() {
feature_err(

View file

@ -0,0 +1,53 @@
error[E0658]: const arguments cannot yet be inferred with `_`
--> $DIR/parend_infer.rs:24:16
|
LL | let c: Foo<_> = Foo::<1>;
| ^
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: const arguments cannot yet be inferred with `_`
--> $DIR/parend_infer.rs:26:16
|
LL | let c: Foo<(_)> = Foo::<1>;
| ^^^
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: const arguments cannot yet be inferred with `_`
--> $DIR/parend_infer.rs:28:16
|
LL | let c: Foo<(((_)))> = Foo::<1>;
| ^^^^^^^
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: using `_` for array lengths is unstable
--> $DIR/parend_infer.rs:17:17
|
LL | let b: [u8; (_)] = [1; (((((_)))))];
| ^^^
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: using `_` for array lengths is unstable
--> $DIR/parend_infer.rs:17:28
|
LL | let b: [u8; (_)] = [1; (((((_)))))];
| ^^^^^^^^^^^
|
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -1,7 +1,9 @@
//@ check-pass
//@[gate] check-pass
//@ revisions: gate nogate
#![cfg_attr(gate, feature(generic_arg_infer))]
struct Foo<const N: usize>;
fn main() {
// AST Types preserve parens for pretty printing reasons. This means
// that this is parsed as a `TyKind::Paren(TyKind::Infer)`. Generic
@ -9,4 +11,20 @@ fn main() {
// but `TyKind::Infer` wrapped in arbitrarily many `TyKind::Paren`.
let a: Vec<(_)> = vec![1_u8];
let a: Vec<(((((_)))))> = vec![1_u8];
// AST Exprs similarly preserve parens for pretty printing reasons.
#[rustfmt::skip]
let b: [u8; (_)] = [1; (((((_)))))];
//[nogate]~^ error: using `_` for array lengths is unstable
//[nogate]~| error: using `_` for array lengths is unstable
let b: [u8; 2] = b;
// This is the same case as AST types as the parser doesn't distinguish between const
// and type args when they share syntax
let c: Foo<_> = Foo::<1>;
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
let c: Foo<(_)> = Foo::<1>;
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
let c: Foo<(((_)))> = Foo::<1>;
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
}