Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small cleanups in rustc_errors / emitter #83427

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,34 +434,31 @@ pub trait Emitter {
span: &mut MultiSpan,
children: &mut Vec<SubDiagnostic>,
) {
let source_map = if let Some(ref sm) = source_map {
sm
} else {
return;
};
debug!("fix_multispans_in_extern_macros: before: span={:?} children={:?}", span, children);
for span in iter::once(&mut *span).chain(children.iter_mut().map(|child| &mut child.span)) {
self.fix_multispan_in_extern_macros(source_map, span);
self.fix_multispan_in_extern_macros(source_map, span);
for child in children.iter_mut() {
self.fix_multispan_in_extern_macros(source_map, &mut child.span);
}
debug!("fix_multispans_in_extern_macros: after: span={:?} children={:?}", span, children);
}

// This "fixes" MultiSpans that contain `Span`s pointing to locations inside of external macros.
// Since these locations are often difficult to read,
// we move these spans from the external macros to their corresponding use site.
fn fix_multispan_in_extern_macros(
&self,
source_map: &Option<Lrc<SourceMap>>,
span: &mut MultiSpan,
) {
let sm = match source_map {
Some(ref sm) => sm,
None => return,
};

fn fix_multispan_in_extern_macros(&self, source_map: &Lrc<SourceMap>, span: &mut MultiSpan) {
// First, find all the spans in external macros and point instead at their use site.
let replacements: Vec<(Span, Span)> = span
.primary_spans()
.iter()
.copied()
.chain(span.span_labels().iter().map(|sp_label| sp_label.span))
.filter_map(|sp| {
if !sp.is_dummy() && sm.is_imported(sp) {
if !sp.is_dummy() && source_map.is_imported(sp) {
let maybe_callsite = sp.source_callsite();
if sp != maybe_callsite {
return Some((sp, maybe_callsite));
Expand Down Expand Up @@ -1232,7 +1229,6 @@ impl EmitterWriter {
is_secondary: bool,
) -> io::Result<()> {
let mut buffer = StyledBuffer::new();
let header_style = if is_secondary { Style::HeaderMsg } else { Style::MainHeaderMsg };

if !msp.has_primary_spans() && !msp.has_span_labels() && is_secondary && !self.short_message
{
Expand All @@ -1257,6 +1253,7 @@ impl EmitterWriter {
buffer.append(0, &code, Style::Level(*level));
buffer.append(0, "]", Style::Level(*level));
}
let header_style = if is_secondary { Style::HeaderMsg } else { Style::MainHeaderMsg };
if *level != Level::FailureNote {
buffer.append(0, ": ", header_style);
}
Expand Down Expand Up @@ -1470,9 +1467,7 @@ impl EmitterWriter {
let mut to_add = FxHashMap::default();

for (depth, style) in depths {
if multilines.get(&depth).is_some() {
multilines.remove(&depth);
Comment on lines -1473 to -1474
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait for CI to complete because I'm surprised that changing these two lines don't affect the output (I'm guessing it was dead code).

Otherwise, r=me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If depth was Some(_), it would have gotten removed in the then branch, but remove also returns the previous value, which should explain why there is no difference. Note that remove returns None if the value wasn't there previously.

} else {
if multilines.remove(&depth).is_none() {
to_add.insert(depth, style);
}
}
Expand Down Expand Up @@ -1726,14 +1721,13 @@ impl EmitterWriter {
if !self.short_message {
draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
}
match emit_to_destination(
if let Err(e) = emit_to_destination(
&buffer.render(),
level,
&mut self.dst,
self.short_message,
) {
Ok(()) => (),
Err(e) => panic!("failed to emit error: {}", e),
panic!("failed to emit error: {}", e)
}
}
if !self.short_message {
Expand Down