forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#70825 - eddyb:enum-discr-correct-generics-par…
…ent, r=nikomatsakis typeck: always expose explicit enum discriminant `AnonConst`s' parent in `generics_of`. This is similar to rust-lang#70452 but for explicit `enum` discriminant constant expressions. However, unlike rust-lang#70452, this PR should have no effect on stable code, as while it alleviates rust-lang#43408 errors, there is no way to actually compile an `enum` with generic parameters *and* explicit discriminants, without `#![feature(arbitrary_enum_discriminant)]`, as explicit discriminant expression don't count as uses of parameters (if they did, they would count as invariant uses). <hr/> There's also 2 other commits here, both related to rust-lang#70453: * "ty: use `delay_span_bug` in `ty::AdtDef::eval_explicit_discr`." - hides the ICEs demonstrated on rust-lang#70453, when there are other errors (which the next commit adds) * "typeck/wfcheck: require that explicit enum discriminants const-evaluate succesfully." - closes rust-lang#70453 by picking alternative "2", i.e. erroring when a discriminant doesn't fully const-evaluate from the perspective of the `enum` definition In the future, it might be possible to allow `enum` discriminants to actually depend on parameters, but that will likely require rust-lang#68436 + some way to restrict the values so no two variants can end up with overlapping discriminants. As this PR would close rust-lang#70453, it shouldn't be merged until a decision is reached there. r? @nikomatsakis
- Loading branch information
Showing
8 changed files
with
126 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![feature(arbitrary_enum_discriminant, core_intrinsics)] | ||
|
||
extern crate core; | ||
use core::intrinsics::discriminant_value; | ||
|
||
#[repr(usize)] | ||
enum MyWeirdOption<T> { | ||
None = 0, | ||
Some(T) = std::mem::size_of::<T>(), | ||
//~^ ERROR constant expression depends on a generic parameter | ||
} | ||
|
||
fn main() { | ||
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0); | ||
assert_eq!(discriminant_value(&MyWeirdOption::Some(0u8)), 1); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice-2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: constant expression depends on a generic parameter | ||
--> $DIR/issue-70453-generics-in-discr-ice-2.rs:9:15 | ||
| | ||
LL | Some(T) = std::mem::size_of::<T>(), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this may fail depending on what value the parameter takes | ||
|
||
error: aborting due to previous error | ||
|
17 changes: 17 additions & 0 deletions
17
src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#![feature(core_intrinsics)] | ||
|
||
extern crate core; | ||
use core::intrinsics::discriminant_value; | ||
|
||
#[repr(usize)] | ||
enum MyWeirdOption<T> { | ||
//~^ ERROR parameter `T` is never used | ||
None = 0, | ||
Some = std::mem::size_of::<T>(), | ||
//~^ ERROR constant expression depends on a generic parameter | ||
} | ||
|
||
fn main() { | ||
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::None), 0); | ||
assert_eq!(discriminant_value(&MyWeirdOption::<u8>::Some), 1); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/enum-discriminant/issue-70453-generics-in-discr-ice.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: constant expression depends on a generic parameter | ||
--> $DIR/issue-70453-generics-in-discr-ice.rs:10:12 | ||
| | ||
LL | Some = std::mem::size_of::<T>(), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this may fail depending on what value the parameter takes | ||
|
||
error[E0392]: parameter `T` is never used | ||
--> $DIR/issue-70453-generics-in-discr-ice.rs:7:20 | ||
| | ||
LL | enum MyWeirdOption<T> { | ||
| ^ unused parameter | ||
| | ||
= help: consider removing `T`, referring to it in a field, or using a marker such as `std::marker::PhantomData` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0392`. |
17 changes: 17 additions & 0 deletions
17
src/test/ui/enum-discriminant/issue-70453-polymorphic-ctfe.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// run-pass | ||
|
||
#![feature(arbitrary_enum_discriminant, core_intrinsics)] | ||
|
||
extern crate core; | ||
use core::intrinsics::discriminant_value; | ||
|
||
#[repr(usize)] | ||
enum MyWeirdOption<T> { | ||
None = 0, | ||
Some(T) = core::mem::size_of::<*mut T>(), | ||
} | ||
|
||
fn main() { | ||
assert_eq!(discriminant_value(&MyWeirdOption::<()>::None), 0); | ||
assert_eq!(discriminant_value(&MyWeirdOption::Some(())), core::mem::size_of::<usize>() as u64); | ||
} |