Skip to content

Commit 9334c39

Browse files
committed
remove special-case code for statics and just use borrowck_fn
Fixes #38520
1 parent b347a23 commit 9334c39

File tree

11 files changed

+61
-83
lines changed

11 files changed

+61
-83
lines changed

src/librustc/cfg/construct.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct LoopScope {
3232
}
3333

3434
pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
35-
body: &hir::Expr) -> CFG {
35+
body: &hir::Body) -> CFG {
3636
let mut graph = graph::Graph::new();
3737
let entry = graph.add_node(CFGNodeData::Entry);
3838

@@ -43,26 +43,18 @@ pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
4343
let fn_exit = graph.add_node(CFGNodeData::Exit);
4444
let body_exit;
4545

46-
// Find the function this expression is from.
47-
let mut node_id = body.id;
48-
loop {
49-
let node = tcx.hir.get(node_id);
50-
if hir::map::blocks::FnLikeNode::from_node(node).is_some() {
51-
break;
52-
}
53-
let parent = tcx.hir.get_parent_node(node_id);
54-
assert!(node_id != parent);
55-
node_id = parent;
56-
}
46+
// Find the tables for this body.
47+
let owner_def_id = tcx.hir.local_def_id(tcx.hir.body_owner(body.id()));
48+
let tables = tcx.item_tables(owner_def_id);
5749

5850
let mut cfg_builder = CFGBuilder {
5951
tcx: tcx,
60-
tables: tcx.item_tables(tcx.hir.local_def_id(node_id)),
52+
tables: tables,
6153
graph: graph,
6254
fn_exit: fn_exit,
6355
loop_scopes: Vec::new()
6456
};
65-
body_exit = cfg_builder.expr(body, entry);
57+
body_exit = cfg_builder.expr(&body.value, entry);
6658
cfg_builder.add_contained_edge(body_exit, fn_exit);
6759
let CFGBuilder {graph, ..} = cfg_builder;
6860
CFG {graph: graph,

src/librustc/cfg/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub type CFGEdge = graph::Edge<CFGEdgeData>;
5959

6060
impl CFG {
6161
pub fn new<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
62-
body: &hir::Expr) -> CFG {
62+
body: &hir::Body) -> CFG {
6363
construct::construct(tcx, body)
6464
}
6565

src/librustc_borrowck/borrowck/gather_loans/mod.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ use rustc::ty::{self, TyCtxt};
2828
use syntax::ast;
2929
use syntax_pos::Span;
3030
use rustc::hir;
31-
use rustc::hir::Expr;
32-
use rustc::hir::intravisit;
33-
use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
3431

3532
use self::restrictions::RestrictionResult;
3633

@@ -514,53 +511,3 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
514511
}
515512
}
516513

517-
/// Context used while gathering loans on static initializers
518-
///
519-
/// This visitor walks static initializer's expressions and makes
520-
/// sure the loans being taken are sound.
521-
struct StaticInitializerCtxt<'a, 'tcx: 'a> {
522-
bccx: &'a BorrowckCtxt<'a, 'tcx>,
523-
body_id: hir::BodyId,
524-
}
525-
526-
impl<'a, 'tcx> Visitor<'tcx> for StaticInitializerCtxt<'a, 'tcx> {
527-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
528-
NestedVisitorMap::None
529-
}
530-
531-
fn visit_expr(&mut self, ex: &'tcx Expr) {
532-
if let hir::ExprAddrOf(mutbl, ref base) = ex.node {
533-
let infcx = self.bccx.tcx.borrowck_fake_infer_ctxt(self.body_id);
534-
let mc = mc::MemCategorizationContext::new(&infcx);
535-
let base_cmt = mc.cat_expr(&base).unwrap();
536-
let borrow_kind = ty::BorrowKind::from_mutbl(mutbl);
537-
// Check that we don't allow borrows of unsafe static items.
538-
let err = check_aliasability(self.bccx, ex.span,
539-
BorrowViolation(euv::AddrOf),
540-
base_cmt, borrow_kind).is_err();
541-
if err {
542-
return; // reported an error, no sense in reporting more.
543-
}
544-
}
545-
546-
intravisit::walk_expr(self, ex);
547-
}
548-
}
549-
550-
pub fn gather_loans_in_static_initializer<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
551-
body: hir::BodyId) {
552-
debug!("gather_loans_in_static_initializer(expr={:?})", body);
553-
554-
let bccx = &BorrowckCtxt {
555-
tcx: tcx,
556-
tables: None
557-
};
558-
559-
let mut sicx = StaticInitializerCtxt {
560-
bccx: bccx,
561-
body_id: body
562-
};
563-
564-
let body = sicx.bccx.tcx.hir.body(body);
565-
sicx.visit_body(body);
566-
}

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowckCtxt<'a, 'tcx> {
8989
match item.node {
9090
hir::ItemStatic(.., ex) |
9191
hir::ItemConst(_, ex) => {
92-
gather_loans::gather_loans_in_static_initializer(self.tcx, ex);
92+
borrowck_fn(self.tcx, ex);
9393
}
9494
_ => { }
9595
}
@@ -99,14 +99,14 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowckCtxt<'a, 'tcx> {
9999

100100
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
101101
if let hir::TraitItemKind::Const(_, Some(expr)) = ti.node {
102-
gather_loans::gather_loans_in_static_initializer(self.tcx, expr);
102+
borrowck_fn(self.tcx, expr);
103103
}
104104
intravisit::walk_trait_item(self, ti);
105105
}
106106

107107
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
108108
if let hir::ImplItemKind::Const(_, expr) = ii.node {
109-
gather_loans::gather_loans_in_static_initializer(self.tcx, expr);
109+
borrowck_fn(self.tcx, expr);
110110
}
111111
intravisit::walk_impl_item(self, ii);
112112
}
@@ -147,7 +147,7 @@ fn borrowck_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, body_id: hir::BodyId) {
147147
mir::borrowck_mir(bccx, owner_id, &attributes);
148148
}
149149

150-
let cfg = cfg::CFG::new(bccx.tcx, &body.value);
150+
let cfg = cfg::CFG::new(bccx.tcx, &body);
151151
let AnalysisData { all_loans,
152152
loans: loan_dfcx,
153153
move_data: flowed_moves } =

src/librustc_driver/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#![deny(warnings)]
2525

2626
#![feature(box_syntax)]
27+
#![feature(loop_break_value)]
2728
#![feature(libc)]
2829
#![feature(quote)]
2930
#![feature(rustc_diagnostic_macros)]

src/librustc_driver/pretty.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,24 @@ fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
718718
mode: PpFlowGraphMode,
719719
mut out: W)
720720
-> io::Result<()> {
721-
let cfg = match code {
722-
blocks::Code::Expr(expr) => cfg::CFG::new(tcx, expr),
723-
blocks::Code::FnLike(fn_like) => {
724-
let body = tcx.hir.body(fn_like.body());
725-
cfg::CFG::new(tcx, &body.value)
726-
},
721+
let body_id = match code {
722+
blocks::Code::Expr(expr) => {
723+
// Find the function this expression is from.
724+
let mut node_id = expr.id;
725+
loop {
726+
let node = tcx.hir.get(node_id);
727+
if let Some(n) = hir::map::blocks::FnLikeNode::from_node(node) {
728+
break n.body();
729+
}
730+
let parent = tcx.hir.get_parent_node(node_id);
731+
assert!(node_id != parent);
732+
node_id = parent;
733+
}
734+
}
735+
blocks::Code::FnLike(fn_like) => fn_like.body(),
727736
};
737+
let body = tcx.hir.body(body_id);
738+
let cfg = cfg::CFG::new(tcx, &body);
728739
let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
729740
let lcfg = LabelledCFG {
730741
hir_map: &tcx.hir,

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnconditionalRecursion {
712712
// to have behaviour like the above, rather than
713713
// e.g. accidentally recurring after an assert.
714714

715-
let cfg = cfg::CFG::new(cx.tcx, &body.value);
715+
let cfg = cfg::CFG::new(cx.tcx, &body);
716716

717717
let mut work_queue = vec![cfg.entry];
718718
let mut reached_exit_without_self_call = false;

src/test/compile-fail/E0017.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
1919
//~| NOTE statics require immutable values
2020
//~| ERROR E0017
2121
//~| NOTE statics require immutable values
22-
//~| ERROR E0388
23-
//~| NOTE cannot write data in a static definition
22+
//~| ERROR cannot borrow
2423
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
2524
//~| NOTE statics require immutable values
2625
//~| ERROR E0017

src/test/compile-fail/E0388.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const CR: &'static mut i32 = &mut C; //~ ERROR E0017
1515
//~| ERROR E0017
1616
static STATIC_REF: &'static mut i32 = &mut X; //~ ERROR E0017
1717
//~| ERROR E0017
18-
//~| ERROR E0388
18+
//~| ERROR cannot borrow
1919
static CONST_REF: &'static mut i32 = &mut C; //~ ERROR E0017
2020
//~| ERROR E0017
2121

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #38520. Check that moves of `Foo` are not
12+
// permitted as `Foo` is not copy (even in a static/const
13+
// initializer).
14+
15+
#![feature(const_fn)]
16+
17+
struct Foo(usize);
18+
19+
const fn get(x: Foo) -> usize {
20+
x.0
21+
}
22+
23+
const X: Foo = Foo(22);
24+
static Y: usize = get(*&X); //~ ERROR E0507
25+
const Z: usize = get(*&X); //~ ERROR E0507
26+
27+
fn main() {
28+
}

src/test/compile-fail/issue-18118.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ pub fn main() {
1313
//~^ ERROR blocks in constants are limited to items and tail expressions
1414
let p = 3;
1515
//~^ ERROR blocks in constants are limited to items and tail expressions
16-
&p
16+
&p //~ ERROR `p` does not live long enough
1717
};
1818
}

0 commit comments

Comments
 (0)