rustc_span: return an impl Iterator instead of a Vec from macro_backtrace.

This commit is contained in:
Eduard-Mihai Burtescu 2020-01-21 01:46:53 +02:00
parent 75284f8cbd
commit 6980f82c0d
4 changed files with 25 additions and 23 deletions

View File

@ -343,8 +343,9 @@ fn fix_multispan_in_std_macros(
if call_sp != *sp && !always_backtrace { if call_sp != *sp && !always_backtrace {
before_after.push((*sp, call_sp)); before_after.push((*sp, call_sp));
} }
let backtrace_len = sp.macro_backtrace().len(); let macro_backtrace: Vec<_> = sp.macro_backtrace().collect();
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() { let backtrace_len = macro_backtrace.len();
for (i, trace) in macro_backtrace.iter().rev().enumerate() {
// Only show macro locations that are local // Only show macro locations that are local
// and display them like a span_note // and display them like a span_note
if trace.def_site.is_dummy() { if trace.def_site.is_dummy() {
@ -398,8 +399,7 @@ fn fix_multispan_in_std_macros(
continue; continue;
} }
if sm.span_to_filename(sp_label.span.clone()).is_macros() && !always_backtrace { 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) = sp_label.span.macro_backtrace().last() {
if let Some(use_site) = v.last() {
before_after.push((sp_label.span.clone(), use_site.call_site.clone())); before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
} }
} }

View File

@ -309,7 +309,7 @@ fn from_span_etc(
// backtrace ourselves, but the `macro_backtrace` helper makes // backtrace ourselves, but the `macro_backtrace` helper makes
// some decision, such as dropping some frames, and I don't // some decision, such as dropping some frames, and I don't
// want to duplicate that logic here. // 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) DiagnosticSpan::from_span_full(span, is_primary, label, suggestion, backtrace, je)
} }
@ -318,7 +318,7 @@ fn from_span_full(
is_primary: bool, is_primary: bool,
label: Option<String>, label: Option<String>,
suggestion: Option<(&String, Applicability)>, suggestion: Option<(&String, Applicability)>,
mut backtrace: vec::IntoIter<ExpnData>, mut backtrace: impl Iterator<Item = ExpnData>,
je: &JsonEmitter, je: &JsonEmitter,
) -> DiagnosticSpan { ) -> DiagnosticSpan {
let start = je.sm.lookup_char_pos(span.lo()); let start = je.sm.lookup_char_pos(span.lo());

View File

@ -445,23 +445,26 @@ pub fn allows_unsafe(&self) -> bool {
self.ctxt().outer_expn_data().allow_internal_unsafe 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 prev_span = DUMMY_SP;
let mut result = vec![]; std::iter::from_fn(move || {
loop { loop {
let expn_data = self.ctxt().outer_expn_data(); let expn_data = self.ctxt().outer_expn_data();
if expn_data.is_root() { if expn_data.is_root() {
break; return None;
} }
// Don't print recursive invocations.
if !expn_data.call_site.source_equal(&prev_span) {
result.push(expn_data.clone());
}
prev_span = self; let is_recursive = expn_data.call_site.source_equal(&prev_span);
self = expn_data.call_site;
} prev_span = self;
result 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`. /// Returns a `Span` that would enclose both `self` and `end`.

View File

@ -947,8 +947,7 @@ pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) ->
} }
pub fn call_span_if_macro(&self, sp: Span) -> Span { pub fn call_span_if_macro(&self, sp: Span) -> Span {
if self.span_to_filename(sp.clone()).is_macros() { if self.span_to_filename(sp.clone()).is_macros() {
let v = sp.macro_backtrace(); if let Some(use_site) = sp.macro_backtrace().last() {
if let Some(use_site) = v.last() {
return use_site.call_site; return use_site.call_site;
} }
} }