From 498ebc46baf1d6f588f3b241f779a42edecf79be Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 14 Nov 2021 13:08:51 -0500 Subject: [PATCH] require full validity when determining the discriminant of a value --- compiler/rustc_const_eval/src/interpret/intrinsics.rs | 6 ++++++ compiler/rustc_const_eval/src/interpret/step.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index 698742fe98ceb..5e7bbc0113271 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -265,6 +265,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } sym::discriminant_value => { let place = self.deref_operand(&args[0])?; + if M::enforce_validity(self) { + // This is 'using' the value, so make sure the validity invariant is satisfied. + // (Also see https://github.com/rust-lang/rust/pull/89764.) + self.validate_operand(&place.into())?; + } + let discr_val = self.read_discriminant(&place.into())?.0; self.write_scalar(discr_val, dest)?; } diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index e6037d561dedc..2759a7d9d268f 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -304,6 +304,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Discriminant(place) => { let op = self.eval_place_to_op(place, None)?; + if M::enforce_validity(self) { + // This is 'using' the value, so make sure the validity invariant is satisfied. + // (Also see https://github.com/rust-lang/rust/pull/89764.) + self.validate_operand(&op)?; + } + let discr_val = self.read_discriminant(&op)?.0; self.write_scalar(discr_val, &dest)?; }