Skip to content

Commit 7ea2981

Browse files
committed
const-eval: do not make UbChecks behavior depend on current crate's flags
1 parent 22572d0 commit 7ea2981

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

+10
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ pub trait Machine<'tcx>: Sized {
280280
Ok(())
281281
}
282282

283+
/// Determines the result of a `NullaryOp::UbChecks` invocation.
284+
fn ub_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
285+
283286
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
284287
/// You can use this to detect long or endlessly running programs.
285288
#[inline]
@@ -627,6 +630,13 @@ pub macro compile_time_machine(<$tcx: lifetime>) {
627630
match fn_val {}
628631
}
629632

633+
#[inline(always)]
634+
fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
635+
// We can't look at `tcx.sess` here as that can differ across crates, which can lead to
636+
// unsound differences in evaluating the same constant at different instantiation sites.
637+
Ok(true)
638+
}
639+
630640
#[inline(always)]
631641
fn adjust_global_allocation<'b>(
632642
_ecx: &InterpCx<$tcx, Self>,

compiler/rustc_const_eval/src/interpret/operator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
512512
self.tcx.offset_of_subfield(self.param_env, layout, fields.iter()).bytes();
513513
ImmTy::from_uint(val, usize_layout())
514514
}
515-
UbChecks => ImmTy::from_bool(self.tcx.sess.ub_checks(), *self.tcx),
515+
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
516516
})
517517
}
518518
}

src/tools/miri/src/machine.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
10601060
ecx.generate_nan(inputs)
10611061
}
10621062

1063+
fn ub_checks(ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool> {
1064+
Ok(ecx.tcx.sess.ub_checks())
1065+
}
1066+
10631067
fn thread_local_static_pointer(
10641068
ecx: &mut MiriInterpCx<'tcx>,
10651069
def_id: DefId,

0 commit comments

Comments
 (0)