Skip to content

Commit 18865ff

Browse files
authored
Unrolled build for rust-lang#122933
Rollup merge of rust-lang#122933 - RalfJung:tag_for_variant, r=oli-obk tag_for_variant follow-ups Follow-up to rust-lang#122784, mostly to clarify the doc comment.
2 parents d6eb0f5 + 928bd3b commit 18865ff

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use either::{Left, Right};
33
use rustc_hir::def::DefKind;
44
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo};
55
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
6-
use rustc_middle::query::{Key, TyCtxtAt};
6+
use rustc_middle::query::TyCtxtAt;
77
use rustc_middle::traits::Reveal;
88
use rustc_middle::ty::layout::LayoutOf;
99
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -243,24 +243,6 @@ pub(crate) fn turn_into_const_value<'tcx>(
243243
op_to_const(&ecx, &mplace.into(), /* for diagnostics */ false)
244244
}
245245

246-
/// Computes the tag (if any) for a given type and variant.
247-
#[instrument(skip(tcx), level = "debug")]
248-
pub fn tag_for_variant_provider<'tcx>(
249-
tcx: TyCtxt<'tcx>,
250-
(ty, variant_index): (Ty<'tcx>, abi::VariantIdx),
251-
) -> Option<ty::ScalarInt> {
252-
assert!(ty.is_enum());
253-
254-
let ecx = InterpCx::new(
255-
tcx,
256-
ty.default_span(tcx),
257-
ty::ParamEnv::reveal_all(),
258-
crate::const_eval::DummyMachine,
259-
);
260-
261-
ecx.tag_for_variant(ty, variant_index).unwrap().map(|(tag, _tag_field)| tag)
262-
}
263-
264246
#[instrument(skip(tcx), level = "debug")]
265247
pub fn eval_to_const_value_raw_provider<'tcx>(
266248
tcx: TyCtxt<'tcx>,

compiler/rustc_const_eval/src/const_eval/mod.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
use rustc_middle::mir;
44
use rustc_middle::mir::interpret::InterpErrorInfo;
5-
use rustc_middle::query::TyCtxtAt;
6-
use rustc_middle::ty::{self, Ty};
5+
use rustc_middle::query::{Key, TyCtxtAt};
6+
use rustc_middle::ty::{self, Ty, TyCtxt};
7+
use rustc_target::abi::VariantIdx;
78

8-
use crate::interpret::format_interp_error;
9+
use crate::interpret::{format_interp_error, InterpCx};
910

1011
mod dummy_machine;
1112
mod error;
@@ -77,3 +78,21 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
7778

7879
Some(mir::DestructuredConstant { variant, fields })
7980
}
81+
82+
/// Computes the tag (if any) for a given type and variant.
83+
#[instrument(skip(tcx), level = "debug")]
84+
pub fn tag_for_variant_provider<'tcx>(
85+
tcx: TyCtxt<'tcx>,
86+
(ty, variant_index): (Ty<'tcx>, VariantIdx),
87+
) -> Option<ty::ScalarInt> {
88+
assert!(ty.is_enum());
89+
90+
let ecx = InterpCx::new(
91+
tcx,
92+
ty.default_span(tcx),
93+
ty::ParamEnv::reveal_all(),
94+
crate::const_eval::DummyMachine,
95+
);
96+
97+
ecx.tag_for_variant(ty, variant_index).unwrap().map(|(tag, _tag_field)| tag)
98+
}

compiler/rustc_const_eval/src/interpret/discriminant.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3838
}
3939
None => {
4040
// No need to write the tag here, because an untagged variant is
41-
// implicitly encoded. For `Niche`-optimized enums, it's by
41+
// implicitly encoded. For `Niche`-optimized enums, this works by
4242
// simply by having a value that is outside the niche variants.
4343
// But what if the data stored here does not actually encode
4444
// this variant? That would be bad! So let's double-check...
@@ -227,8 +227,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
227227
Ok(ImmTy::from_scalar(discr_value, discr_layout))
228228
}
229229

230-
/// Computes the tag value and its field number (if any) of a given variant
231-
/// of type `ty`.
230+
/// Computes how to write the tag of a given variant of enum `ty`:
231+
/// - `None` means that nothing needs to be done as the variant is encoded implicitly
232+
/// - `Some((val, field_idx))` means that the given integer value needs to be stored at the
233+
/// given field index.
232234
pub(crate) fn tag_for_variant(
233235
&self,
234236
ty: Ty<'tcx>,

0 commit comments

Comments
 (0)