Skip to content

Commit 754d171

Browse files
committed
Auto merge of #85195 - Mark-Simulacrum:variant-by-idx, r=petrochenkov
Store VariantIdx to distinguish enum variants This saves ~24% of the instructions on the match-stress-enum benchmark, but I'm not 100% sure that this is OK - if we ever compare two constructors across enums (e.g., a Result and an Option), then this is obviously insufficient; I can experiment with continuing to store the DefId for comparison purposes in that case.
2 parents 17f30e5 + e400595 commit 754d171

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ use super::{FieldPat, Pat, PatKind, PatRange};
5252
use rustc_data_structures::captures::Captures;
5353
use rustc_index::vec::Idx;
5454

55-
use rustc_hir::def_id::DefId;
5655
use rustc_hir::{HirId, RangeEnd};
5756
use rustc_middle::mir::interpret::ConstValue;
5857
use rustc_middle::mir::Field;
@@ -590,7 +589,7 @@ pub(super) enum Constructor<'tcx> {
590589
/// and fixed-length arrays.
591590
Single,
592591
/// Enum variants.
593-
Variant(DefId),
592+
Variant(VariantIdx),
594593
/// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
595594
IntRange(IntRange),
596595
/// Ranges of floating-point literal values (`2.0..=5.2`).
@@ -634,7 +633,7 @@ impl<'tcx> Constructor<'tcx> {
634633

635634
fn variant_index_for_adt(&self, adt: &'tcx ty::AdtDef) -> VariantIdx {
636635
match *self {
637-
Variant(id) => adt.variant_index_with_id(id),
636+
Variant(idx) => idx,
638637
Single => {
639638
assert!(!adt.is_enum());
640639
VariantIdx::new(0)
@@ -649,9 +648,7 @@ impl<'tcx> Constructor<'tcx> {
649648
PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern`
650649
PatKind::Binding { .. } | PatKind::Wild => Wildcard,
651650
PatKind::Leaf { .. } | PatKind::Deref { .. } => Single,
652-
&PatKind::Variant { adt_def, variant_index, .. } => {
653-
Variant(adt_def.variants[variant_index].def_id)
654-
}
651+
&PatKind::Variant { variant_index, .. } => Variant(variant_index),
655652
PatKind::Constant { value } => {
656653
if let Some(int_range) = IntRange::from_const(cx.tcx, cx.param_env, value) {
657654
IntRange(int_range)
@@ -928,15 +925,15 @@ impl<'tcx> SplitWildcard<'tcx> {
928925
// If `exhaustive_patterns` is enabled, we exclude variants known to be
929926
// uninhabited.
930927
def.variants
931-
.iter()
932-
.filter(|v| {
928+
.iter_enumerated()
929+
.filter(|(_, v)| {
933930
!v.uninhabited_from(cx.tcx, substs, def.adt_kind(), cx.param_env)
934931
.contains(cx.tcx, cx.module)
935932
})
936-
.map(|v| Variant(v.def_id))
933+
.map(|(idx, _)| Variant(idx))
937934
.collect()
938935
} else {
939-
def.variants.iter().map(|v| Variant(v.def_id)).collect()
936+
def.variants.indices().map(|idx| Variant(idx)).collect()
940937
}
941938
}
942939
ty::Char => {

0 commit comments

Comments
 (0)