Skip to content

Commit

Permalink
remove special-case code for statics and just use borrowck_fn
Browse files Browse the repository at this point in the history
Fixes #38520
  • Loading branch information
nikomatsakis committed Feb 28, 2017
1 parent a9ec884 commit 085d71c
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 83 deletions.
20 changes: 6 additions & 14 deletions src/librustc/cfg/construct.rs
Expand Up @@ -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);

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/cfg/mod.rs
Expand Up @@ -59,7 +59,7 @@ pub type CFGEdge = graph::Edge<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)
}

Expand Down
53 changes: 0 additions & 53 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Expand Up @@ -28,9 +28,6 @@ use rustc::ty::{self, TyCtxt};
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;

Expand Down Expand Up @@ -514,53 +511,3 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
}
}

/// 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);
}
8 changes: 4 additions & 4 deletions src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -89,7 +89,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowckCtxt<'a, 'tcx> {
match item.node {
hir::ItemStatic(.., ex) |
hir::ItemConst(_, ex) => {
gather_loans::gather_loans_in_static_initializer(self.tcx, ex);
borrowck_fn(self.tcx, ex);
}
_ => { }
}
Expand All @@ -99,14 +99,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowckCtxt<'a, 'tcx> {

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);
}
Expand Down Expand Up @@ -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 } =
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/lib.rs
Expand Up @@ -24,6 +24,7 @@
#![deny(warnings)]

#![feature(box_syntax)]
#![feature(loop_break_value)]
#![feature(libc)]
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
Expand Down
23 changes: 17 additions & 6 deletions src/librustc_driver/pretty.rs
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Expand Up @@ -712,7 +712,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnconditionalRecursion {
// 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;
Expand Down
3 changes: 1 addition & 2 deletions src/test/compile-fail/E0017.rs
Expand Up @@ -19,8 +19,7 @@ static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
//~| 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
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/E0388.rs
Expand Up @@ -15,7 +15,7 @@ const CR: &'static mut i32 = &mut C; //~ ERROR E0017
//~| 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

Expand Down
@@ -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() {
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-18118.rs
Expand Up @@ -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
};
}

0 comments on commit 085d71c

Please sign in to comment.