Skip to content

Commit 1c25bfb

Browse files
move discr=varid check to layout_sanity_check
1 parent 3d96897 commit 1c25bfb

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,17 +1594,17 @@ pub enum TagEncoding<VariantIdx: Idx> {
15941594

15951595
/// Niche (values invalid for a type) encoding the discriminant.
15961596
/// Note that for this encoding, the discriminant and variant index of each variant coincide!
1597-
/// (This gets checked, for example, in [codegen_ssa](https://github.com/rust-lang/rust/blob/df32e15c56f582eb2bdde07711af6271f2ae660b/compiler/rustc_codegen_ssa/src/mir/operand.rs#L485).)
1597+
/// This invariant is codified as part of [`layout_sanity_check`](../rustc_ty_utils/layout/invariant/fn.layout_sanity_check.html).
15981598
///
15991599
/// The variant `untagged_variant` contains a niche at an arbitrary
16001600
/// offset (field [`Variants::Multiple::tag_field`] of the enum).
1601-
/// For a variant with variant index `i`, such that `i!=untagged_variant`,
1601+
/// For a variant with variant index `i`, such that `i != untagged_variant`,
16021602
/// the tag is set to `(i - niche_variants.start).wrapping_add(niche_start)`
16031603
/// (this is wrapping arithmetic using the type of the niche field, cf. the
16041604
/// [`tag_for_variant`](../rustc_const_eval/interpret/struct.InterpCx.html#method.tag_for_variant)
16051605
/// query implementation).
16061606
/// To recover the variant index `i` from a `tag`, the above formula has to be reversed,
1607-
/// i.e. `i = (tag.wrapping_sub(niche_start))+niche_variants.start`. If `i` ends up outside
1607+
/// i.e. `i = tag.wrapping_sub(niche_start) + niche_variants.start`. If `i` ends up outside
16081608
/// `niche_variants`, the tag must have encoded the `untagged_variant`.
16091609
///
16101610
/// For example, `Option<(usize, &T)>` is represented such that the tag for

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -479,17 +479,8 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
479479
_ => (tag_imm, bx.cx().immediate_backend_type(tag_op.layout)),
480480
};
481481

482-
// Layout ensures that we only get here for cases where the discriminant
482+
// `layout_sanity_check` ensures that we only get here for cases where the discriminant
483483
// value and the variant index match, since that's all `Niche` can encode.
484-
// But for emphasis and debugging, let's double-check one anyway.
485-
debug_assert_eq!(
486-
self.layout
487-
.ty
488-
.discriminant_for_variant(bx.tcx(), untagged_variant)
489-
.unwrap()
490-
.val,
491-
u128::from(untagged_variant.as_u32()),
492-
);
493484

494485
let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
495486

compiler/rustc_ty_utils/src/layout/invariant.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou
277277
if !variant.is_uninhabited() {
278278
assert!(idx == *untagged_variant || niche_variants.contains(&idx));
279279
}
280+
281+
// Ensure that for niche encoded tags the discriminant coincides with the variant index.
282+
assert_eq!(
283+
layout.ty.discriminant_for_variant(tcx, idx).unwrap().val,
284+
u128::from(idx.as_u32()),
285+
);
280286
}
281287
}
282288
for variant in variants.iter() {

0 commit comments

Comments
 (0)