Skip to content

Commit 32cc287

Browse files
authored
Rollup merge of rust-lang#39927 - nikomatsakis:incr-comp-skip-borrowck-2, r=eddyb
transition borrowck to visit all **bodies** and not item-likes This is a better structure for incremental compilation and also more compatible with the eventual borrowck mir. It also fixes rust-lang#38520 as a drive-by fix. r? @eddyb
2 parents 3b379c7 + fed58f3 commit 32cc287

File tree

28 files changed

+242
-306
lines changed

28 files changed

+242
-306
lines changed

src/librustc/cfg/construct.rs

+6-14
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

+1-1
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/dep_graph/dep_node.rs

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub enum DepNode<D: Clone + Debug> {
9090
// things read/modify that MIR.
9191
Mir(D),
9292

93+
BorrowCheckKrate,
9394
BorrowCheck(D),
9495
RvalueCheck(D),
9596
Reachability,
@@ -206,6 +207,7 @@ impl<D: Clone + Debug> DepNode<D> {
206207

207208
match *self {
208209
Krate => Some(Krate),
210+
BorrowCheckKrate => Some(BorrowCheckKrate),
209211
CollectLanguageItems => Some(CollectLanguageItems),
210212
CheckStaticRecursion => Some(CheckStaticRecursion),
211213
ResolveLifetimes => Some(ResolveLifetimes),

src/librustc/dep_graph/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ pub use self::dep_node::WorkProductId;
2525
pub use self::graph::DepGraph;
2626
pub use self::graph::WorkProduct;
2727
pub use self::query::DepGraphQuery;
28+
pub use self::visit::visit_all_bodies_in_krate;
2829
pub use self::visit::visit_all_item_likes_in_krate;
2930
pub use self::raii::DepTask;

src/librustc/dep_graph/visit.rs

+10
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,13 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
7474
};
7575
krate.visit_all_item_likes(&mut tracking_visitor)
7676
}
77+
78+
pub fn visit_all_bodies_in_krate<'a, 'tcx, C>(tcx: TyCtxt<'a, 'tcx, 'tcx>, callback: C)
79+
where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
80+
{
81+
let krate = tcx.hir.krate();
82+
for &body_id in &krate.body_ids {
83+
let body_owner_def_id = tcx.hir.body_owner_def_id(body_id);
84+
callback(body_owner_def_id, body_id);
85+
}
86+
}

src/librustc/hir/lowering.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use hir::map::definitions::DefPathData;
4646
use hir::def_id::{DefIndex, DefId};
4747
use hir::def::{Def, PathResolution};
4848
use session::Session;
49-
use util::nodemap::{DefIdMap, NodeMap, FxHashMap};
49+
use util::nodemap::{DefIdMap, NodeMap};
5050

5151
use std::collections::BTreeMap;
5252
use std::iter;
@@ -77,7 +77,7 @@ pub struct LoweringContext<'a> {
7777

7878
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
7979
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
80-
bodies: FxHashMap<hir::BodyId, hir::Body>,
80+
bodies: BTreeMap<hir::BodyId, hir::Body>,
8181

8282
type_def_lifetime_params: DefIdMap<usize>,
8383
}
@@ -111,7 +111,7 @@ pub fn lower_crate(sess: &Session,
111111
items: BTreeMap::new(),
112112
trait_items: BTreeMap::new(),
113113
impl_items: BTreeMap::new(),
114-
bodies: FxHashMap(),
114+
bodies: BTreeMap::new(),
115115
type_def_lifetime_params: DefIdMap(),
116116
}.lower_crate(krate)
117117
}
@@ -185,6 +185,7 @@ impl<'a> LoweringContext<'a> {
185185
let module = self.lower_mod(&c.module);
186186
let attrs = self.lower_attrs(&c.attrs);
187187
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
188+
let body_ids = body_ids(&self.bodies);
188189

189190
hir::Crate {
190191
module: module,
@@ -195,6 +196,7 @@ impl<'a> LoweringContext<'a> {
195196
trait_items: self.trait_items,
196197
impl_items: self.impl_items,
197198
bodies: self.bodies,
199+
body_ids: body_ids,
198200
}
199201
}
200202

@@ -2408,3 +2410,11 @@ impl<'a> LoweringContext<'a> {
24082410
}
24092411
}
24102412
}
2413+
2414+
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
2415+
// Sorting by span ensures that we get things in order within a
2416+
// file, and also puts the files in a sensible order.
2417+
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
2418+
body_ids.sort_by_key(|b| bodies[b].value.span);
2419+
body_ids
2420+
}

src/librustc/hir/map/mod.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -168,43 +168,48 @@ impl<'hir> MapEntry<'hir> {
168168
})
169169
}
170170

171-
fn is_body_owner(self, node_id: NodeId) -> bool {
171+
fn associated_body(self) -> Option<BodyId> {
172172
match self {
173173
EntryItem(_, item) => {
174174
match item.node {
175175
ItemConst(_, body) |
176176
ItemStatic(.., body) |
177-
ItemFn(_, _, _, _, _, body) => body.node_id == node_id,
178-
_ => false
177+
ItemFn(_, _, _, _, _, body) => Some(body),
178+
_ => None,
179179
}
180180
}
181181

182182
EntryTraitItem(_, item) => {
183183
match item.node {
184184
TraitItemKind::Const(_, Some(body)) |
185-
TraitItemKind::Method(_, TraitMethod::Provided(body)) => {
186-
body.node_id == node_id
187-
}
188-
_ => false
185+
TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body),
186+
_ => None
189187
}
190188
}
191189

192190
EntryImplItem(_, item) => {
193191
match item.node {
194192
ImplItemKind::Const(_, body) |
195-
ImplItemKind::Method(_, body) => body.node_id == node_id,
196-
_ => false
193+
ImplItemKind::Method(_, body) => Some(body),
194+
_ => None,
197195
}
198196
}
199197

200198
EntryExpr(_, expr) => {
201199
match expr.node {
202-
ExprClosure(.., body, _) => body.node_id == node_id,
203-
_ => false
200+
ExprClosure(.., body, _) => Some(body),
201+
_ => None,
204202
}
205203
}
206204

207-
_ => false
205+
_ => None
206+
}
207+
}
208+
209+
fn is_body_owner(self, node_id: NodeId) -> bool {
210+
match self.associated_body() {
211+
Some(b) => b.node_id == node_id,
212+
None => false,
208213
}
209214
}
210215
}

src/librustc/hir/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub use self::PathParameters::*;
3131

3232
use hir::def::Def;
3333
use hir::def_id::DefId;
34-
use util::nodemap::{NodeMap, FxHashMap, FxHashSet};
34+
use util::nodemap::{NodeMap, FxHashSet};
3535

3636
use syntax_pos::{Span, ExpnId, DUMMY_SP};
3737
use syntax::codemap::{self, Spanned};
@@ -409,7 +409,13 @@ pub struct Crate {
409409

410410
pub trait_items: BTreeMap<TraitItemId, TraitItem>,
411411
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
412-
pub bodies: FxHashMap<BodyId, Body>,
412+
pub bodies: BTreeMap<BodyId, Body>,
413+
414+
/// A list of the body ids written out in the order in which they
415+
/// appear in the crate. If you're going to process all the bodies
416+
/// in the crate, you should iterate over this list rather than the keys
417+
/// of bodies.
418+
pub body_ids: Vec<BodyId>,
413419
}
414420

415421
impl Crate {

src/librustc/middle/free_region.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ty::{self, TyCtxt, FreeRegion, Region};
1919
use ty::wf::ImpliedBound;
2020
use rustc_data_structures::transitive_relation::TransitiveRelation;
2121

22-
#[derive(Clone)]
22+
#[derive(Clone, RustcEncodable, RustcDecodable)]
2323
pub struct FreeRegionMap {
2424
// Stores the relation `a < b`, where `a` and `b` are regions.
2525
relation: TransitiveRelation<Region>
@@ -30,6 +30,10 @@ impl FreeRegionMap {
3030
FreeRegionMap { relation: TransitiveRelation::new() }
3131
}
3232

33+
pub fn is_empty(&self) -> bool {
34+
self.relation.is_empty()
35+
}
36+
3337
pub fn relate_free_regions_from_implied_bounds<'tcx>(&mut self,
3438
implied_bounds: &[ImpliedBound<'tcx>])
3539
{

src/librustc/ty/context.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ pub struct TypeckTables<'tcx> {
242242

243243
/// Lints for the body of this fn generated by typeck.
244244
pub lints: lint::LintTable,
245+
246+
/// Stores the free-region relationships that were deduced from
247+
/// its where clauses and parameter types. These are then
248+
/// read-again by borrowck.
249+
pub free_region_map: FreeRegionMap,
245250
}
246251

247252
impl<'tcx> TypeckTables<'tcx> {
@@ -259,6 +264,7 @@ impl<'tcx> TypeckTables<'tcx> {
259264
fru_field_types: NodeMap(),
260265
cast_kinds: NodeMap(),
261266
lints: lint::LintTable::new(),
267+
free_region_map: FreeRegionMap::new(),
262268
}
263269
}
264270

@@ -406,14 +412,6 @@ pub struct GlobalCtxt<'tcx> {
406412

407413
pub region_maps: RegionMaps,
408414

409-
// For each fn declared in the local crate, type check stores the
410-
// free-region relationships that were deduced from its where
411-
// clauses and parameter types. These are then read-again by
412-
// borrowck. (They are not used during trans, and hence are not
413-
// serialized or needed for cross-crate fns.)
414-
free_region_maps: RefCell<NodeMap<FreeRegionMap>>,
415-
// FIXME: jroesch make this a refcell
416-
417415
pub tables: RefCell<DepTrackingMap<maps::TypeckTables<'tcx>>>,
418416

419417
/// Maps from a trait item to the trait item "descriptor"
@@ -706,16 +704,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
706704
interned
707705
}
708706

709-
pub fn store_free_region_map(self, id: NodeId, map: FreeRegionMap) {
710-
if self.free_region_maps.borrow_mut().insert(id, map).is_some() {
711-
bug!("Tried to overwrite interned FreeRegionMap for NodeId {:?}", id)
712-
}
713-
}
714-
715-
pub fn free_region_map(self, id: NodeId) -> FreeRegionMap {
716-
self.free_region_maps.borrow()[&id].clone()
717-
}
718-
719707
pub fn lift<T: ?Sized + Lift<'tcx>>(self, value: &T) -> Option<T::Lifted> {
720708
value.lift_to_tcx(self)
721709
}
@@ -762,7 +750,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
762750
types: common_types,
763751
named_region_map: named_region_map,
764752
region_maps: region_maps,
765-
free_region_maps: RefCell::new(FxHashMap()),
766753
item_variance_map: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
767754
variance_computed: Cell::new(false),
768755
sess: s,

src/librustc/ty/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26682668
dep_graph::visit_all_item_likes_in_krate(self.global_tcx(), dep_node_fn, visitor);
26692669
}
26702670

2671+
/// Invokes `callback` for each body in the krate. This will
2672+
/// create a read edge from `DepNode::Krate` to the current task;
2673+
/// it is meant to be run in the context of some global task like
2674+
/// `BorrowckCrate`. The callback would then create a task like
2675+
/// `BorrowckBody(DefId)` to process each individual item.
2676+
pub fn visit_all_bodies_in_krate<C>(self, callback: C)
2677+
where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
2678+
{
2679+
dep_graph::visit_all_bodies_in_krate(self.global_tcx(), callback)
2680+
}
2681+
26712682
/// Looks up the span of `impl_did` if the impl is local; otherwise returns `Err`
26722683
/// with the name of the crate containing the impl.
26732684
pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, Symbol> {

src/librustc_borrowck/borrowck/fragments.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc::middle::mem_categorization as mc;
2727
use std::mem;
2828
use std::rc::Rc;
2929
use syntax::ast;
30-
use syntax_pos::{Span, DUMMY_SP};
30+
use syntax_pos::DUMMY_SP;
3131

3232
#[derive(PartialEq, Eq, PartialOrd, Ord)]
3333
enum Fragment {
@@ -200,14 +200,15 @@ impl FragmentSets {
200200

201201
pub fn instrument_move_fragments<'a, 'tcx>(this: &MoveData<'tcx>,
202202
tcx: TyCtxt<'a, 'tcx, 'tcx>,
203-
sp: Span,
204203
id: ast::NodeId) {
205204
let span_err = tcx.hir.attrs(id).iter()
206205
.any(|a| a.check_name("rustc_move_fragments"));
207206
let print = tcx.sess.opts.debugging_opts.print_move_fragments;
208207

209208
if !span_err && !print { return; }
210209

210+
let sp = tcx.hir.span(id);
211+
211212
let instrument_all_paths = |kind, vec_rc: &Vec<MovePathIndex>| {
212213
for (i, mpi) in vec_rc.iter().enumerate() {
213214
let lp = || this.path_loan_path(*mpi);

0 commit comments

Comments
 (0)