Skip to content

Commit df90a54

Browse files
committed
Auto merge of #43533 - nrc:macro-save, r=jseyfried,
Three small fixes for save-analysis First commit does some naive deduplication of macro uses. We end up with lots of duplication here because of the weird way we get this data (we extract a use for every span generated by a macro use). Second commit is basically a typo fix. Third commit is a bit interesting, it partially reverts a change from #40939 where temporary variables in format! (and thus println!) got a span with the primary pointing at the value stored into the temporary (e.g., `x` in `println!("...", x)`). If `format!` had a definition it should point at the temporary in the macro def, but since it is built-in, that is not possible (for now), so `DUMMY_SP` is the best we can do (using the span in the callee really breaks save-analysis because it thinks `x` is a definition as well as a reference). There aren't a test for this stuff because: the deduplication is filtered by any of the users of save-analysis, so it is purely an efficiency change. I couldn't actually find an example for the second commit that we have any machinery to test, and the third commit is tested by the RLS, so there will be a test once I update the RLS version and and uncomment the previously failing tests). r? @jseyfried
2 parents ebf74d9 + 27b9182 commit df90a54

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

src/Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_save_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["dylib"]
1111
[dependencies]
1212
log = "0.3"
1313
rustc = { path = "../librustc" }
14+
rustc_data_structures = { path = "../librustc_data_structures" }
1415
rustc_typeck = { path = "../librustc_typeck" }
1516
syntax = { path = "../libsyntax" }
1617
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_save_analysis/dump_visitor.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc::hir::def_id::DefId;
2929
use rustc::hir::map::Node;
3030
use rustc::session::Session;
3131
use rustc::ty::{self, TyCtxt};
32+
use rustc_data_structures::fx::FxHashSet;
3233

3334
use std::path::Path;
3435

@@ -74,6 +75,7 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> {
7475
// we only write one macro def per unique macro definition, and
7576
// one macro use per unique callsite span.
7677
// mac_defs: HashSet<Span>,
78+
macro_calls: FxHashSet<Span>,
7779
}
7880

7981
impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
@@ -89,6 +91,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
8991
span: span_utils.clone(),
9092
cur_scope: CRATE_NODE_ID,
9193
// mac_defs: HashSet::new(),
94+
macro_calls: FxHashSet(),
9295
}
9396
}
9497

@@ -972,11 +975,19 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
972975
/// callsite spans to record macro definition and use data, using the
973976
/// mac_uses and mac_defs sets to prevent multiples.
974977
fn process_macro_use(&mut self, span: Span) {
978+
let source_span = span.source_callsite();
979+
if self.macro_calls.contains(&source_span) {
980+
return;
981+
}
982+
self.macro_calls.insert(source_span);
983+
975984
let data = match self.save_ctxt.get_macro_use_data(span) {
976985
None => return,
977986
Some(data) => data,
978987
};
979988

989+
self.dumper.macro_use(data);
990+
980991
// FIXME write the macro def
981992
// let mut hasher = DefaultHasher::new();
982993
// data.callee_span.hash(&mut hasher);
@@ -996,7 +1007,6 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
9961007
// }.lower(self.tcx));
9971008
// }
9981009
// }
999-
self.dumper.macro_use(data);
10001010
}
10011011

10021012
fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) {

src/librustc_save_analysis/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#[macro_use] extern crate log;
2525
#[macro_use] extern crate syntax;
26+
extern crate rustc_data_structures;
2627
extern crate rustc_serialize;
2728
extern crate rustc_typeck;
2829
extern crate syntax_pos;

src/librustc_save_analysis/span_utils.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ impl<'a> SpanUtils<'a> {
398398
return false;
399399
}
400400
// If sub_span is none, filter out generated code.
401-
if sub_span.is_none() {
402-
return true;
403-
}
401+
let sub_span = match sub_span {
402+
Some(ss) => ss,
403+
None => return true,
404+
};
404405

405406
//If the span comes from a fake filemap, filter it.
406407
if !self.sess.codemap().lookup_char_pos(parent.lo).file.is_real_file() {
@@ -409,7 +410,7 @@ impl<'a> SpanUtils<'a> {
409410

410411
// Otherwise, a generated span is deemed invalid if it is not a sub-span of the root
411412
// callsite. This filters out macro internal variables and most malformed spans.
412-
!parent.source_callsite().contains(parent)
413+
!parent.source_callsite().contains(sub_span)
413414
}
414415
}
415416

src/libsyntax_ext/format.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::ext::build::AstBuilder;
2020
use syntax::parse::token;
2121
use syntax::ptr::P;
2222
use syntax::symbol::{Symbol, keywords};
23-
use syntax_pos::Span;
23+
use syntax_pos::{Span, DUMMY_SP};
2424
use syntax::tokenstream;
2525

2626
use std::collections::{HashMap, HashSet};
@@ -558,8 +558,10 @@ impl<'a, 'b> Context<'a, 'b> {
558558
// passed to this function.
559559
for (i, e) in self.args.into_iter().enumerate() {
560560
let name = self.ecx.ident_of(&format!("__arg{}", i));
561-
let span =
562-
Span { ctxt: e.span.ctxt.apply_mark(self.ecx.current_expansion.mark), ..e.span };
561+
let span = Span {
562+
ctxt: e.span.ctxt.apply_mark(self.ecx.current_expansion.mark),
563+
..DUMMY_SP
564+
};
563565
pats.push(self.ecx.pat_ident(span, name));
564566
for ref arg_ty in self.arg_unique_types[i].iter() {
565567
locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name));

0 commit comments

Comments
 (0)