Skip to content

Commit 8f00b1f

Browse files
committed
Allow parenthesis around inferred array lengths
1 parent 69b3959 commit 8f00b1f

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20342034
}
20352035

20362036
fn lower_array_length_to_const_arg(&mut self, c: &AnonConst) -> &'hir hir::ConstArg<'hir> {
2037-
match c.value.kind {
2037+
// We cannot just match on `ExprKind::Underscore` as `(_)` is represented as
2038+
// `ExprKind::Paren(ExprKind::Underscore)` and should also be lowered to `GenericArg::Infer`
2039+
match c.value.peel_parens().kind {
20382040
ExprKind::Underscore => {
20392041
if !self.tcx.features().generic_arg_infer() {
20402042
feature_err(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0658]: const arguments cannot yet be inferred with `_`
2+
--> $DIR/parend_infer.rs:24:16
3+
|
4+
LL | let c: Foo<_> = Foo::<1>;
5+
| ^
6+
|
7+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
8+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: const arguments cannot yet be inferred with `_`
12+
--> $DIR/parend_infer.rs:26:16
13+
|
14+
LL | let c: Foo<(_)> = Foo::<1>;
15+
| ^^^
16+
|
17+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
18+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: const arguments cannot yet be inferred with `_`
22+
--> $DIR/parend_infer.rs:28:16
23+
|
24+
LL | let c: Foo<(((_)))> = Foo::<1>;
25+
| ^^^^^^^
26+
|
27+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
28+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: using `_` for array lengths is unstable
32+
--> $DIR/parend_infer.rs:17:17
33+
|
34+
LL | let b: [u8; (_)] = [1; (((((_)))))];
35+
| ^^^
36+
|
37+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
38+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error[E0658]: using `_` for array lengths is unstable
42+
--> $DIR/parend_infer.rs:17:28
43+
|
44+
LL | let b: [u8; (_)] = [1; (((((_)))))];
45+
| ^^^^^^^^^^^
46+
|
47+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
48+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error: aborting due to 5 previous errors
52+
53+
For more information about this error, try `rustc --explain E0658`.
+19-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
//@ check-pass
1+
//@[gate] check-pass
22
//@ revisions: gate nogate
33
#![cfg_attr(gate, feature(generic_arg_infer))]
44

5+
struct Foo<const N: usize>;
6+
57
fn main() {
68
// AST Types preserve parens for pretty printing reasons. This means
79
// that this is parsed as a `TyKind::Paren(TyKind::Infer)`. Generic
810
// arg lowering therefore needs to take into account not just `TyKind::Infer`
911
// but `TyKind::Infer` wrapped in arbitrarily many `TyKind::Paren`.
1012
let a: Vec<(_)> = vec![1_u8];
1113
let a: Vec<(((((_)))))> = vec![1_u8];
14+
15+
// AST Exprs similarly preserve parens for pretty printing reasons.
16+
#[rustfmt::skip]
17+
let b: [u8; (_)] = [1; (((((_)))))];
18+
//[nogate]~^ error: using `_` for array lengths is unstable
19+
//[nogate]~| error: using `_` for array lengths is unstable
20+
let b: [u8; 2] = b;
21+
22+
// This is the same case as AST types as the parser doesn't distinguish between const
23+
// and type args when they share syntax
24+
let c: Foo<_> = Foo::<1>;
25+
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
26+
let c: Foo<(_)> = Foo::<1>;
27+
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
28+
let c: Foo<(((_)))> = Foo::<1>;
29+
//[nogate]~^ error: const arguments cannot yet be inferred with `_`
1230
}

0 commit comments

Comments
 (0)