Skip to content

Commit 2b15dd1

Browse files
committed
Add more comments to discriminant calculations.
1 parent 143f393 commit 2b15dd1

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: compiler/rustc_codegen_ssa/src/mir/operand.rs

+9
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,26 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
428428
let dl = &bx.tcx().data_layout;
429429
let cast_to_layout = bx.cx().layout_of(cast_to);
430430
let cast_to = bx.cx().immediate_backend_type(cast_to_layout);
431+
432+
// We check uninhabitedness separately because a type like
433+
// `enum Foo { Bar(i32, !) }` is still reported as `Variants::Single`,
434+
// *not* as `Variants::Empty`.
431435
if self.layout.is_uninhabited() {
432436
return bx.cx().const_poison(cast_to);
433437
}
438+
434439
let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants {
435440
Variants::Empty => unreachable!("we already handled uninhabited types"),
436441
Variants::Single { index } => {
437442
let discr_val =
438443
if let Some(discr) = self.layout.ty.discriminant_for_variant(bx.tcx(), index) {
439444
discr.val
440445
} else {
446+
// This arm is for types which are neither enums nor coroutines,
447+
// and thus for which the only possible "variant" should be the first one.
441448
assert_eq!(index, FIRST_VARIANT);
449+
// There's thus no actual discriminant to return, so we return
450+
// what it would have been if this was a single-variant enum.
442451
0
443452
};
444453
return bx.cx().const_uint_big(cast_to, discr_val);

0 commit comments

Comments
 (0)