Skip to content

Commit e5c56cd

Browse files
committed
Auto merge of rust-lang#14955 - HKalbasi:mir-fix, r=HKalbasi
Remove unnecessary `StorageDead` I hope this reduces MIR memory usage.
2 parents dd0c29c + f44fc27 commit e5c56cd

File tree

4 files changed

+238
-301
lines changed

4 files changed

+238
-301
lines changed

crates/hir-ty/src/infer/closure.rs

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ use chalk_ir::{
99
};
1010
use hir_def::{
1111
data::adt::VariantData,
12-
hir::{
13-
Array, BinaryOp, BindingAnnotation, BindingId, CaptureBy, Expr, ExprId, Pat, PatId,
14-
Statement, UnaryOp,
15-
},
12+
hir::{Array, BinaryOp, BindingId, CaptureBy, Expr, ExprId, Pat, PatId, Statement, UnaryOp},
1613
lang_item::LangItem,
1714
resolver::{resolver_for_expr, ResolveValueResult, ValueNs},
1815
DefWithBodyId, FieldId, HasModule, VariantId,
@@ -28,9 +25,9 @@ use crate::{
2825
mir::{BorrowKind, MirSpan, ProjectionElem},
2926
static_lifetime, to_chalk_trait_id,
3027
traits::FnTrait,
31-
utils::{self, generics, pattern_matching_dereference_count, Generics},
32-
Adjust, Adjustment, Binders, ChalkTraitId, ClosureId, ConstValue, DynTy, FnPointer, FnSig,
33-
Interner, Substitution, Ty, TyExt,
28+
utils::{self, generics, Generics},
29+
Adjust, Adjustment, Binders, BindingMode, ChalkTraitId, ClosureId, ConstValue, DynTy,
30+
FnPointer, FnSig, Interner, Substitution, Ty, TyExt,
3431
};
3532

3633
use super::{Expectation, InferenceContext};
@@ -488,13 +485,7 @@ impl InferenceContext<'_> {
488485
if let Some(initializer) = initializer {
489486
self.walk_expr(*initializer);
490487
if let Some(place) = self.place_of_expr(*initializer) {
491-
let ty = self.expr_ty(*initializer);
492-
self.consume_with_pat(
493-
place,
494-
ty,
495-
BindingAnnotation::Unannotated,
496-
*pat,
497-
);
488+
self.consume_with_pat(place, *pat);
498489
}
499490
}
500491
}
@@ -799,41 +790,37 @@ impl InferenceContext<'_> {
799790
}
800791
}
801792

802-
fn consume_with_pat(
803-
&mut self,
804-
mut place: HirPlace,
805-
mut ty: Ty,
806-
mut bm: BindingAnnotation,
807-
pat: PatId,
808-
) {
793+
fn consume_with_pat(&mut self, mut place: HirPlace, pat: PatId) {
794+
let cnt = self.result.pat_adjustments.get(&pat).map(|x| x.len()).unwrap_or_default();
795+
place.projections = place
796+
.projections
797+
.iter()
798+
.cloned()
799+
.chain((0..cnt).map(|_| ProjectionElem::Deref))
800+
.collect::<Vec<_>>()
801+
.into();
809802
match &self.body[pat] {
810803
Pat::Missing | Pat::Wild => (),
811804
Pat::Tuple { args, ellipsis } => {
812-
pattern_matching_dereference(&mut ty, &mut bm, &mut place);
813805
let (al, ar) = args.split_at(ellipsis.unwrap_or(args.len()));
814-
let subst = match ty.kind(Interner) {
815-
TyKind::Tuple(_, s) => s,
806+
let field_count = match self.result[pat].kind(Interner) {
807+
TyKind::Tuple(_, s) => s.len(Interner),
816808
_ => return,
817809
};
818-
let fields = subst.iter(Interner).map(|x| x.assert_ty_ref(Interner)).enumerate();
810+
let fields = 0..field_count;
819811
let it = al.iter().zip(fields.clone()).chain(ar.iter().rev().zip(fields.rev()));
820-
for (arg, (i, ty)) in it {
812+
for (arg, i) in it {
821813
let mut p = place.clone();
822814
p.projections.push(ProjectionElem::TupleOrClosureField(i));
823-
self.consume_with_pat(p, ty.clone(), bm, *arg);
815+
self.consume_with_pat(p, *arg);
824816
}
825817
}
826818
Pat::Or(pats) => {
827819
for pat in pats.iter() {
828-
self.consume_with_pat(place.clone(), ty.clone(), bm, *pat);
820+
self.consume_with_pat(place.clone(), *pat);
829821
}
830822
}
831823
Pat::Record { args, .. } => {
832-
pattern_matching_dereference(&mut ty, &mut bm, &mut place);
833-
let subst = match ty.kind(Interner) {
834-
TyKind::Adt(_, s) => s,
835-
_ => return,
836-
};
837824
let Some(variant) = self.result.variant_resolution_for_pat(pat) else {
838825
return;
839826
};
@@ -843,7 +830,6 @@ impl InferenceContext<'_> {
843830
}
844831
VariantId::StructId(s) => {
845832
let vd = &*self.db.struct_data(s).variant_data;
846-
let field_types = self.db.field_types(variant);
847833
for field_pat in args.iter() {
848834
let arg = field_pat.pat;
849835
let Some(local_id) = vd.field(&field_pat.name) else {
@@ -854,12 +840,7 @@ impl InferenceContext<'_> {
854840
parent: variant.into(),
855841
local_id,
856842
}));
857-
self.consume_with_pat(
858-
p,
859-
field_types[local_id].clone().substitute(Interner, subst),
860-
bm,
861-
arg,
862-
);
843+
self.consume_with_pat(p, arg);
863844
}
864845
}
865846
}
@@ -870,26 +851,20 @@ impl InferenceContext<'_> {
870851
| Pat::Path(_)
871852
| Pat::Lit(_) => self.consume_place(place, pat.into()),
872853
Pat::Bind { id, subpat: _ } => {
873-
let mode = self.body.bindings[*id].mode;
874-
if matches!(mode, BindingAnnotation::Ref | BindingAnnotation::RefMut) {
875-
bm = mode;
876-
}
877-
let capture_kind = match bm {
878-
BindingAnnotation::Unannotated | BindingAnnotation::Mutable => {
854+
let mode = self.result.binding_modes[*id];
855+
let capture_kind = match mode {
856+
BindingMode::Move => {
879857
self.consume_place(place, pat.into());
880858
return;
881859
}
882-
BindingAnnotation::Ref => BorrowKind::Shared,
883-
BindingAnnotation::RefMut => BorrowKind::Mut { allow_two_phase_borrow: false },
860+
BindingMode::Ref(Mutability::Not) => BorrowKind::Shared,
861+
BindingMode::Ref(Mutability::Mut) => {
862+
BorrowKind::Mut { allow_two_phase_borrow: false }
863+
}
884864
};
885865
self.add_capture(place, CaptureKind::ByRef(capture_kind), pat.into());
886866
}
887867
Pat::TupleStruct { path: _, args, ellipsis } => {
888-
pattern_matching_dereference(&mut ty, &mut bm, &mut place);
889-
let subst = match ty.kind(Interner) {
890-
TyKind::Adt(_, s) => s,
891-
_ => return,
892-
};
893868
let Some(variant) = self.result.variant_resolution_for_pat(pat) else {
894869
return;
895870
};
@@ -903,29 +878,20 @@ impl InferenceContext<'_> {
903878
let fields = vd.fields().iter();
904879
let it =
905880
al.iter().zip(fields.clone()).chain(ar.iter().rev().zip(fields.rev()));
906-
let field_types = self.db.field_types(variant);
907881
for (arg, (i, _)) in it {
908882
let mut p = place.clone();
909883
p.projections.push(ProjectionElem::Field(FieldId {
910884
parent: variant.into(),
911885
local_id: i,
912886
}));
913-
self.consume_with_pat(
914-
p,
915-
field_types[i].clone().substitute(Interner, subst),
916-
bm,
917-
*arg,
918-
);
887+
self.consume_with_pat(p, *arg);
919888
}
920889
}
921890
}
922891
}
923892
Pat::Ref { pat, mutability: _ } => {
924-
if let Some((inner, _, _)) = ty.as_reference() {
925-
ty = inner.clone();
926-
place.projections.push(ProjectionElem::Deref);
927-
self.consume_with_pat(place, ty, bm, *pat)
928-
}
893+
place.projections.push(ProjectionElem::Deref);
894+
self.consume_with_pat(place, *pat)
929895
}
930896
Pat::Box { .. } => (), // not supported
931897
}
@@ -1054,12 +1020,3 @@ fn apply_adjusts_to_place(mut r: HirPlace, adjustments: &[Adjustment]) -> Option
10541020
}
10551021
Some(r)
10561022
}
1057-
1058-
fn pattern_matching_dereference(
1059-
cond_ty: &mut Ty,
1060-
binding_mode: &mut BindingAnnotation,
1061-
cond_place: &mut HirPlace,
1062-
) {
1063-
let cnt = pattern_matching_dereference_count(cond_ty, binding_mode);
1064-
cond_place.projections.extend((0..cnt).map(|_| ProjectionElem::Deref));
1065-
}

crates/hir-ty/src/mir/lower.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
478478
current,
479479
None,
480480
cond_place,
481-
self.expr_ty_after_adjustments(*expr),
482481
*pat,
483-
BindingAnnotation::Unannotated,
484482
)?;
485483
self.write_bytes_to_place(
486484
then_target,
@@ -598,16 +596,13 @@ impl<'ctx> MirLowerCtx<'ctx> {
598596
else {
599597
return Ok(None);
600598
};
601-
let cond_ty = self.expr_ty_after_adjustments(*expr);
602599
let mut end = None;
603600
for MatchArm { pat, guard, expr } in arms.iter() {
604601
let (then, mut otherwise) = self.pattern_match(
605602
current,
606603
None,
607604
cond_place.clone(),
608-
cond_ty.clone(),
609605
*pat,
610-
BindingAnnotation::Unannotated,
611606
)?;
612607
let then = if let &Some(guard) = guard {
613608
let next = self.new_basic_block();
@@ -1477,9 +1472,6 @@ impl<'ctx> MirLowerCtx<'ctx> {
14771472
span: MirSpan,
14781473
) -> Result<()> {
14791474
self.drop_scopes.last_mut().unwrap().locals.push(l);
1480-
// FIXME: this storage dead is not neccessary, but since drop scope handling is broken, we need
1481-
// it to avoid falso positives in mutability errors
1482-
self.push_statement(current, StatementKind::StorageDead(l).with_span(span));
14831475
self.push_statement(current, StatementKind::StorageLive(l).with_span(span));
14841476
Ok(())
14851477
}
@@ -1508,14 +1500,8 @@ impl<'ctx> MirLowerCtx<'ctx> {
15081500
return Ok(None);
15091501
};
15101502
current = c;
1511-
(current, else_block) = self.pattern_match(
1512-
current,
1513-
None,
1514-
init_place,
1515-
self.expr_ty_after_adjustments(*expr_id),
1516-
*pat,
1517-
BindingAnnotation::Unannotated,
1518-
)?;
1503+
(current, else_block) =
1504+
self.pattern_match(current, None, init_place, *pat)?;
15191505
match (else_block, else_branch) {
15201506
(None, _) => (),
15211507
(Some(else_block), None) => {
@@ -1595,14 +1581,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
15951581
continue;
15961582
}
15971583
}
1598-
let r = self.pattern_match(
1599-
current,
1600-
None,
1601-
local.into(),
1602-
self.result.locals[local].ty.clone(),
1603-
param,
1604-
BindingAnnotation::Unannotated,
1605-
)?;
1584+
let r = self.pattern_match(current, None, local.into(), param)?;
16061585
if let Some(b) = r.1 {
16071586
self.set_terminator(b, TerminatorKind::Unreachable, param.into());
16081587
}

0 commit comments

Comments
 (0)