Skip to content

Commit 2f090c3

Browse files
committed
Auto merge of #122629 - RalfJung:assert-unsafe-precondition, r=saethlin
refactor check_{lang,library}_ub: use a single intrinsic This enacts the plan I laid out [here](#122282 (comment)): use a single intrinsic, called `ub_checks` (in aniticpation of rust-lang/compiler-team#725), that just exposes the value of `debug_assertions` (consistently implemented in both codegen and the interpreter). Put the language vs library UB logic into the library. This makes it easier to do something like #122282 in the future: that just slightly alters the semantics of `ub_checks` (making it more approximating when crates built with different flags are mixed), but it no longer affects whether these checks can happen in Miri or compile-time. The first commit just moves things around; I don't think these macros and functions belong into `intrinsics.rs` as they are not intrinsics. r? `@saethlin`
2 parents e50ab29 + 6177530 commit 2f090c3

File tree

48 files changed

+314
-292
lines changed

Some content is hidden

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

48 files changed

+314
-292
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
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::UbCheck(_), _) => {}
2003+
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
20042004

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

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ fn codegen_stmt<'tcx>(
780780
NullOp::OffsetOf(fields) => {
781781
layout.offset_of_subfield(fx, fields.iter()).bytes()
782782
}
783-
NullOp::UbCheck(_) => {
783+
NullOp::UbChecks => {
784784
let val = fx.tcx.sess.opts.debug_assertions;
785785
let val = CValue::by_val(
786786
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
680680
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
681681
bx.cx().const_usize(val)
682682
}
683-
mir::NullOp::UbCheck(_) => {
684-
// In codegen, we want to check for language UB and library UB
683+
mir::NullOp::UbChecks => {
685684
let val = bx.tcx().sess.opts.debug_assertions;
686685
bx.cx().const_bool(val)
687686
}

compiler/rustc_const_eval/src/interpret/step.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,7 @@ 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::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)
271-
}
261+
mir::NullOp::UbChecks => Scalar::from_bool(self.tcx.sess.opts.debug_assertions),
272262
};
273263
self.write_scalar(val, &dest)?;
274264
}

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

+1-1
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::UbCheck(_),
561+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks,
562562
_,
563563
) => {}
564564
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11681168
Rvalue::Repeat(_, _)
11691169
| Rvalue::ThreadLocalRef(_)
11701170
| Rvalue::AddressOf(_, _)
1171-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbCheck(_), _)
1171+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbChecks, _)
11721172
| Rvalue::Discriminant(_) => {}
11731173
}
11741174
self.super_rvalue(rvalue, location);

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ 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::check_language_ub
131-
| sym::check_library_ub
130+
| sym::ub_checks
132131
| sym::fadd_algebraic
133132
| sym::fsub_algebraic
134133
| sym::fmul_algebraic
@@ -571,7 +570,7 @@ pub fn check_intrinsic_type(
571570
(0, 0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
572571
}
573572

574-
sym::check_language_ub | sym::check_library_ub => (0, 1, Vec::new(), tcx.types.bool),
573+
sym::ub_checks => (0, 1, Vec::new(), tcx.types.bool),
575574

576575
sym::simd_eq
577576
| sym::simd_ne

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'tcx> Body<'tcx> {
796796
}
797797

798798
match rvalue {
799-
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => {
799+
Rvalue::NullaryOp(NullOp::UbChecks, _) => {
800800
Some((tcx.sess.opts.debug_assertions as u128, targets))
801801
}
802802
Rvalue::Use(Operand::Constant(constant)) => {

compiler/rustc_middle/src/mir/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
944944
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
945945
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
946946
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
947-
NullOp::UbCheck(kind) => write!(fmt, "UbCheck({kind:?})"),
947+
NullOp::UbChecks => write!(fmt, "UbChecks()"),
948948
}
949949
}
950950
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/syntax.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1367,16 +1367,9 @@ pub enum NullOp<'tcx> {
13671367
AlignOf,
13681368
/// Returns the offset of a field
13691369
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1370-
/// Returns whether we want to check for library UB or language UB at monomorphization time.
1371-
/// Both kinds of UB evaluate to `true` in codegen, and only library UB evalutes to `true` in
1372-
/// const-eval/Miri, because the interpreter has its own better checks for language UB.
1373-
UbCheck(UbKind),
1374-
}
1375-
1376-
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1377-
pub enum UbKind {
1378-
LanguageUb,
1379-
LibraryUb,
1370+
/// Returns whether we want to check for UB.
1371+
/// This returns the value of `cfg!(debug_assertions)` at monomorphization time.
1372+
UbChecks,
13801373
}
13811374

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

compiler/rustc_middle/src/mir/tcx.rs

+1-1
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::UbCheck(_), _) => tcx.types.bool,
197+
Rvalue::NullaryOp(NullOp::UbChecks, _) => 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 => {

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
433433
| Rvalue::Discriminant(..)
434434
| Rvalue::Len(..)
435435
| Rvalue::NullaryOp(
436-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbCheck(_),
436+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbChecks,
437437
_,
438438
) => {}
439439
}

compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
487487
NullOp::OffsetOf(fields) => {
488488
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
489489
}
490-
NullOp::UbCheck(_) => return None,
490+
NullOp::UbChecks => return None,
491491
};
492492
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
493493
let imm = ImmTy::try_from_uint(val, usize_layout)?;

compiler/rustc_mir_transform/src/known_panics_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
639639
NullOp::OffsetOf(fields) => {
640640
op_layout.offset_of_subfield(self, fields.iter()).bytes()
641641
}
642-
NullOp::UbCheck(_) => return None,
642+
NullOp::UbChecks => return None,
643643
};
644644
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
645645
}

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,13 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
2020
sym::unreachable => {
2121
terminator.kind = TerminatorKind::Unreachable;
2222
}
23-
sym::check_language_ub => {
23+
sym::ub_checks => {
2424
let target = target.unwrap();
2525
block.statements.push(Statement {
2626
source_info: terminator.source_info,
2727
kind: StatementKind::Assign(Box::new((
2828
*destination,
29-
Rvalue::NullaryOp(
30-
NullOp::UbCheck(UbKind::LanguageUb),
31-
tcx.types.bool,
32-
),
33-
))),
34-
});
35-
terminator.kind = TerminatorKind::Goto { target };
36-
}
37-
sym::check_library_ub => {
38-
let target = target.unwrap();
39-
block.statements.push(Statement {
40-
source_info: terminator.source_info,
41-
kind: StatementKind::Assign(Box::new((
42-
*destination,
43-
Rvalue::NullaryOp(
44-
NullOp::UbCheck(UbKind::LibraryUb),
45-
tcx.types.bool,
46-
),
29+
Rvalue::NullaryOp(NullOp::UbChecks, tcx.types.bool),
4730
))),
4831
});
4932
terminator.kind = TerminatorKind::Goto { target };

compiler/rustc_mir_transform/src/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl<'tcx> Validator<'_, 'tcx> {
446446
NullOp::SizeOf => {}
447447
NullOp::AlignOf => {}
448448
NullOp::OffsetOf(_) => {}
449-
NullOp::UbCheck(_) => {}
449+
NullOp::UbChecks => {}
450450
},
451451

452452
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,13 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
251251
type T = stable_mir::mir::NullOp;
252252
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
253253
use rustc_middle::mir::NullOp::*;
254-
use rustc_middle::mir::UbKind;
255254
match self {
256255
SizeOf => stable_mir::mir::NullOp::SizeOf,
257256
AlignOf => stable_mir::mir::NullOp::AlignOf,
258257
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
259258
indices.iter().map(|idx| idx.stable(tables)).collect(),
260259
),
261-
UbCheck(UbKind::LanguageUb) => {
262-
stable_mir::mir::NullOp::UbCheck(stable_mir::mir::UbKind::LanguageUb)
263-
}
264-
UbCheck(UbKind::LibraryUb) => {
265-
stable_mir::mir::NullOp::UbCheck(stable_mir::mir::UbKind::LibraryUb)
266-
}
260+
UbChecks => stable_mir::mir::NullOp::UbChecks,
267261
}
268262
}
269263
}

compiler/rustc_span/src/symbol.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,6 @@ symbols! {
518518
cfi,
519519
cfi_encoding,
520520
char,
521-
check_language_ub,
522-
check_library_ub,
523521
client,
524522
clippy,
525523
clobber_abi,
@@ -1867,6 +1865,7 @@ symbols! {
18671865
u8_legacy_fn_max_value,
18681866
u8_legacy_fn_min_value,
18691867
u8_legacy_mod,
1868+
ub_checks,
18701869
unaligned_volatile_load,
18711870
unaligned_volatile_store,
18721871
unboxed_closures,

compiler/stable_mir/src/mir/body.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ impl Rvalue {
621621
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
622622
Ok(Ty::usize_ty())
623623
}
624-
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => Ok(Ty::bool_ty()),
624+
Rvalue::NullaryOp(NullOp::UbChecks, _) => Ok(Ty::bool_ty()),
625625
Rvalue::Aggregate(ak, ops) => match *ak {
626626
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
627627
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -989,13 +989,7 @@ pub enum NullOp {
989989
/// Returns the offset of a field.
990990
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
991991
/// cfg!(debug_assertions), but at codegen time
992-
UbCheck(UbKind),
993-
}
994-
995-
#[derive(Clone, Debug, Eq, PartialEq)]
996-
pub enum UbKind {
997-
LanguageUb,
998-
LibraryUb,
992+
UbChecks,
999993
}
1000994

1001995
impl Operand {

library/core/src/char/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::char::TryFromCharError;
44
use crate::convert::TryFrom;
55
use crate::error::Error;
66
use crate::fmt;
7-
use crate::intrinsics::assert_unsafe_precondition;
87
use crate::mem::transmute;
98
use crate::str::FromStr;
9+
use crate::ub_checks::assert_unsafe_precondition;
1010

1111
/// Converts a `u32` to a `char`. See [`char::from_u32`].
1212
#[must_use]

library/core/src/hint.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! Hints may be compile time or runtime.
55
66
use crate::intrinsics;
7+
use crate::ub_checks;
78

89
/// Informs the compiler that the site which is calling this function is not
910
/// reachable, possibly enabling further optimizations.
@@ -98,7 +99,7 @@ use crate::intrinsics;
9899
#[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")]
99100
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
100101
pub const unsafe fn unreachable_unchecked() -> ! {
101-
intrinsics::assert_unsafe_precondition!(
102+
ub_checks::assert_unsafe_precondition!(
102103
check_language_ub,
103104
"hint::unreachable_unchecked must never be reached",
104105
() => false
@@ -148,7 +149,7 @@ pub const unsafe fn unreachable_unchecked() -> ! {
148149
pub const unsafe fn assert_unchecked(cond: bool) {
149150
// SAFETY: The caller promised `cond` is true.
150151
unsafe {
151-
intrinsics::assert_unsafe_precondition!(
152+
ub_checks::assert_unsafe_precondition!(
152153
check_language_ub,
153154
"hint::assert_unchecked must never be called when the condition is false",
154155
(cond: bool = cond) => cond,

0 commit comments

Comments
 (0)