Skip to content

Commit f93827f

Browse files
authored
Simplify Emitter::fix_multispan_in_std_macros
1. Rewrite `if let` into `match` to return earl and avoid indenting giant block 2. Assign `spans_updated` only once
1 parent 9b447e2 commit f93827f

File tree

1 file changed

+65
-64
lines changed

1 file changed

+65
-64
lines changed

src/librustc_errors/emitter.rs

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -295,81 +295,82 @@ pub trait Emitter {
295295
source_map: &Option<Lrc<SourceMapperDyn>>,
296296
span: &mut MultiSpan,
297297
always_backtrace: bool) -> bool {
298-
let mut spans_updated = false;
298+
let sm = match source_map {
299+
Some(ref sm) => sm,
300+
None => return false,
301+
};
299302

300-
if let Some(ref sm) = source_map {
301-
let mut before_after: Vec<(Span, Span)> = vec![];
302-
let mut new_labels: Vec<(Span, String)> = vec![];
303+
let mut before_after: Vec<(Span, Span)> = vec![];
304+
let mut new_labels: Vec<(Span, String)> = vec![];
303305

304-
// First, find all the spans in <*macros> and point instead at their use site
305-
for sp in span.primary_spans() {
306-
if sp.is_dummy() {
306+
// First, find all the spans in <*macros> and point instead at their use site
307+
for sp in span.primary_spans() {
308+
if sp.is_dummy() {
309+
continue;
310+
}
311+
let call_sp = sm.call_span_if_macro(*sp);
312+
if call_sp != *sp && !always_backtrace {
313+
before_after.push((*sp, call_sp));
314+
}
315+
let backtrace_len = sp.macro_backtrace().len();
316+
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
317+
// Only show macro locations that are local
318+
// and display them like a span_note
319+
if trace.def_site_span.is_dummy() {
307320
continue;
308321
}
309-
let call_sp = sm.call_span_if_macro(*sp);
310-
if call_sp != *sp && !always_backtrace {
311-
before_after.push((*sp, call_sp));
322+
if always_backtrace {
323+
new_labels.push((trace.def_site_span,
324+
format!("in this expansion of `{}`{}",
325+
trace.macro_decl_name,
326+
if backtrace_len > 2 {
327+
// if backtrace_len == 1 it'll be pointed
328+
// at by "in this macro invocation"
329+
format!(" (#{})", i + 1)
330+
} else {
331+
String::new()
332+
})));
312333
}
313-
let backtrace_len = sp.macro_backtrace().len();
314-
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
315-
// Only show macro locations that are local
316-
// and display them like a span_note
317-
if trace.def_site_span.is_dummy() {
318-
continue;
319-
}
320-
if always_backtrace {
321-
new_labels.push((trace.def_site_span,
322-
format!("in this expansion of `{}`{}",
323-
trace.macro_decl_name,
324-
if backtrace_len > 2 {
325-
// if backtrace_len == 1 it'll be pointed
326-
// at by "in this macro invocation"
327-
format!(" (#{})", i + 1)
328-
} else {
329-
String::new()
330-
})));
331-
}
332-
// Check to make sure we're not in any <*macros>
333-
if !sm.span_to_filename(trace.def_site_span).is_macros() &&
334-
!trace.macro_decl_name.starts_with("desugaring of ") &&
335-
!trace.macro_decl_name.starts_with("#[") ||
336-
always_backtrace {
337-
new_labels.push((trace.call_site,
338-
format!("in this macro invocation{}",
339-
if backtrace_len > 2 && always_backtrace {
340-
// only specify order when the macro
341-
// backtrace is multiple levels deep
342-
format!(" (#{})", i + 1)
343-
} else {
344-
String::new()
345-
})));
346-
if !always_backtrace {
347-
break;
348-
}
334+
// Check to make sure we're not in any <*macros>
335+
if !sm.span_to_filename(trace.def_site_span).is_macros() &&
336+
!trace.macro_decl_name.starts_with("desugaring of ") &&
337+
!trace.macro_decl_name.starts_with("#[") ||
338+
always_backtrace {
339+
new_labels.push((trace.call_site,
340+
format!("in this macro invocation{}",
341+
if backtrace_len > 2 && always_backtrace {
342+
// only specify order when the macro
343+
// backtrace is multiple levels deep
344+
format!(" (#{})", i + 1)
345+
} else {
346+
String::new()
347+
})));
348+
if !always_backtrace {
349+
break;
349350
}
350351
}
351352
}
352-
for (label_span, label_text) in new_labels {
353-
span.push_span_label(label_span, label_text);
353+
}
354+
for (label_span, label_text) in new_labels {
355+
span.push_span_label(label_span, label_text);
356+
}
357+
for sp_label in span.span_labels() {
358+
if sp_label.span.is_dummy() {
359+
continue;
354360
}
355-
for sp_label in span.span_labels() {
356-
if sp_label.span.is_dummy() {
357-
continue;
358-
}
359-
if sm.span_to_filename(sp_label.span.clone()).is_macros() &&
360-
!always_backtrace
361-
{
362-
let v = sp_label.span.macro_backtrace();
363-
if let Some(use_site) = v.last() {
364-
before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
365-
}
361+
if sm.span_to_filename(sp_label.span.clone()).is_macros() &&
362+
!always_backtrace
363+
{
364+
let v = sp_label.span.macro_backtrace();
365+
if let Some(use_site) = v.last() {
366+
before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
366367
}
367368
}
368-
// After we have them, make sure we replace these 'bad' def sites with their use sites
369-
for (before, after) in before_after {
370-
span.replace(before, after);
371-
spans_updated = true;
372-
}
369+
}
370+
// After we have them, make sure we replace these 'bad' def sites with their use sites
371+
let spans_updated = !before_after.is_empty();
372+
for (before, after) in before_after {
373+
span.replace(before, after);
373374
}
374375

375376
spans_updated

0 commit comments

Comments
 (0)