about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSantiago Pastorino <spastorino@gmail.com>2019-12-11 00:11:12 -0300
committerOliver Scherer <github35764891676564198441@oli-obk.de>2020-01-10 09:08:24 +0100
commit6aa4b5a7603efc7a6f323e1be9be67ecf8f5f227 (patch)
tree8c7767c3c67adce374427b72068905e79e48dadd
parent32fe47779b169cd8d637cfae3b130e004008e6f3 (diff)
downloadrust-6aa4b5a7603efc7a6f323e1be9be67ecf8f5f227.tar.gz
rust-6aa4b5a7603efc7a6f323e1be9be67ecf8f5f227.zip
Add promoted_operand closure to reuse code across different
-rw-r--r--src/librustc_mir/transform/promote_consts.rs74
1 files changed, 20 insertions, 54 deletions
diff --git a/src/librustc_mir/transform/promote_consts.rs b/src/librustc_mir/transform/promote_consts.rs
index a607383f777..1052d037326 100644
--- a/src/librustc_mir/transform/promote_consts.rs
+++ b/src/librustc_mir/transform/promote_consts.rs
@@ -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