Skip to content

Commit bef48f9

Browse files
authored
Rollup merge of #101492 - TaKO8Ki:suggest-adding-array-length-to-ref-to-array, r=oli-obk
Suggest adding array lengths to references to arrays if possible ref: #100590 (review)
2 parents d9609c7 + 1e38442 commit bef48f9

File tree

5 files changed

+120
-29
lines changed

5 files changed

+120
-29
lines changed

compiler/rustc_hir/src/hir.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,14 @@ impl<'hir> Ty<'hir> {
24012401
_ => None,
24022402
}
24032403
}
2404+
2405+
pub fn peel_refs(&self) -> &Self {
2406+
let mut final_ty = self;
2407+
while let TyKind::Rptr(_, MutTy { ty, .. }) = &final_ty.kind {
2408+
final_ty = &ty;
2409+
}
2410+
final_ty
2411+
}
24042412
}
24052413

24062414
/// Not represented directly in the AST; referred to by name through a `ty_path`.

compiler/rustc_typeck/src/check/expr.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -1305,31 +1305,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13051305
}
13061306

13071307
fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
1308-
if let Some(parent_hir_id) = self.tcx.hir().find_parent_node(expr.hir_id) {
1309-
let ty = match self.tcx.hir().find(parent_hir_id) {
1310-
Some(
1311-
hir::Node::Local(hir::Local { ty: Some(ty), .. })
1312-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }),
1313-
) => Some(ty),
1314-
_ => None,
1315-
};
1316-
if let Some(ty) = ty
1317-
&& let hir::TyKind::Array(_, length) = ty.kind
1318-
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
1319-
&& let Some(span) = self.tcx.hir().opt_span(hir_id)
1320-
{
1321-
match self.tcx.sess.diagnostic().steal_diagnostic(span, StashKey::UnderscoreForArrayLengths) {
1322-
Some(mut err) => {
1323-
err.span_suggestion(
1324-
span,
1325-
"consider specifying the array length",
1326-
array_len,
1327-
Applicability::MaybeIncorrect,
1328-
);
1329-
err.emit();
1330-
}
1331-
None => ()
1308+
let parent_node = self.tcx.hir().parent_iter(expr.hir_id).find(|(_, node)| {
1309+
!matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
1310+
});
1311+
let Some((_,
1312+
hir::Node::Local(hir::Local { ty: Some(ty), .. })
1313+
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }))
1314+
) = parent_node else {
1315+
return
1316+
};
1317+
if let hir::TyKind::Array(_, length) = ty.peel_refs().kind
1318+
&& let hir::ArrayLen::Body(hir::AnonConst { hir_id, .. }) = length
1319+
&& let Some(span) = self.tcx.hir().opt_span(hir_id)
1320+
{
1321+
match self.tcx.sess.diagnostic().steal_diagnostic(span, StashKey::UnderscoreForArrayLengths) {
1322+
Some(mut err) => {
1323+
err.span_suggestion(
1324+
span,
1325+
"consider specifying the array length",
1326+
array_len,
1327+
Applicability::MaybeIncorrect,
1328+
);
1329+
err.emit();
13321330
}
1331+
None => ()
13331332
}
13341333
}
13351334
}

src/test/ui/array-slice-vec/suggest-array-length.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ fn main() {
55
const Foo: [i32; 3] = [1, 2, 3];
66
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
77
//~| ERROR using `_` for array lengths is unstable
8+
const REF_FOO: &[u8; 1] = &[1];
9+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
10+
//~| ERROR using `_` for array lengths is unstable
811
let foo: [i32; 3] = [1, 2, 3];
912
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
1013
//~| ERROR using `_` for array lengths is unstable
1114
let bar: [i32; 3] = [0; 3];
1215
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
1316
//~| ERROR using `_` for array lengths is unstable
17+
let ref_foo: &[i32; 3] = &[1, 2, 3];
18+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
19+
//~| ERROR using `_` for array lengths is unstable
20+
let ref_bar: &[i32; 3] = &[0; 3];
21+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
22+
//~| ERROR using `_` for array lengths is unstable
23+
let multiple_ref_foo: &&[i32; 3] = &&[1, 2, 3];
24+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
25+
//~| ERROR using `_` for array lengths is unstable
1426
}

src/test/ui/array-slice-vec/suggest-array-length.rs

+12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ fn main() {
55
const Foo: [i32; _] = [1, 2, 3];
66
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
77
//~| ERROR using `_` for array lengths is unstable
8+
const REF_FOO: &[u8; _] = &[1];
9+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
10+
//~| ERROR using `_` for array lengths is unstable
811
let foo: [i32; _] = [1, 2, 3];
912
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
1013
//~| ERROR using `_` for array lengths is unstable
1114
let bar: [i32; _] = [0; 3];
1215
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
1316
//~| ERROR using `_` for array lengths is unstable
17+
let ref_foo: &[i32; _] = &[1, 2, 3];
18+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
19+
//~| ERROR using `_` for array lengths is unstable
20+
let ref_bar: &[i32; _] = &[0; 3];
21+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
22+
//~| ERROR using `_` for array lengths is unstable
23+
let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
24+
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
25+
//~| ERROR using `_` for array lengths is unstable
1426
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
error: in expressions, `_` can only be used on the left-hand side of an assignment
2-
--> $DIR/suggest-array-length.rs:8:20
2+
--> $DIR/suggest-array-length.rs:11:20
33
|
44
LL | let foo: [i32; _] = [1, 2, 3];
55
| ^ `_` not allowed here
66

77
error: in expressions, `_` can only be used on the left-hand side of an assignment
8-
--> $DIR/suggest-array-length.rs:11:20
8+
--> $DIR/suggest-array-length.rs:14:20
99
|
1010
LL | let bar: [i32; _] = [0; 3];
1111
| ^ `_` not allowed here
1212

13+
error: in expressions, `_` can only be used on the left-hand side of an assignment
14+
--> $DIR/suggest-array-length.rs:17:25
15+
|
16+
LL | let ref_foo: &[i32; _] = &[1, 2, 3];
17+
| ^ `_` not allowed here
18+
19+
error: in expressions, `_` can only be used on the left-hand side of an assignment
20+
--> $DIR/suggest-array-length.rs:20:25
21+
|
22+
LL | let ref_bar: &[i32; _] = &[0; 3];
23+
| ^ `_` not allowed here
24+
25+
error: in expressions, `_` can only be used on the left-hand side of an assignment
26+
--> $DIR/suggest-array-length.rs:23:35
27+
|
28+
LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
29+
| ^ `_` not allowed here
30+
1331
error: in expressions, `_` can only be used on the left-hand side of an assignment
1432
--> $DIR/suggest-array-length.rs:5:22
1533
|
1634
LL | const Foo: [i32; _] = [1, 2, 3];
1735
| ^ `_` not allowed here
1836

37+
error: in expressions, `_` can only be used on the left-hand side of an assignment
38+
--> $DIR/suggest-array-length.rs:8:26
39+
|
40+
LL | const REF_FOO: &[u8; _] = &[1];
41+
| ^ `_` not allowed here
42+
1943
error[E0658]: using `_` for array lengths is unstable
2044
--> $DIR/suggest-array-length.rs:5:22
2145
|
@@ -26,7 +50,16 @@ LL | const Foo: [i32; _] = [1, 2, 3];
2650
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
2751

2852
error[E0658]: using `_` for array lengths is unstable
29-
--> $DIR/suggest-array-length.rs:8:20
53+
--> $DIR/suggest-array-length.rs:8:26
54+
|
55+
LL | const REF_FOO: &[u8; _] = &[1];
56+
| ^ help: consider specifying the array length: `1`
57+
|
58+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
59+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
60+
61+
error[E0658]: using `_` for array lengths is unstable
62+
--> $DIR/suggest-array-length.rs:11:20
3063
|
3164
LL | let foo: [i32; _] = [1, 2, 3];
3265
| ^ help: consider specifying the array length: `3`
@@ -35,14 +68,41 @@ LL | let foo: [i32; _] = [1, 2, 3];
3568
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
3669

3770
error[E0658]: using `_` for array lengths is unstable
38-
--> $DIR/suggest-array-length.rs:11:20
71+
--> $DIR/suggest-array-length.rs:14:20
3972
|
4073
LL | let bar: [i32; _] = [0; 3];
4174
| ^ help: consider specifying the array length: `3`
4275
|
4376
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
4477
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
4578

46-
error: aborting due to 6 previous errors
79+
error[E0658]: using `_` for array lengths is unstable
80+
--> $DIR/suggest-array-length.rs:17:25
81+
|
82+
LL | let ref_foo: &[i32; _] = &[1, 2, 3];
83+
| ^ help: consider specifying the array length: `3`
84+
|
85+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
86+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
87+
88+
error[E0658]: using `_` for array lengths is unstable
89+
--> $DIR/suggest-array-length.rs:20:25
90+
|
91+
LL | let ref_bar: &[i32; _] = &[0; 3];
92+
| ^ help: consider specifying the array length: `3`
93+
|
94+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
95+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
96+
97+
error[E0658]: using `_` for array lengths is unstable
98+
--> $DIR/suggest-array-length.rs:23:35
99+
|
100+
LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
101+
| ^ help: consider specifying the array length: `3`
102+
|
103+
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
104+
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
105+
106+
error: aborting due to 14 previous errors
47107

48108
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)