Skip to content

Commit 8bb7b7b

Browse files
committed
typeck: always expose repeat count AnonConsts' parent in generics_of.
1 parent 8989029 commit 8bb7b7b

7 files changed

+36
-64
lines changed

src/librustc_typeck/collect.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,28 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
11701170
}
11711171
// FIXME(#43408) enable this always when we get lazy normalization.
11721172
Node::AnonConst(_) => {
1173+
let parent_id = tcx.hir().get_parent_item(hir_id);
1174+
let parent_def_id = tcx.hir().local_def_id(parent_id);
1175+
11731176
// HACK(eddyb) this provides the correct generics when
11741177
// `feature(const_generics)` is enabled, so that const expressions
11751178
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
11761179
if tcx.features().const_generics {
1177-
let parent_id = tcx.hir().get_parent_item(hir_id);
1178-
Some(tcx.hir().local_def_id(parent_id))
1180+
Some(parent_def_id)
11791181
} else {
1180-
None
1182+
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
1183+
match parent_node {
1184+
// HACK(eddyb) this provides the correct generics for repeat
1185+
// expressions' count (i.e. `N` in `[x; N]`), as they shouldn't
1186+
// be able to cause query cycle errors.
1187+
Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
1188+
if constant.hir_id == hir_id =>
1189+
{
1190+
Some(parent_def_id)
1191+
}
1192+
1193+
_ => None,
1194+
}
11811195
}
11821196
}
11831197
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {

src/test/ui/associated-const/associated-const-type-parameter-arrays-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Foo for Def {
1414

1515
pub fn test<A: Foo, B: Foo>() {
1616
let _array = [4; <A as Foo>::Y];
17-
//~^ ERROR the trait bound `A: Foo` is not satisfied [E0277]
17+
//~^ ERROR constant expression depends on a generic parameter
1818
}
1919

2020
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
error[E0277]: the trait bound `A: Foo` is not satisfied
1+
error: constant expression depends on a generic parameter
22
--> $DIR/associated-const-type-parameter-arrays-2.rs:16:22
33
|
4-
LL | const Y: usize;
5-
| --------------- required by `Foo::Y`
6-
...
74
LL | let _array = [4; <A as Foo>::Y];
8-
| ^^^^^^^^^^^^^ the trait `Foo` is not implemented for `A`
5+
| ^^^^^^^^^^^^^
96
|
10-
help: consider further restricting this bound
11-
|
12-
LL | pub fn test<A: Foo + Foo, B: Foo>() {
13-
| ^^^^^
7+
= note: this may fail depending on what value the parameter takes
148

159
error: aborting due to previous error
1610

17-
For more information about this error, try `rustc --explain E0277`.

src/test/ui/consts/too_generic_eval_ice.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ impl<A, B> Foo<A, B> {
44
const HOST_SIZE: usize = std::mem::size_of::<B>();
55

66
pub fn crash() -> bool {
7-
[5; Self::HOST_SIZE] == [6; 0] //~ ERROR no associated item named `HOST_SIZE`
8-
//~^ the size for values of type `A` cannot be known
9-
//~| the size for values of type `B` cannot be known
10-
//~| binary operation `==` cannot be applied to type `[{integer}; _]`
7+
[5; Self::HOST_SIZE] == [6; 0]
8+
//~^ ERROR constant expression depends on a generic parameter
9+
//~| ERROR binary operation `==` cannot be applied to type `[{integer}; _]`
1110
}
1211
}
1312

Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
1-
error[E0599]: no associated item named `HOST_SIZE` found for struct `Foo<A, B>` in the current scope
2-
--> $DIR/too_generic_eval_ice.rs:7:19
3-
|
4-
LL | pub struct Foo<A, B>(A, B);
5-
| --------------------------- associated item `HOST_SIZE` not found for this
6-
...
7-
LL | [5; Self::HOST_SIZE] == [6; 0]
8-
| ^^^^^^^^^ associated item not found in `Foo<A, B>`
9-
|
10-
= note: the method `HOST_SIZE` exists but the following trait bounds were not satisfied:
11-
`A: std::marker::Sized`
12-
`B: std::marker::Sized`
13-
14-
error[E0277]: the size for values of type `A` cannot be known at compilation time
15-
--> $DIR/too_generic_eval_ice.rs:7:13
16-
|
17-
LL | pub struct Foo<A, B>(A, B);
18-
| - required by this bound in `Foo`
19-
LL |
20-
LL | impl<A, B> Foo<A, B> {
21-
| - this type parameter needs to be `std::marker::Sized`
22-
...
23-
LL | [5; Self::HOST_SIZE] == [6; 0]
24-
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
25-
|
26-
= help: the trait `std::marker::Sized` is not implemented for `A`
27-
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
28-
29-
error[E0277]: the size for values of type `B` cannot be known at compilation time
1+
error: constant expression depends on a generic parameter
302
--> $DIR/too_generic_eval_ice.rs:7:13
313
|
32-
LL | pub struct Foo<A, B>(A, B);
33-
| - required by this bound in `Foo`
34-
LL |
35-
LL | impl<A, B> Foo<A, B> {
36-
| - this type parameter needs to be `std::marker::Sized`
37-
...
384
LL | [5; Self::HOST_SIZE] == [6; 0]
39-
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^^^^^^
406
|
41-
= help: the trait `std::marker::Sized` is not implemented for `B`
42-
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
7+
= note: this may fail depending on what value the parameter takes
438

449
error[E0369]: binary operation `==` cannot be applied to type `[{integer}; _]`
4510
--> $DIR/too_generic_eval_ice.rs:7:30
@@ -49,7 +14,6 @@ LL | [5; Self::HOST_SIZE] == [6; 0]
4914
| |
5015
| [{integer}; _]
5116

52-
error: aborting due to 4 previous errors
17+
error: aborting due to 2 previous errors
5318

54-
Some errors have detailed explanations: E0277, E0369, E0599.
55-
For more information about an error, try `rustc --explain E0277`.
19+
For more information about this error, try `rustc --explain E0369`.

src/test/ui/issues/issue-39211.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ trait Mat {
88
}
99

1010
fn m<M: Mat>() {
11-
let a = [3; M::Row::DIM]; //~ ERROR associated type `Row` not found for `M`
11+
let a = [3; M::Row::DIM];
12+
//~^ ERROR constant expression depends on a generic parameter
1213
}
1314
fn main() {
1415
}

src/test/ui/issues/issue-39211.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
error[E0220]: associated type `Row` not found for `M`
2-
--> $DIR/issue-39211.rs:11:20
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-39211.rs:11:17
33
|
44
LL | let a = [3; M::Row::DIM];
5-
| ^^^ associated type `Row` not found
5+
| ^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
68

79
error: aborting due to previous error
810

9-
For more information about this error, try `rustc --explain E0220`.

0 commit comments

Comments
 (0)