Add promoted_operand closure to reuse code across different
This commit is contained in:
parent
32fe47779b
commit
6aa4b5a760
1 changed files with 20 additions and 54 deletions
|
|
@ -908,6 +908,23 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
let promoted = &mut self.promoted;
|
||||
let promoted_id = Promoted::new(next_promoted_id);
|
||||
let tcx = self.tcx;
|
||||
let mut promoted_operand = |ty, span| {
|
||||
promoted.span = span;
|
||||
promoted.local_decls[RETURN_PLACE] = LocalDecl::new_return_place(ty, span);
|
||||
|
||||
Operand::Constant(Box::new(Constant {
|
||||
span,
|
||||
user_ty: None,
|
||||
literal: tcx.mk_const(ty::Const {
|
||||
ty,
|
||||
val: ty::ConstKind::Unevaluated(
|
||||
def_id,
|
||||
InternalSubsts::identity_for_item(tcx, def_id),
|
||||
Some(promoted_id),
|
||||
),
|
||||
}),
|
||||
}))
|
||||
};
|
||||
let (blocks, local_decls) = self.source.basic_blocks_and_local_decls_mut();
|
||||
match candidate {
|
||||
Candidate::Ref(loc) => {
|
||||
|
|
@ -926,10 +943,6 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
ty::TypeAndMut { ty, mutbl: borrow_kind.to_mutbl_lossy() },
|
||||
);
|
||||
|
||||
promoted.span = span;
|
||||
promoted.local_decls[RETURN_PLACE] =
|
||||
LocalDecl::new_return_place(ref_ty, span);
|
||||
|
||||
*region = tcx.lifetimes.re_static;
|
||||
|
||||
let mut projection = vec![PlaceElem::Deref];
|
||||
|
|
@ -944,24 +957,11 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
let promoted_ref = local_decls.push(promoted_ref);
|
||||
assert_eq!(self.temps.push(TempState::Unpromotable), promoted_ref);
|
||||
|
||||
let promoted_ref_rvalue =
|
||||
Rvalue::Use(Operand::Constant(Box::new(Constant {
|
||||
span,
|
||||
user_ty: None,
|
||||
literal: tcx.mk_const(ty::Const {
|
||||
ty: ref_ty,
|
||||
val: ty::ConstKind::Unevaluated(
|
||||
def_id,
|
||||
InternalSubsts::identity_for_item(tcx, def_id),
|
||||
Some(promoted_id),
|
||||
),
|
||||
}),
|
||||
})));
|
||||
let promoted_ref_statement = Statement {
|
||||
source_info: statement.source_info,
|
||||
kind: StatementKind::Assign(Box::new((
|
||||
Place::from(promoted_ref),
|
||||
promoted_ref_rvalue,
|
||||
Rvalue::Use(promoted_operand(ref_ty, span)),
|
||||
))),
|
||||
};
|
||||
self.extra_statements.push((loc, promoted_ref_statement));
|
||||
|
|
@ -988,24 +988,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
let ty = operand.ty(local_decls, self.tcx);
|
||||
let span = statement.source_info.span;
|
||||
|
||||
promoted.span = span;
|
||||
promoted.local_decls[RETURN_PLACE] =
|
||||
LocalDecl::new_return_place(ty, span);
|
||||
|
||||
let promoted_operand = Operand::Constant(Box::new(Constant {
|
||||
span,
|
||||
user_ty: None,
|
||||
literal: tcx.mk_const(ty::Const {
|
||||
ty,
|
||||
val: ty::ConstKind::Unevaluated(
|
||||
def_id,
|
||||
InternalSubsts::identity_for_item(tcx, def_id),
|
||||
Some(promoted_id),
|
||||
),
|
||||
}),
|
||||
}));
|
||||
|
||||
Rvalue::Use(mem::replace(operand, promoted_operand))
|
||||
Rvalue::Use(mem::replace(operand, promoted_operand(ty, span)))
|
||||
}
|
||||
_ => bug!(),
|
||||
}
|
||||
|
|
@ -1017,24 +1000,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|||
let ty = args[index].ty(local_decls, self.tcx);
|
||||
let span = terminator.source_info.span;
|
||||
|
||||
promoted.span = span;
|
||||
promoted.local_decls[RETURN_PLACE] =
|
||||
LocalDecl::new_return_place(ty, span);
|
||||
|
||||
let promoted_operand = Operand::Constant(Box::new(Constant {
|
||||
span,
|
||||
user_ty: None,
|
||||
literal: tcx.mk_const(ty::Const {
|
||||
ty,
|
||||
val: ty::ConstKind::Unevaluated(
|
||||
def_id,
|
||||
InternalSubsts::identity_for_item(tcx, def_id),
|
||||
Some(promoted_id),
|
||||
),
|
||||
}),
|
||||
}));
|
||||
|
||||
Rvalue::Use(mem::replace(&mut args[index], promoted_operand))
|
||||
Rvalue::Use(mem::replace(&mut args[index], promoted_operand(ty, span)))
|
||||
}
|
||||
// We expected a `TerminatorKind::Call` for which we'd like to promote an
|
||||
// argument. `qualify_consts` saw a `TerminatorKind::Call` here, but
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue