]> git.lizzy.rs Git - rust.git/commitdiff
remove special-case code for statics and just use `borrowck_fn`
authorNiko Matsakis <niko@alum.mit.edu>
Sat, 18 Feb 2017 11:52:16 +0000 (06:52 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 28 Feb 2017 13:43:47 +0000 (08:43 -0500)
Fixes #38520

src/librustc/cfg/construct.rs
src/librustc/cfg/mod.rs
src/librustc_borrowck/borrowck/gather_loans/mod.rs
src/librustc_borrowck/borrowck/mod.rs
src/librustc_driver/lib.rs
src/librustc_driver/pretty.rs
src/librustc_lint/builtin.rs
src/test/compile-fail/E0017.rs
src/test/compile-fail/E0388.rs
src/test/compile-fail/borrowck/move-in-static-initializer-issue-38520.rs [new file with mode: 0644]
src/test/compile-fail/issue-18118.rs

index 122543aee40eca45178cf1b84ab1cf77d0bb269c..4567795184e4dccf168c6a8ddf6f0f8c63ea1f20 100644 (file)
@@ -32,7 +32,7 @@ struct LoopScope {
 }
 
 pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
-                           body: &hir::Expr) -> CFG {
+                           body: &hir::Body) -> CFG {
     let mut graph = graph::Graph::new();
     let entry = graph.add_node(CFGNodeData::Entry);
 
@@ -43,26 +43,18 @@ pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
     let fn_exit = graph.add_node(CFGNodeData::Exit);
     let body_exit;
 
-    // Find the function this expression is from.
-    let mut node_id = body.id;
-    loop {
-        let node = tcx.hir.get(node_id);
-        if hir::map::blocks::FnLikeNode::from_node(node).is_some() {
-            break;
-        }
-        let parent = tcx.hir.get_parent_node(node_id);
-        assert!(node_id != parent);
-        node_id = parent;
-    }
+    // Find the tables for this body.
+    let owner_def_id = tcx.hir.local_def_id(tcx.hir.body_owner(body.id()));
+    let tables = tcx.item_tables(owner_def_id);
 
     let mut cfg_builder = CFGBuilder {
         tcx: tcx,
-        tables: tcx.item_tables(tcx.hir.local_def_id(node_id)),
+        tables: tables,
         graph: graph,
         fn_exit: fn_exit,
         loop_scopes: Vec::new()
     };
-    body_exit = cfg_builder.expr(body, entry);
+    body_exit = cfg_builder.expr(&body.value, entry);
     cfg_builder.add_contained_edge(body_exit, fn_exit);
     let CFGBuilder {graph, ..} = cfg_builder;
     CFG {graph: graph,
index 43434b884c8d4b7e55b96c41c678565362ad7ef4..1473dbb1676f32411eb6e2d6b4393f5d33e4a178 100644 (file)
@@ -59,7 +59,7 @@ pub struct CFGEdgeData {
 
 impl CFG {
     pub fn new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
-                         body: &hir::Expr) -> CFG {
+                         body: &hir::Body) -> CFG {
         construct::construct(tcx, body)
     }
 
index b31b54a25415a2261fc547db509c435bad4bbdca..28b6c7a13f1713cb2c4bea107725d2e80cc4081a 100644 (file)
@@ -28,9 +28,6 @@
 use syntax::ast;
 use syntax_pos::Span;
 use rustc::hir;
-use rustc::hir::Expr;
-use rustc::hir::intravisit;
-use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
 
 use self::restrictions::RestrictionResult;
 
@@ -514,53 +511,3 @@ pub fn report_potential_errors(&self) {
     }
 }
 
-/// Context used while gathering loans on static initializers
-///
-/// This visitor walks static initializer's expressions and makes
-/// sure the loans being taken are sound.
-struct StaticInitializerCtxt<'a, 'tcx: 'a> {
-    bccx: &'a BorrowckCtxt<'a, 'tcx>,
-    body_id: hir::BodyId,
-}
-
-impl<'a, 'tcx> Visitor<'tcx> for StaticInitializerCtxt<'a, 'tcx> {
-    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
-        NestedVisitorMap::None
-    }
-
-    fn visit_expr(&mut self, ex: &'tcx Expr) {
-        if let hir::ExprAddrOf(mutbl, ref base) = ex.node {
-            let infcx = self.bccx.tcx.borrowck_fake_infer_ctxt(self.body_id);
-            let mc = mc::MemCategorizationContext::new(&infcx);
-            let base_cmt = mc.cat_expr(&base).unwrap();
-            let borrow_kind = ty::BorrowKind::from_mutbl(mutbl);
-            // Check that we don't allow borrows of unsafe static items.
-            let err = check_aliasability(self.bccx, ex.span,
-                                         BorrowViolation(euv::AddrOf),
-                                         base_cmt, borrow_kind).is_err();
-            if err {
-                return; // reported an error, no sense in reporting more.
-            }
-        }
-
-        intravisit::walk_expr(self, ex);
-    }
-}
-
-pub fn gather_loans_in_static_initializer<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
-                                                    body: hir::BodyId) {
-    debug!("gather_loans_in_static_initializer(expr={:?})", body);
-
-    let bccx = &BorrowckCtxt {
-        tcx: tcx,
-        tables: None
-    };
-
-    let mut sicx = StaticInitializerCtxt {
-        bccx: bccx,
-        body_id: body
-    };
-
-    let body = sicx.bccx.tcx.hir.body(body);
-    sicx.visit_body(body);
-}
index 9c29cd375ff5152bfcade9bb671809e4530a6330..613f28138a51924505d69054e7770afcd2ca94f1 100644 (file)
@@ -89,7 +89,7 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
         match item.node {
             hir::ItemStatic(.., ex) |
             hir::ItemConst(_, ex) => {
-                gather_loans::gather_loans_in_static_initializer(self.tcx, ex);
+                borrowck_fn(self.tcx, ex);
             }
             _ => { }
         }
@@ -99,14 +99,14 @@ fn visit_item(&mut self, item: &'tcx hir::Item) {
 
     fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
         if let hir::TraitItemKind::Const(_, Some(expr)) = ti.node {
-            gather_loans::gather_loans_in_static_initializer(self.tcx, expr);
+            borrowck_fn(self.tcx, expr);
         }
         intravisit::walk_trait_item(self, ti);
     }
 
     fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
         if let hir::ImplItemKind::Const(_, expr) = ii.node {
-            gather_loans::gather_loans_in_static_initializer(self.tcx, expr);
+            borrowck_fn(self.tcx, expr);
         }
         intravisit::walk_impl_item(self, ii);
     }
@@ -147,7 +147,7 @@ fn borrowck_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, body_id: hir::BodyId) {
         mir::borrowck_mir(bccx, owner_id, &attributes);
     }
 
-    let cfg = cfg::CFG::new(bccx.tcx, &body.value);
+    let cfg = cfg::CFG::new(bccx.tcx, &body);
     let AnalysisData { all_loans,
                        loans: loan_dfcx,
                        move_data: flowed_moves } =
index 7fd4fa44ca45dff0ad42b8577283e3df38663cf3..9810f121ef2c125570c4df911f625e6b7ae2510a 100644 (file)
@@ -24,6 +24,7 @@
 #![deny(warnings)]
 
 #![feature(box_syntax)]
+#![feature(loop_break_value)]
 #![feature(libc)]
 #![feature(quote)]
 #![feature(rustc_diagnostic_macros)]
index 064c4982ef00eb87adcbd828b18ac8701898e3e0..b6978478085d5b224607c90a0acc898c17ad712b 100644 (file)
@@ -718,13 +718,24 @@ fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
                                        mode: PpFlowGraphMode,
                                        mut out: W)
                                        -> io::Result<()> {
-    let cfg = match code {
-        blocks::Code::Expr(expr) => cfg::CFG::new(tcx, expr),
-        blocks::Code::FnLike(fn_like) => {
-            let body = tcx.hir.body(fn_like.body());
-            cfg::CFG::new(tcx, &body.value)
-        },
+    let body_id = match code {
+        blocks::Code::Expr(expr) => {
+            // Find the function this expression is from.
+            let mut node_id = expr.id;
+            loop {
+                let node = tcx.hir.get(node_id);
+                if let Some(n) = hir::map::blocks::FnLikeNode::from_node(node) {
+                    break n.body();
+                }
+                let parent = tcx.hir.get_parent_node(node_id);
+                assert!(node_id != parent);
+                node_id = parent;
+            }
+        }
+        blocks::Code::FnLike(fn_like) => fn_like.body(),
     };
+    let body = tcx.hir.body(body_id);
+    let cfg = cfg::CFG::new(tcx, &body);
     let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
     let lcfg = LabelledCFG {
         hir_map: &tcx.hir,
index b3f09c28277ada98725c0c6729d148ee23499fdf..58336f939d1228e4f550d603ddc93d8f29c5dc7c 100644 (file)
@@ -712,7 +712,7 @@ fn check_fn(&mut self,
         // to have behaviour like the above, rather than
         // e.g. accidentally recurring after an assert.
 
-        let cfg = cfg::CFG::new(cx.tcx, &body.value);
+        let cfg = cfg::CFG::new(cx.tcx, &body);
 
         let mut work_queue = vec![cfg.entry];
         let mut reached_exit_without_self_call = false;
index 44d2f158d20bbeadad7b5c28d289963c044e7258..c6bec6090f242a1024ecb22d46eca907e20ad33a 100644 (file)
@@ -19,8 +19,7 @@
                                               //~| NOTE statics require immutable values
                                               //~| ERROR E0017
                                               //~| NOTE statics require immutable values
-                                              //~| ERROR E0388
-                                              //~| NOTE cannot write data in a static definition
+                                              //~| ERROR cannot borrow
 static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
                                              //~| NOTE statics require immutable values
                                              //~| ERROR E0017
index 13f2c23d8c4a9912624fc6a67505262d3a963769..2c88039d373e59ef77ecc11fc195fc883dec494f 100644 (file)
@@ -15,7 +15,7 @@
                                      //~| ERROR E0017
 static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
                                               //~| ERROR E0017
-                                              //~| ERROR E0388
+                                              //~| ERROR cannot borrow
 static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
                                              //~| ERROR E0017
 
diff --git a/src/test/compile-fail/borrowck/move-in-static-initializer-issue-38520.rs b/src/test/compile-fail/borrowck/move-in-static-initializer-issue-38520.rs
new file mode 100644 (file)
index 0000000..3c1980e
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Regression test for #38520. Check that moves of `Foo` are not
+// permitted as `Foo` is not copy (even in a static/const
+// initializer).
+
+#![feature(const_fn)]
+
+struct Foo(usize);
+
+const fn get(x: Foo) -> usize {
+    x.0
+}
+
+const X: Foo = Foo(22);
+static Y: usize = get(*&X); //~ ERROR E0507
+const Z: usize = get(*&X); //~ ERROR E0507
+
+fn main() {
+}
index 3afb34f037bcc6301642652eff6605a97cb047e7..35e57dffb6c455c776dab8084a221f197cb22ba1 100644 (file)
@@ -13,6 +13,6 @@ pub fn main() {
         //~^ ERROR blocks in constants are limited to items and tail expressions
         let p = 3;
         //~^ ERROR blocks in constants are limited to items and tail expressions
-        &p
+        &p //~ ERROR `p` does not live long enough
     };
 }