Skip to content

Commit 6177530

Browse files
committed
refactor check_{lang,library}_ub: use a single intrinsic, put policy into library
1 parent 987ef4c commit 6177530

34 files changed

+134
-119
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::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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 11 deletions
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

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::UbCheck(_),
561+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbChecks,
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
@@ -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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 10 deletions
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

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::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 => {

0 commit comments

Comments
 (0)