Skip to content

Commit 6aa4b5a

Browse files
spastorinooli-obk
authored andcommitted
Add promoted_operand closure to reuse code across different
1 parent 32fe477 commit 6aa4b5a

File tree

1 file changed

+20
-54
lines changed

1 file changed

+20
-54
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,23 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
908908
let promoted = &mut self.promoted;
909909
let promoted_id = Promoted::new(next_promoted_id);
910910
let tcx = self.tcx;
911+
let mut promoted_operand = |ty, span| {
912+
promoted.span = span;
913+
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
914+
915+
Operand::Constant(Box::new(Constant {
916+
span,
917+
user_ty: None,
918+
literal: tcx.mk_const(ty::Const {
919+
ty,
920+
val: ty::ConstKind::Unevaluated(
921+
def_id,
922+
InternalSubsts::identity_for_item(tcx, def_id),
923+
Some(promoted_id),
924+
),
925+
}),
926+
}))
927+
};
911928
let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
912929
match candidate {
913930
Candidate::Ref(loc) => {
@@ -926,10 +943,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
926943
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
927944
);
928945

929-
promoted.span = span;
930-
promoted.local_decls[RETURN_PLACE] =
931-
LocalDecl::new_return_place(ref_ty, span);
932-
933946
*region = tcx.lifetimes.re_static;
934947

935948
let mut projection = vec![PlaceElem::Deref];
@@ -944,24 +957,11 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
944957
let promoted_ref = local_decls.push(promoted_ref);
945958
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
946959

947-
let promoted_ref_rvalue =
948-
Rvalue::Use(Operand::Constant(Box::new(Constant {
949-
span,
950-
user_ty: None,
951-
literal: tcx.mk_const(ty::Const {
952-
ty: ref_ty,
953-
val: ty::ConstKind::Unevaluated(
954-
def_id,
955-
InternalSubsts::identity_for_item(tcx, def_id),
956-
Some(promoted_id),
957-
),
958-
}),
959-
})));
960960
let promoted_ref_statement = Statement {
961961
source_info: statement.source_info,
962962
kind: StatementKind::Assign(Box::new((
963963
Place::from(promoted_ref),
964-
promoted_ref_rvalue,
964+
Rvalue::Use(promoted_operand(ref_ty, span)),
965965
))),
966966
};
967967
self.extra_statements.push((loc, promoted_ref_statement));
@@ -988,24 +988,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
988988
let ty = operand.ty(local_decls, self.tcx);
989989
let span = statement.source_info.span;
990990

991-
promoted.span = span;
992-
promoted.local_decls[RETURN_PLACE] =
993-
LocalDecl::new_return_place(ty, span);
994-
995-
let promoted_operand = Operand::Constant(Box::new(Constant {
996-
span,
997-
user_ty: None,
998-
literal: tcx.mk_const(ty::Const {
999-
ty,
1000-
val: ty::ConstKind::Unevaluated(
1001-
def_id,
1002-
InternalSubsts::identity_for_item(tcx, def_id),
1003-
Some(promoted_id),
1004-
),
1005-
}),
1006-
}));
1007-
1008-
Rvalue::Use(mem::replace(operand, promoted_operand))
991+
Rvalue::Use(mem::replace(operand, promoted_operand(ty, span)))
1009992
}
1010993
_ => bug!(),
1011994
}
@@ -1017,24 +1000,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
10171000
let ty = args[index].ty(local_decls, self.tcx);
10181001
let span = terminator.source_info.span;
10191002

1020-
promoted.span = span;
1021-
promoted.local_decls[RETURN_PLACE] =
1022-
LocalDecl::new_return_place(ty, span);
1023-
1024-
let promoted_operand = Operand::Constant(Box::new(Constant {
1025-
span,
1026-
user_ty: None,
1027-
literal: tcx.mk_const(ty::Const {
1028-
ty,
1029-
val: ty::ConstKind::Unevaluated(
1030-
def_id,
1031-
InternalSubsts::identity_for_item(tcx, def_id),
1032-
Some(promoted_id),
1033-
),
1034-
}),
1035-
}));
1036-
1037-
Rvalue::Use(mem::replace(&mut args[index], promoted_operand))
1003+
Rvalue::Use(mem::replace(&mut args[index], promoted_operand(ty, span)))
10381004
}
10391005
// We expected a `TerminatorKind::Call` for which we'd like to promote an
10401006
// argument. `qualify_consts` saw a `TerminatorKind::Call` here, but

0 commit comments

Comments
 (0)