Skip to content

Commit

Permalink
fix ICE in ConstProp
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jul 6, 2022
1 parent dfd243e commit a73e255
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_const_eval/src/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_middle::mir;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::def_id::DefId;
use rustc_target::abi::Size;
use rustc_target::spec::abi::Abi;
use rustc_target::spec::abi::Abi as FnAbi;

use super::{
AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
Expand Down Expand Up @@ -139,7 +139,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
/// Whether to enforce integers and floats not having provenance.
fn enforce_number_no_provenance(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;

/// Whether function calls should be [ABI](Abi)-checked.
/// Whether function calls should be [ABI](FnAbi)-checked.
fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
true
}
Expand Down Expand Up @@ -170,7 +170,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
fn find_mir_or_eval_fn(
ecx: &mut InterpCx<'mir, 'tcx, Self>,
instance: ty::Instance<'tcx>,
abi: Abi,
abi: FnAbi,
args: &[OpTy<'tcx, Self::PointerTag>],
destination: &PlaceTy<'tcx, Self::PointerTag>,
target: Option<mir::BasicBlock>,
Expand All @@ -182,7 +182,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
fn call_extra_fn(
ecx: &mut InterpCx<'mir, 'tcx, Self>,
fn_val: Self::ExtraFnVal,
abi: Abi,
abi: FnAbi,
args: &[OpTy<'tcx, Self::PointerTag>],
destination: &PlaceTy<'tcx, Self::PointerTag>,
target: Option<mir::BasicBlock>,
Expand Down Expand Up @@ -480,7 +480,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
fn call_extra_fn(
_ecx: &mut InterpCx<$mir, $tcx, Self>,
fn_val: !,
_abi: Abi,
_abi: FnAbi,
_args: &[OpTy<$tcx>],
_destination: &PlaceTy<$tcx, Self::PointerTag>,
_target: Option<mir::BasicBlock>,
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_mir_transform/src/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use rustc_middle::ty::{
self, ConstKind, EarlyBinder, Instance, ParamEnv, Ty, TyCtxt, TypeVisitable,
};
use rustc_span::{def_id::DefId, Span};
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
use rustc_target::spec::abi::Abi;
use rustc_target::abi::{self, HasDataLayout, Size, TargetDataLayout};
use rustc_target::spec::abi::Abi as FnAbi;
use rustc_trait_selection::traits;

use crate::MirPass;
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
fn find_mir_or_eval_fn(
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
_instance: ty::Instance<'tcx>,
_abi: Abi,
_abi: FnAbi,
_args: &[OpTy<'tcx>],
_destination: &PlaceTy<'tcx>,
_target: Option<BasicBlock>,
Expand Down Expand Up @@ -654,6 +654,11 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
(Ok(_), Ok(_)) => return this.ecx.eval_rvalue_into_place(rvalue, place),
};

if !matches!(const_arg.layout.abi, abi::Abi::Scalar(..)) {
// We cannot handle Scalar Pair stuff.
return this.ecx.eval_rvalue_into_place(rvalue, place);
}

let arg_value = const_arg.to_scalar()?.to_bits(const_arg.layout.size)?;
let dest = this.ecx.eval_place(place)?;

Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/consts/issue-96169.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// check-pass
// compile-flags: -Zmir-opt-level=4 --emit=mir
#![allow(unused)]
fn a() -> usize { 0 }

fn bar(_: u32) {}

fn baz() -> *const dyn Fn(u32) { unimplemented!() }

fn foo() {
match () {
_ if baz() == &bar as &dyn Fn(u32) => (),
() => (),
}
}

fn main() {
}

0 comments on commit a73e255

Please sign in to comment.