Skip to content

Commit 5b2f6c7

Browse files
committed
Consume MirVisitor changes and use more accurate spans for region renumbering
1 parent d2fc06e commit 5b2f6c7

File tree

1 file changed

+23
-18
lines changed
  • src/librustc_mir/transform

1 file changed

+23
-18
lines changed

src/librustc_mir/transform/nll.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ use rustc::ty::TypeFoldable;
1212
use rustc::ty::subst::Substs;
1313
use rustc::ty::{Ty, TyCtxt, ClosureSubsts};
1414
use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind};
15-
use rustc::mir::visit::MutVisitor;
15+
use rustc::mir::visit::{MutVisitor, Lookup};
1616
use rustc::mir::transform::{MirPass, MirSource};
1717
use rustc::infer::{self, InferCtxt};
1818
use syntax_pos::Span;
1919

2020
#[allow(dead_code)]
2121
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
2222
infcx: InferCtxt<'a, 'gcx, 'tcx>,
23-
source: Mir<'tcx>
23+
source: &'a Mir<'tcx>
2424
}
2525

2626
impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
27-
pub fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>, source: Mir<'tcx>) -> Self {
27+
pub fn new(infcx: InferCtxt<'a, 'gcx, 'tcx>, source: &'a Mir<'tcx>) -> Self {
2828
NLLVisitor {
2929
infcx: infcx,
3030
source: source,
@@ -38,28 +38,29 @@ impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
3838
}
3939
}
4040

41-
fn span_from_location<'tcx>(source: Mir<'tcx>, location: Location) -> Span {
41+
fn span_from_location<'tcx>(source: &Mir<'tcx>, location: Location) -> Span {
4242
source[location.block].statements[location.statement_index].source_info.span
4343
}
4444

4545
impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
46-
fn visit_ty(&mut self, ty: &mut Ty<'tcx>) {
46+
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, lookup: Lookup) {
4747
let old_ty = *ty;
48-
// FIXME: Nashenas88 - span should be narrowed down
49-
*ty = self.renumber_regions(&old_ty, self.source.span);
48+
let span = match lookup {
49+
Lookup::Loc(location) => span_from_location(self.source, location),
50+
Lookup::Src(source_info) => source_info.span,
51+
};
52+
*ty = self.renumber_regions(&old_ty, span);
5053
}
5154

52-
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>) {
53-
// FIXME: Nashenas88 - span should be narrowed down
54-
*substs = self.renumber_regions(&{*substs}, self.source.span);
55+
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) {
56+
*substs = self.renumber_regions(&{*substs}, span_from_location(self.source, location));
5557
}
5658

5759
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
5860
match *rvalue {
5961
Rvalue::Ref(ref mut r, _, _) => {
60-
let span = span_from_location(location);
6162
let old_r = *r;
62-
*r = self.renumber_regions(&old_r, span);
63+
*r = self.renumber_regions(&old_r, span_from_location(self.source, location));
6364
}
6465
Rvalue::Use(..) |
6566
Rvalue::Repeat(..) |
@@ -78,9 +79,9 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
7879
}
7980

8081
fn visit_closure_substs(&mut self,
81-
substs: &mut ClosureSubsts<'tcx>) {
82-
// FIXME: Nashenas88 - span should be narrowed down
83-
*substs = self.renumber_regions(substs, self.source.span);
82+
substs: &mut ClosureSubsts<'tcx>,
83+
location: Location) {
84+
*substs = self.renumber_regions(substs, span_from_location(self.source, location));
8485
}
8586

8687
fn visit_statement(&mut self,
@@ -107,11 +108,15 @@ impl MirPass for NLL {
107108
}
108109

109110
tcx.infer_ctxt().enter(|infcx| {
110-
let mut visitor = NLLVisitor::new(infcx, mir.clone());
111111
// Clone mir so we can mutate it without disturbing the rest
112112
// of the compiler
113-
let mut mir = mir.clone();
114-
visitor.visit_mir(&mut mir);
113+
let mut renumbered_mir = mir.clone();
114+
115+
// Note that we're using the passed-in mir for the visitor. This is
116+
// so we can lookup locations during traversal without worrying about
117+
// maintaing both a mutable and immutable reference to the same object
118+
let mut visitor = NLLVisitor::new(infcx, &mir);
119+
visitor.visit_mir(&mut renumbered_mir);
115120
})
116121
}
117122
}

0 commit comments

Comments
 (0)