Skip to content

Commit

Permalink
typeck: always expose repeat count AnonConsts' parent in `generics_…
Browse files Browse the repository at this point in the history
…of`.
  • Loading branch information
eddyb committed Apr 14, 2020
1 parent 8989029 commit 8bb7b7b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 64 deletions.
20 changes: 17 additions & 3 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,14 +1170,28 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
}
// FIXME(#43408) enable this always when we get lazy normalization.
Node::AnonConst(_) => {
let parent_id = tcx.hir().get_parent_item(hir_id);
let parent_def_id = tcx.hir().local_def_id(parent_id);

// HACK(eddyb) this provides the correct generics when
// `feature(const_generics)` is enabled, so that const expressions
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
if tcx.features().const_generics {
let parent_id = tcx.hir().get_parent_item(hir_id);
Some(tcx.hir().local_def_id(parent_id))
Some(parent_def_id)
} else {
None
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
match parent_node {
// HACK(eddyb) this provides the correct generics for repeat
// expressions' count (i.e. `N` in `[x; N]`), as they shouldn't
// be able to cause query cycle errors.
Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
if constant.hir_id == hir_id =>
{
Some(parent_def_id)
}

_ => None,
}
}
}
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Foo for Def {

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

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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
7 changes: 3 additions & 4 deletions src/test/ui/consts/too_generic_eval_ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ impl<A, B> Foo<A, B> {
const HOST_SIZE: usize = std::mem::size_of::<B>();

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

Expand Down
46 changes: 5 additions & 41 deletions src/test/ui/consts/too_generic_eval_ice.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,10 @@
error[E0599]: no associated item named `HOST_SIZE` found for struct `Foo<A, B>` in the current scope
--> $DIR/too_generic_eval_ice.rs:7:19
|
LL | pub struct Foo<A, B>(A, B);
| --------------------------- associated item `HOST_SIZE` not found for this
...
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^^^^^^^^ associated item not found in `Foo<A, B>`
|
= note: the method `HOST_SIZE` exists but the following trait bounds were not satisfied:
`A: std::marker::Sized`
`B: std::marker::Sized`

error[E0277]: the size for values of type `A` cannot be known at compilation time
--> $DIR/too_generic_eval_ice.rs:7:13
|
LL | pub struct Foo<A, B>(A, B);
| - required by this bound in `Foo`
LL |
LL | impl<A, B> Foo<A, B> {
| - this type parameter needs to be `std::marker::Sized`
...
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `A`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>

error[E0277]: the size for values of type `B` cannot be known at compilation time
error: constant expression depends on a generic parameter
--> $DIR/too_generic_eval_ice.rs:7:13
|
LL | pub struct Foo<A, B>(A, B);
| - required by this bound in `Foo`
LL |
LL | impl<A, B> Foo<A, B> {
| - this type parameter needs to be `std::marker::Sized`
...
LL | [5; Self::HOST_SIZE] == [6; 0]
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^^^^
|
= help: the trait `std::marker::Sized` is not implemented for `B`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: this may fail depending on what value the parameter takes

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

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0369, E0599.
For more information about an error, try `rustc --explain E0277`.
For more information about this error, try `rustc --explain E0369`.
3 changes: 2 additions & 1 deletion src/test/ui/issues/issue-39211.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ trait Mat {
}

fn m<M: Mat>() {
let a = [3; M::Row::DIM]; //~ ERROR associated type `Row` not found for `M`
let a = [3; M::Row::DIM];
//~^ ERROR constant expression depends on a generic parameter
}
fn main() {
}
9 changes: 5 additions & 4 deletions src/test/ui/issues/issue-39211.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0220]: associated type `Row` not found for `M`
--> $DIR/issue-39211.rs:11:20
error: constant expression depends on a generic parameter
--> $DIR/issue-39211.rs:11:17
|
LL | let a = [3; M::Row::DIM];
| ^^^ associated type `Row` not found
| ^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to previous error

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

0 comments on commit 8bb7b7b

Please sign in to comment.