Skip to content

Commit

Permalink
rustc_span: return an impl Iterator instead of a Vec from macro_backt…
Browse files Browse the repository at this point in the history
…race.
  • Loading branch information
eddyb committed Jan 26, 2020
1 parent 75284f8 commit 6980f82
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ pub trait Emitter {
if call_sp != *sp && !always_backtrace {
before_after.push((*sp, call_sp));
}
let backtrace_len = sp.macro_backtrace().len();
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
let macro_backtrace: Vec<_> = sp.macro_backtrace().collect();
let backtrace_len = macro_backtrace.len();
for (i, trace) in macro_backtrace.iter().rev().enumerate() {
// Only show macro locations that are local
// and display them like a span_note
if trace.def_site.is_dummy() {
Expand Down Expand Up @@ -398,8 +399,7 @@ pub trait Emitter {
continue;
}
if sm.span_to_filename(sp_label.span.clone()).is_macros() && !always_backtrace {
let v = sp_label.span.macro_backtrace();
if let Some(use_site) = v.last() {
if let Some(use_site) = sp_label.span.macro_backtrace().last() {
before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_errors/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl DiagnosticSpan {
// backtrace ourselves, but the `macro_backtrace` helper makes
// some decision, such as dropping some frames, and I don't
// want to duplicate that logic here.
let backtrace = span.macro_backtrace().into_iter();
let backtrace = span.macro_backtrace();
DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
}

Expand All @@ -318,7 +318,7 @@ impl DiagnosticSpan {
is_primary: bool,
label: Option<String>,
suggestion: Option<(&String, Applicability)>,
mut backtrace: vec::IntoIter<ExpnData>,
mut backtrace: impl Iterator<Item = ExpnData>,
je: &JsonEmitter,
) -> DiagnosticSpan {
let start = je.sm.lookup_char_pos(span.lo());
Expand Down
33 changes: 18 additions & 15 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,26 @@ impl Span {
self.ctxt().outer_expn_data().allow_internal_unsafe
}

pub fn macro_backtrace(mut self) -> Vec<ExpnData> {
pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
let mut prev_span = DUMMY_SP;
let mut result = vec![];
loop {
let expn_data = self.ctxt().outer_expn_data();
if expn_data.is_root() {
break;
}
// Don't print recursive invocations.
if !expn_data.call_site.source_equal(&prev_span) {
result.push(expn_data.clone());
}
std::iter::from_fn(move || {
loop {
let expn_data = self.ctxt().outer_expn_data();
if expn_data.is_root() {
return None;
}

prev_span = self;
self = expn_data.call_site;
}
result
let is_recursive = expn_data.call_site.source_equal(&prev_span);

prev_span = self;
self = expn_data.call_site;

// Don't print recursive invocations.
if !is_recursive {
return Some(expn_data);
}
}
})
}

/// Returns a `Span` that would enclose both `self` and `end`.
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_span/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,7 @@ impl SourceMap {
}
pub fn call_span_if_macro(&self, sp: Span) -> Span {
if self.span_to_filename(sp.clone()).is_macros() {
let v = sp.macro_backtrace();
if let Some(use_site) = v.last() {
if let Some(use_site) = sp.macro_backtrace().last() {
return use_site.call_site;
}
}
Expand Down

0 comments on commit 6980f82

Please sign in to comment.