diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-03-08 13:22:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-08 13:22:29 +0100 |
| commit | 8abeac230af348148f1fd4f20ea6fd1045b941e9 (patch) | |
| tree | 0fab5767d64ddb664bc99c1edae40a94ea92778f | |
| parent | a08a5d429276cb4f0feaadbee538997cae90e897 (diff) | |
| parent | 2e5f86c6a7e2e24ab390e046c70c5b65c750da30 (diff) | |
| download | rust-8abeac230af348148f1fd4f20ea6fd1045b941e9.tar.gz rust-8abeac230af348148f1fd4f20ea6fd1045b941e9.zip | |
Rollup merge of #122183 - RalfJung:read-discriminant-uninhabited-variant, r=tmiasko
interpret: update comment about read_discriminant on uninhabited variants
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/discriminant.rs | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/discriminant.rs b/compiler/rustc_const_eval/src/interpret/discriminant.rs index e951a77611c..6d4f6d0cb3c 100644 --- a/compiler/rustc_const_eval/src/interpret/discriminant.rs +++ b/compiler/rustc_const_eval/src/interpret/discriminant.rs @@ -10,6 +10,8 @@ use super::{ImmTy, InterpCx, InterpResult, Machine, Readable, Scalar, Writeable} impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { /// Writes the discriminant of the given variant. + /// + /// If the variant is uninhabited, this is UB. #[instrument(skip(self), level = "trace")] pub fn write_discriminant( &mut self, @@ -102,6 +104,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { /// Read discriminant, return the runtime value as well as the variant index. /// Can also legally be called on non-enums (e.g. through the discriminant_value intrinsic)! + /// + /// Will never return an uninhabited variant. #[instrument(skip(self), level = "trace")] pub fn read_discriminant( &self, @@ -244,7 +248,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { variant } }; - // 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. + // Reading the discriminant of an uninhabited variant is UB. This is the basis for the + // `uninhabited_enum_branching` MIR pass. It also ensures consistency with + // `write_discriminant`. if op.layout().for_variant(self, index).abi.is_uninhabited() { throw_ub!(UninhabitedEnumVariantRead(index)) } |
