Skip to content

Commit 6980f82

Browse files
committed
rustc_span: return an impl Iterator instead of a Vec from macro_backtrace.
1 parent 75284f8 commit 6980f82

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

src/librustc_errors/emitter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,9 @@ pub trait Emitter {
343343
if call_sp != *sp && !always_backtrace {
344344
before_after.push((*sp, call_sp));
345345
}
346-
let backtrace_len = sp.macro_backtrace().len();
347-
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
346+
let macro_backtrace: Vec<_> = sp.macro_backtrace().collect();
347+
let backtrace_len = macro_backtrace.len();
348+
for (i, trace) in macro_backtrace.iter().rev().enumerate() {
348349
// Only show macro locations that are local
349350
// and display them like a span_note
350351
if trace.def_site.is_dummy() {
@@ -398,8 +399,7 @@ pub trait Emitter {
398399
continue;
399400
}
400401
if sm.span_to_filename(sp_label.span.clone()).is_macros() && !always_backtrace {
401-
let v = sp_label.span.macro_backtrace();
402-
if let Some(use_site) = v.last() {
402+
if let Some(use_site) = sp_label.span.macro_backtrace().last() {
403403
before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
404404
}
405405
}

src/librustc_errors/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl DiagnosticSpan {
309309
// backtrace ourselves, but the `macro_backtrace` helper makes
310310
// some decision, such as dropping some frames, and I don't
311311
// want to duplicate that logic here.
312-
let backtrace = span.macro_backtrace().into_iter();
312+
let backtrace = span.macro_backtrace();
313313
DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
314314
}
315315

@@ -318,7 +318,7 @@ impl DiagnosticSpan {
318318
is_primary: bool,
319319
label: Option<String>,
320320
suggestion: Option<(&String, Applicability)>,
321-
mut backtrace: vec::IntoIter<ExpnData>,
321+
mut backtrace: impl Iterator<Item = ExpnData>,
322322
je: &JsonEmitter,
323323
) -> DiagnosticSpan {
324324
let start = je.sm.lookup_char_pos(span.lo());

src/librustc_span/lib.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -445,23 +445,26 @@ impl Span {
445445
self.ctxt().outer_expn_data().allow_internal_unsafe
446446
}
447447

448-
pub fn macro_backtrace(mut self) -> Vec<ExpnData> {
448+
pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
449449
let mut prev_span = DUMMY_SP;
450-
let mut result = vec![];
451-
loop {
452-
let expn_data = self.ctxt().outer_expn_data();
453-
if expn_data.is_root() {
454-
break;
455-
}
456-
// Don't print recursive invocations.
457-
if !expn_data.call_site.source_equal(&prev_span) {
458-
result.push(expn_data.clone());
459-
}
450+
std::iter::from_fn(move || {
451+
loop {
452+
let expn_data = self.ctxt().outer_expn_data();
453+
if expn_data.is_root() {
454+
return None;
455+
}
460456

461-
prev_span = self;
462-
self = expn_data.call_site;
463-
}
464-
result
457+
let is_recursive = expn_data.call_site.source_equal(&prev_span);
458+
459+
prev_span = self;
460+
self = expn_data.call_site;
461+
462+
// Don't print recursive invocations.
463+
if !is_recursive {
464+
return Some(expn_data);
465+
}
466+
}
467+
})
465468
}
466469

467470
/// Returns a `Span` that would enclose both `self` and `end`.

src/librustc_span/source_map.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,7 @@ impl SourceMap {
947947
}
948948
pub fn call_span_if_macro(&self, sp: Span) -> Span {
949949
if self.span_to_filename(sp.clone()).is_macros() {
950-
let v = sp.macro_backtrace();
951-
if let Some(use_site) = v.last() {
950+
if let Some(use_site) = sp.macro_backtrace().last() {
952951
return use_site.call_site;
953952
}
954953
}

0 commit comments

Comments
 (0)