Skip to content

Commit

Permalink
[const-prop] Fix ICE when trying to eval polymorphic promoted MIR
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Oct 4, 2019
1 parent cfb6d84 commit e9009c8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/librustc_mir/interpret/place.rs
Expand Up @@ -594,6 +594,13 @@ where
StaticKind::Promoted(promoted, promoted_substs) => {
let substs = self.subst_from_frame_and_normalize_erasing_regions(promoted_substs);
let instance = ty::Instance::new(place_static.def_id, substs);

// Even after getting `substs` from the frame, this instance may still be
// polymorphic because `ConstProp` will try to promote polymorphic MIR.
if instance.needs_subst() {
throw_inval!(TooGeneric);
}

self.const_eval_raw(GlobalId {
instance,
promoted: Some(promoted),
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/consts/const-eval/issue-50814.rs
Expand Up @@ -11,7 +11,6 @@ struct Sum<A,B>(A,B);

impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A,B> {
const MAX: u8 = A::MAX + B::MAX; //~ ERROR any use of this value will cause an error
//~| ERROR any use of this value will cause an error
}

fn foo<T>(_: T) -> &'static u8 {
Expand Down
12 changes: 2 additions & 10 deletions src/test/ui/consts/const-eval/issue-50814.stderr
Expand Up @@ -9,21 +9,13 @@ LL | const MAX: u8 = A::MAX + B::MAX;
= note: `#[deny(const_err)]` on by default

error[E0080]: evaluation of constant expression failed
--> $DIR/issue-50814.rs:18:5
--> $DIR/issue-50814.rs:17:5
|
LL | &Sum::<U8,U8>::MAX
| ^-----------------
| |
| referenced constant has errors

error: any use of this value will cause an error
--> $DIR/issue-50814.rs:13:21
|
LL | const MAX: u8 = A::MAX + B::MAX;
| ----------------^^^^^^^^^^^^^^^-
| |
| attempt to add with overflow

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

For more information about this error, try `rustc --explain E0080`.
20 changes: 20 additions & 0 deletions src/test/ui/consts/const-eval/issue-64908.rs
@@ -0,0 +1,20 @@
// run-pass

// This test verifies that the `ConstProp` pass doesn't cause an ICE when evaluating polymorphic
// promoted MIR.

pub trait ArrowPrimitiveType {
type Native;
}

pub fn new<T: ArrowPrimitiveType>() {
assert_eq!(0, std::mem::size_of::<T::Native>());
}

impl ArrowPrimitiveType for () {
type Native = ();
}

fn main() {
new::<()>();
}

0 comments on commit e9009c8

Please sign in to comment.