Skip to content

Commit 768408a

Browse files
committed
Auto merge of #121662 - saethlin:precondition-unification, r=RalfJung
Distinguish between library and lang UB in assert_unsafe_precondition As described in #121583 (comment), `assert_unsafe_precondition` now explicitly distinguishes between language UB (conditions we explicitly optimize on) and library UB (things we document you shouldn't do, and maybe some library internals assume you don't do). `debug_assert_nounwind` was originally added to avoid the "only at runtime" aspect of `assert_unsafe_precondition`. Since then the difference between the macros has gotten muddied. This totally revamps the situation. Now _all_ preconditions shall be checked with `assert_unsafe_precondition`. If you have a precondition that's only checkable at runtime, do a `const_eval_select` hack, as done in this PR. r? RalfJung
2 parents 2d24fe5 + 2a1f97f commit 768408a

File tree

47 files changed

+420
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+420
-264
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20002000
ConstraintCategory::SizedBound,
20012001
);
20022002
}
2003-
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
2003+
&Rvalue::NullaryOp(NullOp::UbCheck(_), _) => {}
20042004

20052005
Rvalue::ShallowInitBox(operand, ty) => {
20062006
self.check_operand(operand, location);

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ fn codegen_stmt<'tcx>(
779779
NullOp::OffsetOf(fields) => {
780780
layout.offset_of_subfield(fx, fields.iter()).bytes()
781781
}
782-
NullOp::DebugAssertions => {
782+
NullOp::UbCheck(_) => {
783783
let val = fx.tcx.sess.opts.debug_assertions;
784784
let val = CValue::by_val(
785785
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
685685
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
686686
bx.cx().const_usize(val)
687687
}
688-
mir::NullOp::DebugAssertions => {
688+
mir::NullOp::UbCheck(_) => {
689+
// In codegen, we want to check for language UB and library UB
689690
let val = bx.tcx().sess.opts.debug_assertions;
690691
bx.cx().const_bool(val)
691692
}

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
258258
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
259259
Scalar::from_target_usize(val, self)
260260
}
261-
mir::NullOp::DebugAssertions => {
262-
// The checks hidden behind this are always better done by the interpreter
263-
// itself, because it knows the runtime state better.
264-
Scalar::from_bool(false)
261+
mir::NullOp::UbCheck(kind) => {
262+
// We want to enable checks for library UB, because the interpreter doesn't
263+
// know about those on its own.
264+
// But we want to disable checks for language UB, because the interpreter
265+
// has its own better checks for that.
266+
let should_check = match kind {
267+
mir::UbKind::LibraryUb => self.tcx.sess.opts.debug_assertions,
268+
mir::UbKind::LanguageUb => false,
269+
};
270+
Scalar::from_bool(should_check)
265271
}
266272
};
267273
self.write_scalar(val, &dest)?;

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
558558
Rvalue::Cast(_, _, _) => {}
559559

560560
Rvalue::NullaryOp(
561-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
561+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbCheck(_),
562562
_,
563563
) => {}
564564
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11571157
Rvalue::Repeat(_, _)
11581158
| Rvalue::ThreadLocalRef(_)
11591159
| Rvalue::AddressOf(_, _)
1160-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
1160+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbCheck(_), _)
11611161
| Rvalue::Discriminant(_) => {}
11621162
}
11631163
self.super_rvalue(rvalue, location);

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
127127
| sym::variant_count
128128
| sym::is_val_statically_known
129129
| sym::ptr_mask
130-
| sym::debug_assertions
130+
| sym::check_language_ub
131+
| sym::check_library_ub
131132
| sym::fadd_algebraic
132133
| sym::fsub_algebraic
133134
| sym::fmul_algebraic
@@ -584,7 +585,7 @@ pub fn check_intrinsic_type(
584585
(0, 0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
585586
}
586587

587-
sym::debug_assertions => (0, 1, Vec::new(), tcx.types.bool),
588+
sym::check_language_ub | sym::check_library_ub => (0, 1, Vec::new(), tcx.types.bool),
588589

589590
sym::simd_eq
590591
| sym::simd_ne

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
915915
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
916916
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
917917
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
918-
NullOp::DebugAssertions => write!(fmt, "cfg!(debug_assertions)"),
918+
NullOp::UbCheck(kind) => write!(fmt, "UbCheck({kind:?})"),
919919
}
920920
}
921921
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,16 @@ pub enum NullOp<'tcx> {
13661366
AlignOf,
13671367
/// Returns the offset of a field
13681368
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1369-
/// cfg!(debug_assertions), but expanded in codegen
1370-
DebugAssertions,
1369+
/// Returns whether we want to check for library UB or language UB at monomorphization time.
1370+
/// Both kinds of UB evaluate to `true` in codegen, and only library UB evalutes to `true` in
1371+
/// const-eval/Miri, because the interpreter has its own better checks for language UB.
1372+
UbCheck(UbKind),
1373+
}
1374+
1375+
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1376+
pub enum UbKind {
1377+
LanguageUb,
1378+
LibraryUb,
13711379
}
13721380

13731381
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'tcx> Rvalue<'tcx> {
194194
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
195195
tcx.types.usize
196196
}
197-
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => tcx.types.bool,
197+
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => tcx.types.bool,
198198
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
199199
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
200200
AggregateKind::Tuple => {

0 commit comments

Comments
 (0)