Skip to content

Commit f00755e

Browse files
committed
Auto merge of #122629 - RalfJung:assert-unsafe-precondition, r=<try>
refactor check_{lang,library}_ub: use a single intrinsics 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 1eb882e + 69e8455 commit f00755e

File tree

33 files changed

+254
-285
lines changed

33 files changed

+254
-285
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_ssa/src/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ 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::UbCheck(_) => {
688+
mir::NullOp::UbChecks => {
689689
// In codegen, we want to check for language UB and library UB
690690
let val = bx.tcx().sess.opts.debug_assertions;
691691
bx.cx().const_bool(val)

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
@@ -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::UbCheck(_), _)
1160+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbChecks, _)
11611161
| Rvalue::Discriminant(_) => {}
11621162
}
11631163
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
@@ -585,7 +584,7 @@ pub fn check_intrinsic_type(
585584
(0, 0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
586585
}
587586

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

590589
sym::simd_eq
591590
| sym::simd_ne

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl<'tcx> Body<'tcx> {
762762
}
763763

764764
match rvalue {
765-
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => {
765+
Rvalue::NullaryOp(NullOp::UbChecks, _) => {
766766
Some((tcx.sess.opts.debug_assertions as u128, targets))
767767
}
768768
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
@@ -1366,16 +1366,9 @@ pub enum NullOp<'tcx> {
13661366
AlignOf,
13671367
/// Returns the offset of a field
13681368
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
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,
1369+
/// Returns whether we want to check for UB at monomorphization time.
1370+
/// This returns the value of `cfg!(debug_assertions)`.
1371+
UbChecks,
13791372
}
13801373

13811374
#[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
@@ -488,7 +488,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
488488
NullOp::OffsetOf(fields) => {
489489
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
490490
}
491-
NullOp::UbCheck(_) => return None,
491+
NullOp::UbChecks => return None,
492492
};
493493
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
494494
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,
@@ -1865,6 +1863,7 @@ symbols! {
18651863
u8_legacy_fn_max_value,
18661864
u8_legacy_fn_min_value,
18671865
u8_legacy_mod,
1866+
ub_checks,
18681867
unaligned_volatile_load,
18691868
unaligned_volatile_store,
18701869
unboxed_closures,

compiler/stable_mir/src/mir/body.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl Rvalue {
639639
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
640640
Ok(Ty::usize_ty())
641641
}
642-
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => Ok(Ty::bool_ty()),
642+
Rvalue::NullaryOp(NullOp::UbChecks, _) => Ok(Ty::bool_ty()),
643643
Rvalue::Aggregate(ak, ops) => match *ak {
644644
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
645645
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1007,13 +1007,7 @@ pub enum NullOp {
10071007
/// Returns the offset of a field.
10081008
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
10091009
/// cfg!(debug_assertions), but at codegen time
1010-
UbCheck(UbKind),
1011-
}
1012-
1013-
#[derive(Clone, Debug, Eq, PartialEq)]
1014-
pub enum UbKind {
1015-
LanguageUb,
1016-
LibraryUb,
1010+
UbChecks,
10171011
}
10181012

10191013
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)