Skip to content

Commit 926c7a2

Browse files
committed
typeck: always expose explicit enum discriminant AnonConsts' parent in generics_of.
1 parent 2c29f0c commit 926c7a2

6 files changed

+83
-2
lines changed

src/librustc_typeck/collect.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1178,9 +1178,11 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
11781178
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
11791179
match parent_node {
11801180
// HACK(eddyb) this provides the correct generics for repeat
1181-
// expressions' count (i.e. `N` in `[x; N]`), as they shouldn't
1182-
// be able to cause query cycle errors.
1181+
// expressions' count (i.e. `N` in `[x; N]`), and explicit
1182+
// `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
1183+
// as they shouldn't be able to cause query cycle errors.
11831184
Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
1185+
| Node::Variant(Variant { disr_expr: Some(ref constant), .. })
11841186
if constant.hir_id == hir_id =>
11851187
{
11861188
Some(parent_def_id.to_def_id())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(arbitrary_enum_discriminant, core_intrinsics)]
2+
3+
extern crate core;
4+
use core::intrinsics::discriminant_value;
5+
6+
#[repr(usize)]
7+
enum MyWeirdOption<T> {
8+
None = 0,
9+
Some(T) = std::mem::size_of::<T>(),
10+
//~^ ERROR constant expression depends on a generic parameter
11+
}
12+
13+
fn main() {
14+
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0);
15+
assert_eq!(discriminant_value(&MyWeirdOption::Some(0u8)), 1);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-70453-generics-in-discr-ice-2.rs:9:15
3+
|
4+
LL | Some(T) = std::mem::size_of::<T>(),
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics)]
2+
3+
extern crate core;
4+
use core::intrinsics::discriminant_value;
5+
6+
#[repr(usize)]
7+
enum MyWeirdOption<T> {
8+
//~^ ERROR parameter `T` is never used
9+
None = 0,
10+
Some = std::mem::size_of::<T>(),
11+
//~^ ERROR constant expression depends on a generic parameter
12+
}
13+
14+
fn main() {
15+
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0);
16+
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::Some), 1);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-70453-generics-in-discr-ice.rs:10:12
3+
|
4+
LL | Some = std::mem::size_of::<T>(),
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error[E0392]: parameter `T` is never used
10+
--> $DIR/issue-70453-generics-in-discr-ice.rs:7:20
11+
|
12+
LL | enum MyWeirdOption<T> {
13+
| ^ unused parameter
14+
|
15+
= help: consider removing `T`, referring to it in a field, or using a marker such as `std::marker::PhantomData`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0392`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
3+
#![feature(arbitrary_enum_discriminant, core_intrinsics)]
4+
5+
extern crate core;
6+
use core::intrinsics::discriminant_value;
7+
8+
#[repr(usize)]
9+
enum MyWeirdOption<T> {
10+
None = 0,
11+
Some(T) = core::mem::size_of::<*mut T>(),
12+
}
13+
14+
fn main() {
15+
assert_eq!(discriminant_value(&MyWeirdOption::<()>::None), 0);
16+
assert_eq!(discriminant_value(&MyWeirdOption::Some(())), core::mem::size_of::<usize>() as u64);
17+
}

0 commit comments

Comments
 (0)