Skip to content

Commit 6404d29

Browse files
authored
Rollup merge of #143088 - firefighterduck:improve-doc-discr-tag, r=RalfJung
Improve documentation of `TagEncoding` This PR is follow-up from the [discussion here](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/.E2.9C.94.20VariantId.3DDiscriminant.20when.20tag.20is.20niche.20encoded.3F/with/524384295). It aims at making the `TagEncoding` documentation less ambiguous and more detailed with references to relevant implementation sides. It especially clears up the ambiguous use of discriminant/variant index, which sparked the discussion referenced above. PS: While working with layout data, I somehow ended up looking at the docs for `FakeBorrowKind` and noticed that the one example was not in a doc comment. I hope that this is minor enough of a fix for it to be okay in this otherwise unrelated PR.
2 parents fd0062c + 1c25bfb commit 6404d29

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,24 +1592,33 @@ pub enum TagEncoding<VariantIdx: Idx> {
15921592
/// (so converting the tag to the discriminant can require sign extension).
15931593
Direct,
15941594

1595-
/// Niche (values invalid for a type) encoding the discriminant:
1596-
/// Discriminant and variant index coincide.
1595+
/// Niche (values invalid for a type) encoding the discriminant.
1596+
/// Note that for this encoding, the discriminant and variant index of each variant coincide!
1597+
/// This invariant is codified as part of [`layout_sanity_check`](../rustc_ty_utils/layout/invariant/fn.layout_sanity_check.html).
1598+
///
15971599
/// The variant `untagged_variant` contains a niche at an arbitrary
1598-
/// offset (field `tag_field` of the enum), which for a variant with
1599-
/// discriminant `d` is set to
1600-
/// `(d - niche_variants.start).wrapping_add(niche_start)`
1601-
/// (this is wrapping arithmetic using the type of the niche field).
1600+
/// offset (field [`Variants::Multiple::tag_field`] of the enum).
1601+
/// For a variant with variant index `i`, such that `i != untagged_variant`,
1602+
/// the tag is set to `(i - niche_variants.start).wrapping_add(niche_start)`
1603+
/// (this is wrapping arithmetic using the type of the niche field, cf. the
1604+
/// [`tag_for_variant`](../rustc_const_eval/interpret/struct.InterpCx.html#method.tag_for_variant)
1605+
/// query implementation).
1606+
/// 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
1608+
/// `niche_variants`, the tag must have encoded the `untagged_variant`.
16021609
///
1603-
/// For example, `Option<(usize, &T)>` is represented such that
1604-
/// `None` has a null pointer for the second tuple field, and
1605-
/// `Some` is the identity function (with a non-null reference).
1610+
/// For example, `Option<(usize, &T)>` is represented such that the tag for
1611+
/// `None` is the null pointer in the second tuple field, and
1612+
/// `Some` is the identity function (with a non-null reference)
1613+
/// and has no additional tag, i.e. the reference being non-null uniquely identifies this variant.
16061614
///
16071615
/// Other variants that are not `untagged_variant` and that are outside the `niche_variants`
16081616
/// range cannot be represented; they must be uninhabited.
1617+
/// Nonetheless, uninhabited variants can also fall into the range of `niche_variants`.
16091618
Niche {
16101619
untagged_variant: VariantIdx,
1611-
/// This range *may* contain `untagged_variant`; that is then just a "dead value" and
1612-
/// not used to encode anything.
1620+
/// This range *may* contain `untagged_variant` or uninhabited variants;
1621+
/// these are then just "dead values" and not used to encode anything.
16131622
niche_variants: RangeInclusive<VariantIdx>,
16141623
/// This is inbounds of the type of the niche field
16151624
/// (not sign-extended, i.e., all bits beyond the niche field size are 0).

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_middle/src/mir/syntax.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,15 @@ pub enum FakeBorrowKind {
284284
///
285285
/// This is used when lowering deref patterns, where shallow borrows wouldn't prevent something
286286
/// like:
287-
// ```compile_fail
288-
// let mut b = Box::new(false);
289-
// match b {
290-
// deref!(true) => {} // not reached because `*b == false`
291-
// _ if { *b = true; false } => {} // not reached because the guard is `false`
292-
// deref!(false) => {} // not reached because the guard changed it
293-
// // UB because we reached the unreachable.
294-
// }
295-
// ```
287+
/// ```compile_fail
288+
/// let mut b = Box::new(false);
289+
/// match b {
290+
/// deref!(true) => {} // not reached because `*b == false`
291+
/// _ if { *b = true; false } => {} // not reached because the guard is `false`
292+
/// deref!(false) => {} // not reached because the guard changed it
293+
/// // UB because we reached the unreachable.
294+
/// }
295+
/// ```
296296
Deep,
297297
}
298298

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)