Skip to content

Commit c36707a

Browse files
committed
Add ScopeAuxiliaryVec, return MIR+aux via tuple
It's nice to be able to index with a scope-id, but coherence rules prevent us from implementing `Index<ScopeId>` for `Vec<ScopeAuxiliary>`, and I'd prefer that `ScopeAuxiliary` remain in librustc_mir, just for compilation time reasons.
1 parent 70d0123 commit c36707a

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

src/librustc_mir/build/matches/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
633633
span: span,
634634
});
635635
let index = index as u32;
636-
let extent = self.scope_auxiliary[var_scope_id.index()].extent;
636+
let extent = self.scope_auxiliary[var_scope_id].extent;
637637
self.schedule_drop(span, extent, &Lvalue::Var(index), var_ty);
638638
self.var_indices.insert(var_id, index);
639639

src/librustc_mir/build/mod.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc::middle::ty::{FnOutput, Ty};
1414
use rustc::mir::repr::*;
1515
use rustc_data_structures::fnv::FnvHashMap;
1616
use rustc_front::hir;
17-
17+
use std::ops::{Index, IndexMut};
1818
use syntax::ast;
1919
use syntax::codemap::Span;
2020

@@ -33,7 +33,7 @@ pub struct Builder<'a, 'tcx: 'a> {
3333
// but these are liable to get out of date once optimization
3434
// begins. They are also hopefully temporary, and will be
3535
// no longer needed when we adopt graph-based regions.
36-
scope_auxiliary: Vec<ScopeAuxiliary>,
36+
scope_auxiliary: ScopeAuxiliaryVec,
3737

3838
// the current set of loops; see the `scope` module for more
3939
// details
@@ -85,9 +85,24 @@ pub struct Location {
8585
pub statement_index: usize,
8686
}
8787

88-
pub struct MirAndScopeAuxiliary<'tcx> {
89-
pub mir: Mir<'tcx>,
90-
pub scope_auxiliary: Vec<ScopeAuxiliary>,
88+
pub struct ScopeAuxiliaryVec {
89+
pub vec: Vec<ScopeAuxiliary>
90+
}
91+
92+
impl Index<ScopeId> for ScopeAuxiliaryVec {
93+
type Output = ScopeAuxiliary;
94+
95+
#[inline]
96+
fn index(&self, index: ScopeId) -> &ScopeAuxiliary {
97+
&self.vec[index.index()]
98+
}
99+
}
100+
101+
impl IndexMut<ScopeId> for ScopeAuxiliaryVec {
102+
#[inline]
103+
fn index_mut(&mut self, index: ScopeId) -> &mut ScopeAuxiliary {
104+
&mut self.vec[index.index()]
105+
}
91106
}
92107

93108
///////////////////////////////////////////////////////////////////////////
@@ -143,7 +158,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
143158
argument_extent: CodeExtent,
144159
return_ty: FnOutput<'tcx>,
145160
ast_block: &'tcx hir::Block)
146-
-> MirAndScopeAuxiliary<'tcx> {
161+
-> (Mir<'tcx>, ScopeAuxiliaryVec) {
147162
let cfg = CFG { basic_blocks: vec![] };
148163

149164
let mut builder = Builder {
@@ -152,7 +167,7 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
152167
fn_span: span,
153168
scopes: vec![],
154169
scope_datas: vec![],
155-
scope_auxiliary: vec![],
170+
scope_auxiliary: ScopeAuxiliaryVec { vec: vec![] },
156171
loop_scopes: vec![],
157172
temp_decls: vec![],
158173
var_decls: vec![],
@@ -188,8 +203,8 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
188203
true
189204
}));
190205

191-
MirAndScopeAuxiliary {
192-
mir: Mir {
206+
(
207+
Mir {
193208
basic_blocks: builder.cfg.basic_blocks,
194209
scopes: builder.scope_datas,
195210
var_decls: builder.var_decls,
@@ -198,8 +213,8 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
198213
return_ty: return_ty,
199214
span: span
200215
},
201-
scope_auxiliary: builder.scope_auxiliary,
202-
}
216+
builder.scope_auxiliary,
217+
)
203218
}
204219

205220
impl<'a,'tcx> Builder<'a,'tcx> {

src/librustc_mir/build/scope.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
257257
free: None,
258258
cached_block: None,
259259
});
260-
self.scope_auxiliary.push(ScopeAuxiliary {
260+
self.scope_auxiliary.vec.push(ScopeAuxiliary {
261261
extent: extent,
262262
dom: self.cfg.current_location(entry),
263263
postdoms: vec![]
@@ -279,7 +279,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
279279
let scope = self.scopes.pop().unwrap();
280280
assert_eq!(scope.extent, extent);
281281
unpack!(block = build_scope_drops(&mut self.cfg, &scope, &self.scopes, block));
282-
self.scope_auxiliary[scope.id.index()]
282+
self.scope_auxiliary[scope.id]
283283
.postdoms
284284
.push(self.cfg.current_location(block));
285285
block.unit()
@@ -313,7 +313,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
313313
self.cfg.terminate(block, scope.id, span, free);
314314
block = next;
315315
}
316-
self.scope_auxiliary[scope.id.index()]
316+
self.scope_auxiliary[scope.id]
317317
.postdoms
318318
.push(self.cfg.current_location(block));
319319
}

src/librustc_mir/mir_map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
extern crate syntax;
2020
extern crate rustc_front;
2121

22-
use build::{self, MirAndScopeAuxiliary};
22+
use build;
2323
use rustc::dep_graph::DepNode;
2424
use rustc::mir::repr::Mir;
2525
use pretty;
@@ -183,7 +183,7 @@ fn build_mir<'a,'tcx:'a>(cx: Cx<'a,'tcx>,
183183
let parameter_scope =
184184
cx.tcx().region_maps.lookup_code_extent(
185185
CodeExtentData::ParameterScope { fn_id: fn_id, body_id: body.id });
186-
let MirAndScopeAuxiliary { mut mir, scope_auxiliary } =
186+
let (mut mir, scope_auxiliary) =
187187
build::construct(cx,
188188
span,
189189
implicit_arg_tys,

src/librustc_mir/pretty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use build::{Location, ScopeAuxiliary};
11+
use build::{Location, ScopeAuxiliaryVec};
1212
use rustc::mir::repr::*;
1313
use rustc::middle::ty::{self, TyCtxt};
1414
use rustc_data_structures::fnv::FnvHashMap;
@@ -39,7 +39,7 @@ pub fn dump_mir<'a, 'tcx>(tcx: &TyCtxt<'tcx>,
3939
disambiguator: &Display,
4040
node_id: NodeId,
4141
mir: &Mir<'tcx>,
42-
auxiliary: Option<&Vec<ScopeAuxiliary>>) {
42+
auxiliary: Option<&ScopeAuxiliaryVec>) {
4343
let filters = match tcx.sess.opts.debugging_opts.dump_mir {
4444
None => return,
4545
Some(ref filters) => filters,
@@ -91,12 +91,12 @@ pub fn write_mir_fn<'tcx>(tcx: &TyCtxt<'tcx>,
9191
node_id: NodeId,
9292
mir: &Mir<'tcx>,
9393
w: &mut Write,
94-
auxiliary: Option<&Vec<ScopeAuxiliary>>)
94+
auxiliary: Option<&ScopeAuxiliaryVec>)
9595
-> io::Result<()> {
9696
// compute scope/entry exit annotations
9797
let mut annotations = FnvHashMap();
9898
if let Some(auxiliary) = auxiliary {
99-
for (index, auxiliary) in auxiliary.iter().enumerate() {
99+
for (index, auxiliary) in auxiliary.vec.iter().enumerate() {
100100
let scope_id = ScopeId::new(index);
101101

102102
annotations.entry(auxiliary.dom)
@@ -183,7 +183,7 @@ fn comment(tcx: &TyCtxt,
183183

184184
fn write_scope_tree(tcx: &TyCtxt,
185185
mir: &Mir,
186-
auxiliary: Option<&Vec<ScopeAuxiliary>>,
186+
auxiliary: Option<&ScopeAuxiliaryVec>,
187187
scope_tree: &FnvHashMap<Option<ScopeId>, Vec<ScopeId>>,
188188
w: &mut Write,
189189
parent: Option<ScopeId>,
@@ -201,7 +201,7 @@ fn write_scope_tree(tcx: &TyCtxt,
201201
}
202202

203203
if let Some(auxiliary) = auxiliary {
204-
let extent = auxiliary[child.index()].extent;
204+
let extent = auxiliary[child].extent;
205205
let data = tcx.region_maps.code_extent_data(extent);
206206
writeln!(w, "{0:1$}Extent: {2:?}", "", indent, data)?;
207207
}

0 commit comments

Comments
 (0)