Skip to content

Commit 28e684b

Browse files
committed
Auto merge of #127995 - workingjubilee:say-turings-prayer, r=BoxyUwU
compiler: Never debug_assert in codegen In the name of Turing and his Hoarey heralds, assert our truths before creating a monster! The `rustc_codegen_llvm` and `rustc_codegen_ssa` crates are fairly critical for rustc's correctness. Small mistakes here can easily result in undefined behavior, since a "small mistake" can mean something like "link and execute the wrong code". We should probably run any and all asserts in these modules unconditionally on whether this is a "debug build", and damn the costs in performance. ...Especially because the costs in performance seem to be *nothing*. It is not clear how much correctness we gain here, but I'll take free correctness improvements.
2 parents 004e155 + ce7b069 commit 28e684b

File tree

16 files changed

+57
-65
lines changed

16 files changed

+57
-65
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'ll> CodegenCx<'ll, '_> {
330330

331331
// If this assertion triggers, there's something wrong with commandline
332332
// argument validation.
333-
debug_assert!(
333+
assert!(
334334
!(self.tcx.sess.opts.cg.linker_plugin_lto.enabled()
335335
&& self.tcx.sess.target.is_like_windows
336336
&& self.tcx.sess.opts.cg.prefer_dynamic)

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
170170
) -> DINodeCreationResult<'ll> {
171171
// The debuginfo generated by this function is only valid if `ptr_type` is really just
172172
// a (fat) pointer. Make sure it is not called for e.g. `Box<T, NonZSTAllocator>`.
173-
debug_assert_eq!(
173+
assert_eq!(
174174
cx.size_and_align_of(ptr_type),
175175
cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))
176176
);
@@ -185,7 +185,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
185185
match fat_pointer_kind(cx, pointee_type) {
186186
None => {
187187
// This is a thin pointer. Create a regular pointer type and give it the correct name.
188-
debug_assert_eq!(
188+
assert_eq!(
189189
(data_layout.pointer_size, data_layout.pointer_align.abi),
190190
cx.size_and_align_of(ptr_type),
191191
"ptr_type={ptr_type}, pointee_type={pointee_type}",
@@ -240,8 +240,8 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
240240
FatPtrKind::Slice => ("data_ptr", "length"),
241241
};
242242

243-
debug_assert_eq!(abi::FAT_PTR_ADDR, 0);
244-
debug_assert_eq!(abi::FAT_PTR_EXTRA, 1);
243+
assert_eq!(abi::FAT_PTR_ADDR, 0);
244+
assert_eq!(abi::FAT_PTR_EXTRA, 1);
245245

246246
// The data pointer type is a regular, thin pointer, regardless of whether this
247247
// is a slice or a trait object.
@@ -498,7 +498,7 @@ pub fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll D
498498
}
499499
};
500500

501-
debug_assert_eq!(di_node_for_uid as *const _, di_node as *const _);
501+
assert_eq!(di_node_for_uid as *const _, di_node as *const _);
502502
} else {
503503
debug_context(cx).type_map.insert(unique_type_id, di_node);
504504
}
@@ -1060,7 +1060,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10601060
let ty::Adt(adt_def, _) = struct_type.kind() else {
10611061
bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
10621062
};
1063-
debug_assert!(adt_def.is_struct());
1063+
assert!(adt_def.is_struct());
10641064
let containing_scope = get_namespace_for_item(cx, adt_def.did());
10651065
let struct_type_and_layout = cx.layout_of(struct_type);
10661066
let variant_def = adt_def.non_enum_variant();
@@ -1130,7 +1130,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
11301130
}
11311131
};
11321132

1133-
debug_assert!(
1133+
assert!(
11341134
up_var_tys.iter().all(|t| t == cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t))
11351135
);
11361136

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
204204
let enum_type_and_layout = cx.layout_of(enum_type);
205205
let enum_type_name = compute_debuginfo_type_name(cx.tcx, enum_type, false);
206206

207-
debug_assert!(!wants_c_like_enum_debuginfo(enum_type_and_layout));
207+
assert!(!wants_c_like_enum_debuginfo(enum_type_and_layout));
208208

209209
type_map::build_type_with_children(
210210
cx,
@@ -279,7 +279,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
279279
let coroutine_type_and_layout = cx.layout_of(coroutine_type);
280280
let coroutine_type_name = compute_debuginfo_type_name(cx.tcx, coroutine_type, false);
281281

282-
debug_assert!(!wants_c_like_enum_debuginfo(coroutine_type_and_layout));
282+
assert!(!wants_c_like_enum_debuginfo(coroutine_type_and_layout));
283283

284284
type_map::build_type_with_children(
285285
cx,
@@ -517,7 +517,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
517517
if is_128_bits {
518518
DiscrKind::Exact128(discr_val)
519519
} else {
520-
debug_assert_eq!(discr_val, discr_val as u64 as u128);
520+
assert_eq!(discr_val, discr_val as u64 as u128);
521521
DiscrKind::Exact(discr_val as u64)
522522
}
523523
}
@@ -526,8 +526,8 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
526526
if is_128_bits {
527527
DiscrKind::Range128(min, max)
528528
} else {
529-
debug_assert_eq!(min, min as u64 as u128);
530-
debug_assert_eq!(max, max as u64 as u128);
529+
assert_eq!(min, min as u64 as u128);
530+
assert_eq!(max, max as u64 as u128);
531531
DiscrKind::Range(min as u64, max as u64)
532532
}
533533
}
@@ -815,7 +815,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
815815
}
816816
}));
817817

818-
debug_assert_eq!(
818+
assert_eq!(
819819
cx.size_and_align_of(enum_type_and_layout.field(cx, tag_field).ty),
820820
cx.size_and_align_of(super::tag_base_type(cx, enum_type_and_layout))
821821
);

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn tag_base_type<'ll, 'tcx>(
106106
cx: &CodegenCx<'ll, 'tcx>,
107107
enum_type_and_layout: TyAndLayout<'tcx>,
108108
) -> Ty<'tcx> {
109-
debug_assert!(match enum_type_and_layout.ty.kind() {
109+
assert!(match enum_type_and_layout.ty.kind() {
110110
ty::Coroutine(..) => true,
111111
ty::Adt(adt_def, _) => adt_def.is_enum(),
112112
_ => false,
@@ -251,7 +251,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
251251
variant_layout: TyAndLayout<'tcx>,
252252
di_flags: DIFlags,
253253
) -> &'ll DIType {
254-
debug_assert_eq!(variant_layout.ty, enum_type_and_layout.ty);
254+
assert_eq!(variant_layout.ty, enum_type_and_layout.ty);
255255

256256
type_map::build_type_with_children(
257257
cx,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/native.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
6565

6666
let visibility_flags = visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did());
6767

68-
debug_assert!(!wants_c_like_enum_debuginfo(enum_type_and_layout));
68+
assert!(!wants_c_like_enum_debuginfo(enum_type_and_layout));
6969

7070
type_map::build_type_with_children(
7171
cx,
@@ -142,7 +142,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
142142
let containing_scope = get_namespace_for_item(cx, coroutine_def_id);
143143
let coroutine_type_and_layout = cx.layout_of(coroutine_type);
144144

145-
debug_assert!(!wants_c_like_enum_debuginfo(coroutine_type_and_layout));
145+
assert!(!wants_c_like_enum_debuginfo(coroutine_type_and_layout));
146146

147147
let coroutine_type_name = compute_debuginfo_type_name(cx.tcx, coroutine_type, false);
148148

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod private {
3636

3737
/// A unique identifier for anything that we create a debuginfo node for.
3838
/// The types it contains are expected to already be normalized (which
39-
/// is debug_asserted in the constructors).
39+
/// is asserted in the constructors).
4040
///
4141
/// Note that there are some things that only show up in debuginfo, like
4242
/// the separate type descriptions for each enum variant. These get an ID
@@ -58,12 +58,12 @@ pub(super) enum UniqueTypeId<'tcx> {
5858

5959
impl<'tcx> UniqueTypeId<'tcx> {
6060
pub fn for_ty(tcx: TyCtxt<'tcx>, t: Ty<'tcx>) -> Self {
61-
debug_assert_eq!(t, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t));
61+
assert_eq!(t, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t));
6262
UniqueTypeId::Ty(t, private::HiddenZst)
6363
}
6464

6565
pub fn for_enum_variant_part(tcx: TyCtxt<'tcx>, enum_ty: Ty<'tcx>) -> Self {
66-
debug_assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
66+
assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
6767
UniqueTypeId::VariantPart(enum_ty, private::HiddenZst)
6868
}
6969

@@ -72,7 +72,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
7272
enum_ty: Ty<'tcx>,
7373
variant_idx: VariantIdx,
7474
) -> Self {
75-
debug_assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
75+
assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
7676
UniqueTypeId::VariantStructType(enum_ty, variant_idx, private::HiddenZst)
7777
}
7878

@@ -81,7 +81,7 @@ impl<'tcx> UniqueTypeId<'tcx> {
8181
enum_ty: Ty<'tcx>,
8282
variant_idx: VariantIdx,
8383
) -> Self {
84-
debug_assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
84+
assert_eq!(enum_ty, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), enum_ty));
8585
UniqueTypeId::VariantStructTypeCppLikeWrapper(enum_ty, variant_idx, private::HiddenZst)
8686
}
8787

@@ -90,11 +90,8 @@ impl<'tcx> UniqueTypeId<'tcx> {
9090
self_type: Ty<'tcx>,
9191
implemented_trait: Option<PolyExistentialTraitRef<'tcx>>,
9292
) -> Self {
93-
debug_assert_eq!(
94-
self_type,
95-
tcx.normalize_erasing_regions(ParamEnv::reveal_all(), self_type)
96-
);
97-
debug_assert_eq!(
93+
assert_eq!(self_type, tcx.normalize_erasing_regions(ParamEnv::reveal_all(), self_type));
94+
assert_eq!(
9895
implemented_trait,
9996
tcx.normalize_erasing_regions(ParamEnv::reveal_all(), implemented_trait)
10097
);
@@ -252,10 +249,7 @@ pub(super) fn build_type_with_children<'ll, 'tcx>(
252249
members: impl FnOnce(&CodegenCx<'ll, 'tcx>, &'ll DIType) -> SmallVec<&'ll DIType>,
253250
generics: impl FnOnce(&CodegenCx<'ll, 'tcx>) -> SmallVec<&'ll DIType>,
254251
) -> DINodeCreationResult<'ll> {
255-
debug_assert_eq!(
256-
debug_context(cx).type_map.di_node_for_unique_id(stub_info.unique_type_id),
257-
None
258-
);
252+
assert_eq!(debug_context(cx).type_map.di_node_for_unique_id(stub_info.unique_type_id), None);
259253

260254
debug_context(cx).type_map.insert(stub_info.unique_type_id, stub_info.metadata);
261255

compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) fn fat_pointer_kind<'ll, 'tcx>(
8181
ty::Dynamic(..) => Some(FatPtrKind::Dyn),
8282
ty::Foreign(_) => {
8383
// Assert that pointers to foreign types really are thin:
84-
debug_assert_eq!(
84+
assert_eq!(
8585
cx.size_of(Ty::new_imm_ptr(cx.tcx, pointee_tail_ty)),
8686
cx.size_of(Ty::new_imm_ptr(cx.tcx, cx.tcx.types.u8))
8787
);

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn exported_symbols_provider_local(
352352
}
353353
MonoItem::Fn(Instance { def: InstanceKind::DropGlue(def_id, Some(ty)), args }) => {
354354
// A little sanity-check
355-
debug_assert_eq!(
355+
assert_eq!(
356356
args.non_erasable_generics(tcx, def_id).next(),
357357
Some(GenericArgKind::Type(ty))
358358
);
@@ -370,7 +370,7 @@ fn exported_symbols_provider_local(
370370
args,
371371
}) => {
372372
// A little sanity-check
373-
debug_assert_eq!(
373+
assert_eq!(
374374
args.non_erasable_generics(tcx, def_id).next(),
375375
Some(GenericArgKind::Type(ty))
376376
);
@@ -462,7 +462,7 @@ fn upstream_monomorphizations_for_provider(
462462
tcx: TyCtxt<'_>,
463463
def_id: DefId,
464464
) -> Option<&UnordMap<GenericArgsRef<'_>, CrateNum>> {
465-
debug_assert!(!def_id.is_local());
465+
assert!(!def_id.is_local());
466466
tcx.upstream_monomorphizations(()).get(&def_id)
467467
}
468468

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15121512
// We reduce the `running` counter by one. The
15131513
// `tokens.truncate()` below will take care of
15141514
// giving the Token back.
1515-
debug_assert!(running_with_own_token > 0);
1515+
assert!(running_with_own_token > 0);
15161516
running_with_own_token -= 1;
15171517
main_thread_state = MainThreadState::Lending;
15181518
}

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn push_debuginfo_type_name<'tcx>(
459459
output: &mut String,
460460
visited: &mut FxHashSet<Ty<'tcx>>,
461461
) {
462-
debug_assert!(!wants_c_like_enum_debuginfo(ty_and_layout));
462+
assert!(!wants_c_like_enum_debuginfo(ty_and_layout));
463463
output.push_str("enum2$<");
464464
push_inner(output, visited);
465465
push_close_angle_bracket(true, output);
@@ -660,7 +660,7 @@ fn push_generic_params_internal<'tcx>(
660660
output: &mut String,
661661
visited: &mut FxHashSet<Ty<'tcx>>,
662662
) -> bool {
663-
debug_assert_eq!(args, tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), args));
663+
assert_eq!(args, tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), args));
664664
let mut args = args.non_erasable_generics(tcx, def_id).peekable();
665665
if args.peek().is_none() {
666666
return false;

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
8484
}
8585
if is_cleanupret {
8686
// Cross-funclet jump - need a trampoline
87-
debug_assert!(base::wants_new_eh_instructions(fx.cx.tcx().sess));
87+
assert!(base::wants_new_eh_instructions(fx.cx.tcx().sess));
8888
debug!("llbb_with_cleanup: creating cleanup trampoline for {:?}", target);
8989
let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target);
9090
let trampoline_llbb = Bx::append_block(fx.cx, fx.llfn, name);

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn calculate_debuginfo_offset<
194194
}
195195
_ => {
196196
// Sanity check for `can_use_in_debuginfo`.
197-
debug_assert!(!elem.can_use_in_debuginfo());
197+
assert!(!elem.can_use_in_debuginfo());
198198
bug!("unsupported var debuginfo projection `{:?}`", projection)
199199
}
200200
}
@@ -502,7 +502,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
502502

503503
let DebugInfoOffset { direct_offset, indirect_offsets, result: fragment_layout } =
504504
calculate_debuginfo_offset(bx, &fragment.projection, var_layout);
505-
debug_assert!(indirect_offsets.is_empty());
505+
assert!(indirect_offsets.is_empty());
506506

507507
if fragment_layout.size == Size::ZERO {
508508
// Fragment is a ZST, so does not represent anything. Avoid generating anything

compiler/rustc_codegen_ssa/src/mir/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
565565
for elem in place_ref.projection.iter() {
566566
match elem {
567567
mir::ProjectionElem::Field(ref f, _) => {
568-
debug_assert!(
568+
assert!(
569569
!o.layout.ty.is_any_ptr(),
570570
"Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
571571
but tried to access field {f:?} of pointer {o:?}",

compiler/rustc_codegen_ssa/src/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<V: CodegenObject> PlaceValue<V> {
5555

5656
/// Creates a `PlaceRef` to this location with the given type.
5757
pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
58-
debug_assert!(
58+
assert!(
5959
layout.is_unsized() || layout.abi.is_uninhabited() || self.llextra.is_none(),
6060
"Had pointer metadata {:?} for sized type {layout:?}",
6161
self.llextra,
@@ -488,7 +488,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
488488
cg_base = match *elem {
489489
mir::ProjectionElem::Deref => bx.load_operand(cg_base).deref(bx.cx()),
490490
mir::ProjectionElem::Field(ref field, _) => {
491-
debug_assert!(
491+
assert!(
492492
!cg_base.layout.ty.is_any_ptr(),
493493
"Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
494494
but tried to access field {field:?} of pointer {cg_base:?}",

0 commit comments

Comments
 (0)