]> git.lizzy.rs Git - rust.git/commitdiff
track the innermost scope for every stmt
authorNiko Matsakis <niko@alum.mit.edu>
Wed, 9 Mar 2016 18:36:04 +0000 (13:36 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Wed, 23 Mar 2016 20:37:48 +0000 (16:37 -0400)
12 files changed:
src/librustc/mir/repr.rs
src/librustc_mir/build/block.rs
src/librustc_mir/build/cfg.rs
src/librustc_mir/build/expr/as_lvalue.rs
src/librustc_mir/build/expr/as_rvalue.rs
src/librustc_mir/build/expr/as_temp.rs
src/librustc_mir/build/expr/into.rs
src/librustc_mir/build/matches/mod.rs
src/librustc_mir/build/matches/test.rs
src/librustc_mir/build/matches/util.rs
src/librustc_mir/build/misc.rs
src/librustc_mir/build/scope.rs

index ebf78792d72e20c881385ccdf3ba9fe5981b88fa..7f28fe05beab087bcee669b8ed5f195201443cbf 100644 (file)
@@ -458,6 +458,7 @@ pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
 #[derive(Clone, RustcEncodable, RustcDecodable)]
 pub struct Statement<'tcx> {
     pub span: Span,
+    pub scope: ScopeId,
     pub kind: StatementKind<'tcx>,
 }
 
@@ -474,6 +475,7 @@ fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
         }
     }
 }
+
 ///////////////////////////////////////////////////////////////////////////
 // Lvalues
 
index b4c3e93b28005eab4b6333c727ea5e046d1ae20e..7ada408361d0582ac81a319cc4cfffb724ca2c7e 100644 (file)
@@ -71,7 +71,8 @@ pub fn ast_block(&mut self,
                 unpack!(block = this.into(destination, block, expr));
             } else {
                 // FIXME(#31472)
-                this.cfg.push_assign_unit(block, span, destination);
+                let scope_id = this.innermost_scope_id();
+                this.cfg.push_assign_unit(block, scope_id, span, destination);
             }
             // Finally, we pop all the let scopes before exiting out from the scope of block
             // itself.
index d804fc8635a92526765802e870fef0f579e7ad6a..e85c8ccce84731dc7abbc5d9d117800176b1a36e 100644 (file)
@@ -50,10 +50,12 @@ pub fn current_location(&mut self, block: BasicBlock) -> Location {
 
     pub fn push_assign(&mut self,
                        block: BasicBlock,
+                       scope: ScopeId,
                        span: Span,
                        lvalue: &Lvalue<'tcx>,
                        rvalue: Rvalue<'tcx>) {
         self.push(block, Statement {
+            scope: scope,
             span: span,
             kind: StatementKind::Assign(lvalue.clone(), rvalue)
         });
@@ -61,17 +63,20 @@ pub fn push_assign(&mut self,
 
     pub fn push_assign_constant(&mut self,
                                 block: BasicBlock,
+                                scope: ScopeId,
                                 span: Span,
                                 temp: &Lvalue<'tcx>,
                                 constant: Constant<'tcx>) {
-        self.push_assign(block, span, temp, Rvalue::Use(Operand::Constant(constant)));
+        self.push_assign(block, scope, span, temp,
+                         Rvalue::Use(Operand::Constant(constant)));
     }
 
     pub fn push_assign_unit(&mut self,
                             block: BasicBlock,
+                            scope: ScopeId,
                             span: Span,
                             lvalue: &Lvalue<'tcx>) {
-        self.push_assign(block, span, lvalue, Rvalue::Aggregate(
+        self.push_assign(block, scope, span, lvalue, Rvalue::Aggregate(
             AggregateKind::Tuple, vec![]
         ));
     }
index be2b70b86990eb87baa5f8998bc1eca78a495197..fba1ff6434bb9e45381cea1740d8e31b6c11696b 100644 (file)
@@ -34,6 +34,7 @@ fn expr_as_lvalue(&mut self,
         debug!("expr_as_lvalue(block={:?}, expr={:?})", block, expr);
 
         let this = self;
+        let scope_id = this.innermost_scope_id();
         let expr_span = expr.span;
         match expr.kind {
             ExprKind::Scope { extent, value } => {
@@ -58,9 +59,9 @@ fn expr_as_lvalue(&mut self,
 
                 // bounds check:
                 let (len, lt) = (this.temp(usize_ty.clone()), this.temp(bool_ty));
-                this.cfg.push_assign(block, expr_span, // len = len(slice)
+                this.cfg.push_assign(block, scope_id, expr_span, // len = len(slice)
                                      &len, Rvalue::Len(slice.clone()));
-                this.cfg.push_assign(block, expr_span, // lt = idx < len
+                this.cfg.push_assign(block, scope_id, expr_span, // lt = idx < len
                                      &lt, Rvalue::BinaryOp(BinOp::Lt,
                                                            idx.clone(),
                                                            Operand::Consume(len.clone())));
index a77a64ec3d8990354b51e97e363e05ff4f932c52..b340d933e64c3f35f764399a378be2f28f0da6a0 100644 (file)
@@ -33,6 +33,7 @@ fn expr_as_rvalue(&mut self,
         debug!("expr_as_rvalue(block={:?}, expr={:?})", block, expr);
 
         let this = self;
+        let scope_id = this.innermost_scope_id();
         let expr_span = expr.span;
 
         match expr.kind {
@@ -75,7 +76,7 @@ fn expr_as_rvalue(&mut self,
                 let value = this.hir.mirror(value);
                 let result = this.temp(expr.ty);
                 // to start, malloc some memory of suitable type (thus far, uninitialized):
-                this.cfg.push_assign(block, expr_span, &result, Rvalue::Box(value.ty));
+                this.cfg.push_assign(block, scope_id, expr_span, &result, Rvalue::Box(value.ty));
                 this.in_scope(value_extents, block, |this, _| {
                     // schedule a shallow free of that memory, lest we unwind:
                     this.schedule_box_free(expr_span, value_extents, &result, value.ty);
index 2041fef885d08ccec1b3f83b9f13e80297a98512..30a42bcd709d26db3906523d4b05bf36bc98497b 100644 (file)
@@ -55,7 +55,8 @@ fn expr_as_temp(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<
                 let expr_span = expr.span;
                 let lvalue = unpack!(block = this.as_lvalue(block, expr));
                 let rvalue = Rvalue::Use(Operand::Consume(lvalue));
-                this.cfg.push_assign(block, expr_span, &temp, rvalue);
+                let scope_id = this.innermost_scope_id();
+                this.cfg.push_assign(block, scope_id, expr_span, &temp, rvalue);
             }
             _ => {
                 unpack!(block = this.into(&temp, block, expr));
index ce2b7dc34ebf0ea57b5a89c62248de26f6c6f5dd..08b156c07bb419e4d91e73aca122f525be290b00 100644 (file)
@@ -36,6 +36,7 @@ pub fn into_expr(&mut self,
         // just use the name `this` uniformly
         let this = self;
         let expr_span = expr.span;
+        let scope_id = this.innermost_scope_id();
 
         match expr.kind {
             ExprKind::Scope { extent, value } => {
@@ -63,7 +64,8 @@ pub fn into_expr(&mut self,
                 } else {
                     // Body of the `if` expression without an `else` clause must return `()`, thus
                     // we implicitly generate a `else {}` if it is not specified.
-                    this.cfg.push_assign_unit(else_block, expr_span, destination);
+                    let scope_id = this.innermost_scope_id();
+                    this.cfg.push_assign_unit(else_block, scope_id, expr_span, destination);
                     else_block
                 };
 
@@ -104,7 +106,7 @@ pub fn into_expr(&mut self,
                 });
 
                 this.cfg.push_assign_constant(
-                    true_block, expr_span, destination,
+                    true_block, scope_id, expr_span, destination,
                     Constant {
                         span: expr_span,
                         ty: this.hir.bool_ty(),
@@ -112,7 +114,7 @@ pub fn into_expr(&mut self,
                     });
 
                 this.cfg.push_assign_constant(
-                    false_block, expr_span, destination,
+                    false_block, scope_id, expr_span, destination,
                     Constant {
                         span: expr_span,
                         ty: this.hir.bool_ty(),
@@ -178,7 +180,7 @@ pub fn into_expr(&mut self,
                 // If the loop may reach its exit_block, we assign an empty tuple to the
                 // destination to keep the MIR well-formed.
                 if might_break {
-                    this.cfg.push_assign_unit(exit_block, expr_span, destination);
+                    this.cfg.push_assign_unit(exit_block, scope_id, expr_span, destination);
                 }
                 exit_block.unit()
             }
@@ -189,7 +191,7 @@ pub fn into_expr(&mut self,
                 let rhs = unpack!(block = this.as_operand(block, rhs));
                 let lhs = unpack!(block = this.as_lvalue(block, lhs));
                 unpack!(block = this.build_drop(block, lhs.clone()));
-                this.cfg.push_assign(block, expr_span, &lhs, Rvalue::Use(rhs));
+                this.cfg.push_assign(block, scope_id, expr_span, &lhs, Rvalue::Use(rhs));
                 block.unit()
             }
             ExprKind::AssignOp { op, lhs, rhs } => {
@@ -208,7 +210,7 @@ pub fn into_expr(&mut self,
                 // we don't have to drop prior contents or anything
                 // because AssignOp is only legal for Copy types
                 // (overloaded ops should be desugared into a call).
-                this.cfg.push_assign(block, expr_span, &lhs,
+                this.cfg.push_assign(block, scope_id, expr_span, &lhs,
                                      Rvalue::BinaryOp(op,
                                                       Operand::Consume(lhs.clone()),
                                                       rhs));
@@ -229,7 +231,7 @@ pub fn into_expr(&mut self,
                 block = match value {
                     Some(value) => unpack!(this.into(&Lvalue::ReturnPointer, block, value)),
                     None => {
-                        this.cfg.push_assign_unit(block, expr_span, &Lvalue::ReturnPointer);
+                        this.cfg.push_assign_unit(block, scope_id, expr_span, &Lvalue::ReturnPointer);
                         block
                     }
                 };
@@ -293,7 +295,7 @@ pub fn into_expr(&mut self,
                 });
 
                 let rvalue = unpack!(block = this.as_rvalue(block, expr));
-                this.cfg.push_assign(block, expr_span, destination, rvalue);
+                this.cfg.push_assign(block, scope_id, expr_span, destination, rvalue);
                 block.unit()
             }
         }
index bc92da30caba8d7aca9af9ad1ffb9e365acd4eae..7f38324bca4aa6ceb0f41cd4906eefc357132340 100644 (file)
@@ -584,7 +584,9 @@ fn bind_matched_candidate(&mut self,
                     Rvalue::Ref(region, borrow_kind, binding.source),
             };
 
-            self.cfg.push_assign(block, binding.span, &Lvalue::Var(var_index), rvalue);
+            let scope_id = self.innermost_scope_id();
+            self.cfg.push_assign(block, scope_id, binding.span,
+                                 &Lvalue::Var(var_index), rvalue);
         }
     }
 
index 0efa24f3119432af582b4003f374a42d5fa14e9a..f5e7cfb2d28078e438818ca9bbd47c14b8dc0d10 100644 (file)
@@ -146,6 +146,7 @@ pub fn perform_test(&mut self,
                         lvalue: &Lvalue<'tcx>,
                         test: &Test<'tcx>)
                         -> Vec<BasicBlock> {
+        let scope_id = self.innermost_scope_id();
         match test.kind {
             TestKind::Switch { adt_def } => {
                 let num_enum_variants = self.hir.num_variants(adt_def);
@@ -189,7 +190,7 @@ pub fn perform_test(&mut self,
                         if let ty::TyArray(_, _) = mt.ty.sty {
                             ty = tcx.mk_imm_ref(region, tcx.mk_slice(tcx.types.u8));
                             let val_slice = self.temp(ty);
-                            self.cfg.push_assign(block, test.span, &val_slice,
+                            self.cfg.push_assign(block, scope_id, test.span, &val_slice,
                                                  Rvalue::Cast(CastKind::Unsize, val, ty));
                             val = Operand::Consume(val_slice);
                         }
@@ -204,7 +205,7 @@ pub fn perform_test(&mut self,
                     });
 
                     let slice = self.temp(ty);
-                    self.cfg.push_assign(block, test.span, &slice,
+                    self.cfg.push_assign(block, scope_id, test.span, &slice,
                                          Rvalue::Cast(CastKind::Unsize, array, ty));
                     Operand::Consume(slice)
                 } else {
@@ -268,13 +269,14 @@ pub fn perform_test(&mut self,
                 let (actual, result) = (self.temp(usize_ty), self.temp(bool_ty));
 
                 // actual = len(lvalue)
-                self.cfg.push_assign(block, test.span, &actual, Rvalue::Len(lvalue.clone()));
+                self.cfg.push_assign(block, scope_id, test.span, &actual, Rvalue::Len(lvalue.clone()));
 
                 // expected = <N>
-                let expected = self.push_usize(block, test.span, len);
+                let expected = self.push_usize(block, scope_id, test.span, len);
 
                 // result = actual == expected OR result = actual < expected
                 self.cfg.push_assign(block,
+                                     scope_id,
                                      test.span,
                                      &result,
                                      Rvalue::BinaryOp(op,
@@ -305,7 +307,9 @@ fn compare(&mut self,
         let result = self.temp(bool_ty);
 
         // result = op(left, right)
-        self.cfg.push_assign(block, span, &result, Rvalue::BinaryOp(op, left, right));
+        let scope_id = self.innermost_scope_id();
+        self.cfg.push_assign(block, scope_id, span, &result,
+                             Rvalue::BinaryOp(op, left, right));
 
         // branch based on result
         let target_block = self.cfg.start_new_block();
index b46c3ffb76a1b82dab822db1771059eb88f14089..101d7594309a9a505b2ca2b12187627a37d8468d 100644 (file)
@@ -61,7 +61,8 @@ pub fn prefix_suffix_slice<'pat>(&mut self,
                 from_end: suffix_len,
             };
             let temp = self.temp(slice.ty.clone()); // no need to schedule drop, temp is always copy
-            self.cfg.push_assign(block, slice.span, &temp, rvalue);
+            let scope_id = self.innermost_scope_id();
+            self.cfg.push_assign(block, scope_id, slice.span, &temp, rvalue);
             match_pairs.push(MatchPair::new(temp, slice));
         }
 
index 13ab26c358d6a6a9fd6d52637a22e42ff1c24e30..0d1690783301be37dc0efbfdb1aa59112743d4a1 100644 (file)
@@ -46,11 +46,16 @@ pub fn literal_operand(&mut self,
         Operand::Constant(constant)
     }
 
-    pub fn push_usize(&mut self, block: BasicBlock, span: Span, value: u64) -> Lvalue<'tcx> {
+    pub fn push_usize(&mut self,
+                      block: BasicBlock,
+                      scope_id: ScopeId,
+                      span: Span,
+                      value: u64)
+                      -> Lvalue<'tcx> {
         let usize_ty = self.hir.usize_ty();
         let temp = self.temp(usize_ty);
         self.cfg.push_assign_constant(
-            block, span, &temp,
+            block, scope_id, span, &temp,
             Constant {
                 span: span,
                 ty: self.hir.usize_ty(),
index 1dd9ec5bae5d1fc811ab9f0ad9b5473347f26b35..61b35b94e7000cce9d7b83c505d371d737b77321 100644 (file)
@@ -464,13 +464,14 @@ pub fn panic_bounds_check(&mut self,
         let (tuple, tuple_ref) = (self.temp(tup_ty), self.temp(ref_ty));
         let (file, line) = self.span_to_fileline_args(span);
         let elems = vec![Operand::Constant(file), Operand::Constant(line)];
+        let scope_id = self.innermost_scope_id();
         // FIXME: We should have this as a constant, rather than a stack variable (to not pollute
         // icache with cold branch code), however to achieve that we either have to rely on rvalue
         // promotion or have some way, in MIR, to create constants.
-        self.cfg.push_assign(block, span, &tuple, // tuple = (file_arg, line_arg);
+        self.cfg.push_assign(block, scope_id, span, &tuple, // tuple = (file_arg, line_arg);
                              Rvalue::Aggregate(AggregateKind::Tuple, elems));
         // FIXME: is this region really correct here?
-        self.cfg.push_assign(block, span, &tuple_ref, // tuple_ref = &tuple;
+        self.cfg.push_assign(block, scope_id, span, &tuple_ref, // tuple_ref = &tuple;
                              Rvalue::Ref(region, BorrowKind::Shared, tuple));
         let cleanup = self.diverge_cleanup();
         self.cfg.terminate(block, Terminator::Call {
@@ -505,13 +506,14 @@ pub fn panic(&mut self, block: BasicBlock, msg: &'static str, span: Span) {
         let elems = vec![Operand::Constant(message),
                          Operand::Constant(file),
                          Operand::Constant(line)];
+        let scope_id = self.innermost_scope_id();
         // FIXME: We should have this as a constant, rather than a stack variable (to not pollute
         // icache with cold branch code), however to achieve that we either have to rely on rvalue
         // promotion or have some way, in MIR, to create constants.
-        self.cfg.push_assign(block, span, &tuple, // tuple = (message_arg, file_arg, line_arg);
+        self.cfg.push_assign(block, scope_id, span, &tuple, // tuple = (message_arg, file_arg, line_arg);
                              Rvalue::Aggregate(AggregateKind::Tuple, elems));
         // FIXME: is this region really correct here?
-        self.cfg.push_assign(block, span, &tuple_ref, // tuple_ref = &tuple;
+        self.cfg.push_assign(block, scope_id, span, &tuple_ref, // tuple_ref = &tuple;
                              Rvalue::Ref(region, BorrowKind::Shared, tuple));
         let cleanup = self.diverge_cleanup();
         self.cfg.terminate(block, Terminator::Call {