Skip to content

Commit 590d3ba

Browse files
Unconditionally lower generic_arg_infer
1 parent 653fc55 commit 590d3ba

File tree

10 files changed

+66
-120
lines changed

10 files changed

+66
-120
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,20 +2031,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20312031
fn lower_array_length_to_const_arg(&mut self, c: &AnonConst) -> &'hir hir::ConstArg<'hir> {
20322032
match c.value.kind {
20332033
ExprKind::Underscore => {
2034-
if self.tcx.features().generic_arg_infer() {
2035-
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
2036-
self.arena
2037-
.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
2038-
} else {
2034+
if !self.tcx.features().generic_arg_infer() {
20392035
feature_err(
20402036
&self.tcx.sess,
20412037
sym::generic_arg_infer,
20422038
c.value.span,
20432039
fluent_generated::ast_lowering_underscore_array_length_unstable,
20442040
)
20452041
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2046-
self.lower_anon_const_to_const_arg(c)
20472042
}
2043+
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
2044+
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
20482045
}
20492046
_ => self.lower_anon_const_to_const_arg(c),
20502047
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ fn infer_placeholder_type<'tcx>(
452452
if let Some(ty) = tcx.hir_node_by_def_id(def_id).ty() {
453453
visitor.visit_ty(ty);
454454
}
455+
// If we have just one span, let's try to steal a const `_` feature error.
456+
let try_steal_span = if !tcx.features().generic_arg_infer() && visitor.0.len() == 1 {
457+
visitor.0.first().copied()
458+
} else {
459+
None
460+
};
455461
// If we didn't find any infer tys, then just fallback to `span``.
456462
if visitor.0.is_empty() {
457463
visitor.0.push(span);
@@ -473,7 +479,16 @@ fn infer_placeholder_type<'tcx>(
473479
));
474480
}
475481
}
476-
diag.emit()
482+
483+
if let Some(try_steal_span) = try_steal_span {
484+
cx.dcx().try_steal_replace_and_emit_err(
485+
try_steal_span,
486+
StashKey::UnderscoreForArrayLengths,
487+
diag,
488+
)
489+
} else {
490+
diag.emit()
491+
}
477492
});
478493
Ty::new_error(tcx, guar)
479494
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17761776
let Some((
17771777
_,
17781778
hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. })
1779-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }),
17801779
)) = parent_node
17811780
else {
17821781
return;

tests/ui/array-slice-vec/suggest-array-length.fixed

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33

44
fn main() {
55
const Foo: [i32; 3] = [1, 2, 3];
6-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
7-
//~| ERROR using `_` for array lengths is unstable
6+
//~^ ERROR using `_` for array lengths is unstable
87
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
8+
//~^ ERROR using `_` for array lengths is unstable
119
let foo: [i32; 3] = [1, 2, 3];
12-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
13-
//~| ERROR using `_` for array lengths is unstable
10+
//~^ ERROR using `_` for array lengths is unstable
1411
let bar: [i32; 3] = [0; 3];
15-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
16-
//~| ERROR using `_` for array lengths is unstable
12+
//~^ ERROR using `_` for array lengths is unstable
1713
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
14+
//~^ ERROR using `_` for array lengths is unstable
2015
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
16+
//~^ ERROR using `_` for array lengths is unstable
2317
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
18+
//~^ ERROR using `_` for array lengths is unstable
2619
}

tests/ui/array-slice-vec/suggest-array-length.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,17 @@
33

44
fn main() {
55
const Foo: [i32; _] = [1, 2, 3];
6-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
7-
//~| ERROR using `_` for array lengths is unstable
6+
//~^ ERROR using `_` for array lengths is unstable
87
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
8+
//~^ ERROR using `_` for array lengths is unstable
119
let foo: [i32; _] = [1, 2, 3];
12-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
13-
//~| ERROR using `_` for array lengths is unstable
10+
//~^ ERROR using `_` for array lengths is unstable
1411
let bar: [i32; _] = [0; 3];
15-
//~^ ERROR in expressions, `_` can only be used on the left-hand side of an assignment
16-
//~| ERROR using `_` for array lengths is unstable
12+
//~^ ERROR using `_` for array lengths is unstable
1713
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
14+
//~^ ERROR using `_` for array lengths is unstable
2015
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
16+
//~^ ERROR using `_` for array lengths is unstable
2317
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
18+
//~^ ERROR using `_` for array lengths is unstable
2619
}
Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,23 @@
1-
error: in expressions, `_` can only be used on the left-hand side of an assignment
2-
--> $DIR/suggest-array-length.rs:11:20
3-
|
4-
LL | let foo: [i32; _] = [1, 2, 3];
5-
| ^ `_` not allowed here
6-
7-
error: in expressions, `_` can only be used on the left-hand side of an assignment
8-
--> $DIR/suggest-array-length.rs:14:20
9-
|
10-
LL | let bar: [i32; _] = [0; 3];
11-
| ^ `_` not allowed here
12-
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-
31-
error: in expressions, `_` can only be used on the left-hand side of an assignment
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
322
--> $DIR/suggest-array-length.rs:5:22
333
|
344
LL | const Foo: [i32; _] = [1, 2, 3];
35-
| ^ `_` not allowed here
5+
| ------^-
6+
| | |
7+
| | not allowed in type signatures
8+
| help: replace with the correct type: `[i32; 3]`
369

37-
error: in expressions, `_` can only be used on the left-hand side of an assignment
38-
--> $DIR/suggest-array-length.rs:8:26
10+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
11+
--> $DIR/suggest-array-length.rs:7:26
3912
|
4013
LL | const REF_FOO: &[u8; _] = &[1];
41-
| ^ `_` not allowed here
14+
| ------^-
15+
| | |
16+
| | not allowed in type signatures
17+
| help: replace with the correct type: `&[u8; 1]`
4218

4319
error[E0658]: using `_` for array lengths is unstable
44-
--> $DIR/suggest-array-length.rs:5:22
45-
|
46-
LL | const Foo: [i32; _] = [1, 2, 3];
47-
| ^ help: consider specifying the array length: `3`
48-
|
49-
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
50-
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
51-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
52-
53-
error[E0658]: using `_` for array lengths is unstable
54-
--> $DIR/suggest-array-length.rs:8:26
55-
|
56-
LL | const REF_FOO: &[u8; _] = &[1];
57-
| ^ help: consider specifying the array length: `1`
58-
|
59-
= note: see issue #85077 <https://github.com/rust-lang/rust/issues/85077> for more information
60-
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
61-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
62-
63-
error[E0658]: using `_` for array lengths is unstable
64-
--> $DIR/suggest-array-length.rs:11:20
20+
--> $DIR/suggest-array-length.rs:9:20
6521
|
6622
LL | let foo: [i32; _] = [1, 2, 3];
6723
| ^ help: consider specifying the array length: `3`
@@ -71,7 +27,7 @@ LL | let foo: [i32; _] = [1, 2, 3];
7127
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
7228

7329
error[E0658]: using `_` for array lengths is unstable
74-
--> $DIR/suggest-array-length.rs:14:20
30+
--> $DIR/suggest-array-length.rs:11:20
7531
|
7632
LL | let bar: [i32; _] = [0; 3];
7733
| ^ help: consider specifying the array length: `3`
@@ -81,7 +37,7 @@ LL | let bar: [i32; _] = [0; 3];
8137
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
8238

8339
error[E0658]: using `_` for array lengths is unstable
84-
--> $DIR/suggest-array-length.rs:17:25
40+
--> $DIR/suggest-array-length.rs:13:25
8541
|
8642
LL | let ref_foo: &[i32; _] = &[1, 2, 3];
8743
| ^ help: consider specifying the array length: `3`
@@ -91,7 +47,7 @@ LL | let ref_foo: &[i32; _] = &[1, 2, 3];
9147
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9248

9349
error[E0658]: using `_` for array lengths is unstable
94-
--> $DIR/suggest-array-length.rs:20:25
50+
--> $DIR/suggest-array-length.rs:15:25
9551
|
9652
LL | let ref_bar: &[i32; _] = &[0; 3];
9753
| ^ help: consider specifying the array length: `3`
@@ -101,7 +57,7 @@ LL | let ref_bar: &[i32; _] = &[0; 3];
10157
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10258

10359
error[E0658]: using `_` for array lengths is unstable
104-
--> $DIR/suggest-array-length.rs:23:35
60+
--> $DIR/suggest-array-length.rs:17:35
10561
|
10662
LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
10763
| ^ help: consider specifying the array length: `3`
@@ -110,6 +66,7 @@ LL | let multiple_ref_foo: &&[i32; _] = &&[1, 2, 3];
11066
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
11167
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
11268

113-
error: aborting due to 14 previous errors
69+
error: aborting due to 7 previous errors
11470

115-
For more information about this error, try `rustc --explain E0658`.
71+
Some errors have detailed explanations: E0121, E0658.
72+
For more information about an error, try `rustc --explain E0121`.

tests/ui/async-await/issues/issue-95307.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
pub trait C {
77
async fn new() -> [u8; _];
8-
//~^ ERROR: using `_` for array lengths is unstable
9-
//~| ERROR: in expressions, `_` can only be used on the left-hand side of an assignment
8+
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions
9+
//~| ERROR using `_` for array lengths is unstable
1010
}
1111

1212
fn main() {}

tests/ui/async-await/issues/issue-95307.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error: in expressions, `_` can only be used on the left-hand side of an assignment
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
22
--> $DIR/issue-95307.rs:7:28
33
|
44
LL | async fn new() -> [u8; _];
5-
| ^ `_` not allowed here
5+
| ^ not allowed in type signatures
6+
|
7+
help: use type parameters instead
8+
|
9+
LL | async fn new<T>() -> [u8; T];
10+
| +++ ~
611

712
error[E0658]: using `_` for array lengths is unstable
813
--> $DIR/issue-95307.rs:7:28
@@ -16,4 +21,5 @@ LL | async fn new() -> [u8; _];
1621

1722
error: aborting due to 2 previous errors
1823

19-
For more information about this error, try `rustc --explain E0658`.
24+
Some errors have detailed explanations: E0121, E0658.
25+
For more information about an error, try `rustc --explain E0121`.
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
error: in expressions, `_` can only be used on the left-hand side of an assignment
2-
--> $DIR/feature-gate-generic_arg_infer.rs:11:27
3-
|
4-
LL | let _x: [u8; 3] = [0; _];
5-
| ^ `_` not allowed here
6-
7-
error: in expressions, `_` can only be used on the left-hand side of an assignment
8-
--> $DIR/feature-gate-generic_arg_infer.rs:14:18
9-
|
10-
LL | let _y: [u8; _] = [0; 3];
11-
| ^ `_` not allowed here
12-
131
error[E0658]: using `_` for array lengths is unstable
14-
--> $DIR/feature-gate-generic_arg_infer.rs:14:18
2+
--> $DIR/feature-gate-generic_arg_infer.rs:13:18
153
|
164
LL | let _y: [u8; _] = [0; 3];
175
| ^ help: consider specifying the array length: `3`
@@ -21,7 +9,7 @@ LL | let _y: [u8; _] = [0; 3];
219
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2210

2311
error[E0747]: type provided when a constant was expected
24-
--> $DIR/feature-gate-generic_arg_infer.rs:20:20
12+
--> $DIR/feature-gate-generic_arg_infer.rs:18:20
2513
|
2614
LL | let _x = foo::<_>([1,2]);
2715
| ^
@@ -42,7 +30,7 @@ LL | let _x: [u8; 3] = [0; _];
4230
= help: add `#![feature(generic_arg_infer)]` to the crate attributes to enable
4331
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4432

45-
error: aborting due to 5 previous errors
33+
error: aborting due to 3 previous errors
4634

4735
Some errors have detailed explanations: E0658, E0747.
4836
For more information about an error, try `rustc --explain E0658`.

tests/ui/feature-gates/feature-gate-generic_arg_infer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ fn foo<const N: usize>(_: [u8; N]) -> [u8; N] {
1010
fn bar() {
1111
let _x: [u8; 3] = [0; _];
1212
//[normal]~^ ERROR: using `_` for array lengths is unstable
13-
//[normal]~| ERROR: in expressions, `_` can only be used on the left-hand side of an assignment
1413
let _y: [u8; _] = [0; 3];
1514
//[normal]~^ ERROR: using `_` for array lengths is unstable
16-
//[normal]~| ERROR: in expressions, `_` can only be used on the left-hand side of an assignment
1715
}
1816

1917
fn main() {

0 commit comments

Comments
 (0)