Skip to content

Commit a42e3b6

Browse files
committed
Add promoted_operand closure to reuse code across different
1 parent cefe65b commit a42e3b6

File tree

1 file changed

+24
-58
lines changed

1 file changed

+24
-58
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,23 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
950950
let promoted = &mut self.promoted;
951951
let promoted_id = Promoted::new(next_promoted_id);
952952
let tcx = self.tcx;
953+
let mut promoted_operand = |ty, span| {
954+
promoted.span = span;
955+
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
956+
957+
Operand::Constant(Box::new(Constant {
958+
span,
959+
user_ty: None,
960+
literal: tcx.mk_const(ty::Const {
961+
ty,
962+
val: ty::ConstKind::Unevaluated(
963+
def_id,
964+
InternalSubsts::identity_for_item(tcx, def_id),
965+
Some(promoted_id),
966+
),
967+
}),
968+
}))
969+
};
953970
let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
954971
match candidate {
955972
Candidate::Ref(loc) => {
@@ -968,10 +985,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
968985
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
969986
);
970987

971-
promoted.span = span;
972-
promoted.local_decls[RETURN_PLACE] =
973-
LocalDecl::new_return_place(ref_ty, span);
974-
975988
*region = tcx.lifetimes.re_static;
976989

977990
let mut projection = vec![PlaceElem::Deref];
@@ -986,24 +999,11 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
986999
let promoted_ref = local_decls.push(promoted_ref);
9871000
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
9881001

989-
let promoted_ref_rvalue =
990-
Rvalue::Use(Operand::Constant(Box::new(Constant {
991-
span,
992-
user_ty: None,
993-
literal: tcx.mk_const(ty::Const {
994-
ty: ref_ty,
995-
val: ty::ConstKind::Unevaluated(
996-
def_id,
997-
InternalSubsts::identity_for_item(tcx, def_id),
998-
Some(promoted_id),
999-
),
1000-
}),
1001-
})));
10021002
let promoted_ref_statement = Statement {
10031003
source_info: statement.source_info,
10041004
kind: StatementKind::Assign(Box::new((
10051005
Place::from(promoted_ref),
1006-
promoted_ref_rvalue,
1006+
Rvalue::Use(promoted_operand(ref_ty, span)),
10071007
))),
10081008
};
10091009
self.extra_statements.push((loc, promoted_ref_statement));
@@ -1030,24 +1030,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
10301030
let ty = operand.ty(local_decls, self.tcx);
10311031
let span = statement.source_info.span;
10321032

1033-
promoted.span = span;
1034-
promoted.local_decls[RETURN_PLACE] =
1035-
LocalDecl::new_return_place(ty, span);
1036-
1037-
let promoted_operand = Operand::Constant(Box::new(Constant {
1038-
span,
1039-
user_ty: None,
1040-
literal: tcx.mk_const(ty::Const {
1041-
ty,
1042-
val: ty::ConstKind::Unevaluated(
1043-
def_id,
1044-
InternalSubsts::identity_for_item(tcx, def_id),
1045-
Some(promoted_id),
1046-
),
1047-
}),
1048-
}));
1049-
1050-
Rvalue::Use(mem::replace(operand, promoted_operand))
1033+
Rvalue::Use(mem::replace(operand, promoted_operand(ty, span)))
10511034
}
10521035
_ => bug!(),
10531036
}
@@ -1059,24 +1042,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
10591042
let ty = args[index].ty(local_decls, self.tcx);
10601043
let span = terminator.source_info.span;
10611044

1062-
promoted.span = span;
1063-
promoted.local_decls[RETURN_PLACE] =
1064-
LocalDecl::new_return_place(ty, span);
1065-
1066-
let promoted_operand = Operand::Constant(Box::new(Constant {
1067-
span,
1068-
user_ty: None,
1069-
literal: tcx.mk_const(ty::Const {
1070-
ty,
1071-
val: ty::ConstKind::Unevaluated(
1072-
def_id,
1073-
InternalSubsts::identity_for_item(tcx, def_id),
1074-
Some(promoted_id),
1075-
),
1076-
}),
1077-
}));
1078-
1079-
Rvalue::Use(mem::replace(&mut args[index], promoted_operand))
1045+
Rvalue::Use(mem::replace(&mut args[index], promoted_operand(ty, span)))
10801046
}
10811047
// We expected a `TerminatorKind::Call` for which we'd like to promote an
10821048
// argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
@@ -1093,10 +1059,10 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
10931059
};
10941060

10951061
assert_eq!(self.new_block(), START_BLOCK);
1096-
self.visit_rvalue(&mut rvalue, Location {
1097-
block: BasicBlock::new(0),
1098-
statement_index: usize::MAX
1099-
});
1062+
self.visit_rvalue(
1063+
&mut rvalue,
1064+
Location { block: BasicBlock::new(0), statement_index: usize::MAX },
1065+
);
11001066

11011067
let span = self.promoted.span;
11021068
self.assign(RETURN_PLACE, rvalue, span);

0 commit comments

Comments
 (0)