Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[const-prop] Fix ICE when trying to eval polymorphic promoted MIR #65066

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
wesleywiser marked this conversation as resolved.
Show resolved Hide resolved

// 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::<()>();
}