Skip to content

Commit 6c464c5

Browse files
committed
interpret: update comment about read_discriminant on uninhabited variants
1 parent 9fb91aa commit 6c464c5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

compiler/rustc_const_eval/src/interpret/discriminant.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use super::{ImmTy, InterpCx, InterpResult, Machine, Readable, Scalar, Writeable}
1010

1111
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1212
/// Writes the discriminant of the given variant.
13+
///
14+
/// If the variant is uninhabited, this is UB.
1315
#[instrument(skip(self), level = "trace")]
1416
pub fn write_discriminant(
1517
&mut self,
@@ -102,6 +104,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
102104

103105
/// Read discriminant, return the runtime value as well as the variant index.
104106
/// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)!
107+
///
108+
/// Will never return an uninhabited variant.
105109
#[instrument(skip(self), level = "trace")]
106110
pub fn read_discriminant(
107111
&self,
@@ -244,7 +248,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
244248
variant
245249
}
246250
};
247-
// For consistency with `write_discriminant`, and to make sure that `project_downcast` cannot fail due to strange layouts, we declare immediate UB for uninhabited variants.
251+
// Reading the discriminant of an uninhabited variant is UB. This is the basis for the
252+
// `uninhabited_enum_branching` MIR pass. It also ensures consistency with
253+
// `write_discriminant`, and makes sure that `project_downcast` cannot fail due to strange
254+
// layouts.
248255
if op.layout().for_variant(self, index).abi.is_uninhabited() {
249256
throw_ub!(UninhabitedEnumVariantRead(index))
250257
}

0 commit comments

Comments
 (0)