Skip to content

Commit 9eb9176

Browse files
committed
Simplify span fallback
1 parent b9bf119 commit 9eb9176

File tree

1 file changed

+23
-47
lines changed

1 file changed

+23
-47
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_lint_defs::pluralize;
2424

2525
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
2626
use rustc_data_structures::sync::Lrc;
27-
use rustc_error_messages::FluentArgs;
27+
use rustc_error_messages::{FluentArgs, SpanLabel};
2828
use rustc_span::hygiene::{ExpnKind, MacroKind};
2929
use std::borrow::Cow;
3030
use std::cmp::{max, min, Reverse};
@@ -2202,46 +2202,28 @@ impl FileWithAnnotatedLines {
22022202
let mut multiline_annotations = vec![];
22032203

22042204
if let Some(ref sm) = emitter.source_map() {
2205-
for span_label in msp.span_labels() {
2206-
let fixup_lo_hi = |span: Span| {
2207-
let lo = sm.lookup_char_pos(span.lo());
2208-
let mut hi = sm.lookup_char_pos(span.hi());
2209-
2210-
// Watch out for "empty spans". If we get a span like 6..6, we
2211-
// want to just display a `^` at 6, so convert that to
2212-
// 6..7. This is degenerate input, but it's best to degrade
2213-
// gracefully -- and the parser likes to supply a span like
2214-
// that for EOF, in particular.
2215-
2216-
if lo.col_display == hi.col_display && lo.line == hi.line {
2217-
hi.col_display += 1;
2218-
}
2219-
(lo, hi)
2205+
for SpanLabel { span, is_primary, label } in msp.span_labels() {
2206+
// If we don't have a useful span, pick the primary span if that exists.
2207+
// Worst case we'll just print an error at the top of the main file.
2208+
let span = match (span.is_dummy(), msp.primary_span()) {
2209+
(_, None) | (false, _) => span,
2210+
(true, Some(span)) => span,
22202211
};
22212212

2222-
if span_label.span.is_dummy() {
2223-
if let Some(span) = msp.primary_span() {
2224-
// if we don't know where to render the annotation, emit it as a note
2225-
// on the primary span.
2226-
2227-
let (lo, hi) = fixup_lo_hi(span);
2228-
2229-
let ann = Annotation {
2230-
start_col: lo.col_display,
2231-
end_col: hi.col_display,
2232-
is_primary: span_label.is_primary,
2233-
label: span_label
2234-
.label
2235-
.as_ref()
2236-
.map(|m| emitter.translate_message(m, args).to_string()),
2237-
annotation_type: AnnotationType::Singleline,
2238-
};
2239-
add_annotation_to_file(&mut output, lo.file, lo.line, ann);
2240-
}
2241-
continue;
2213+
let lo = sm.lookup_char_pos(span.lo());
2214+
let mut hi = sm.lookup_char_pos(span.hi());
2215+
2216+
// Watch out for "empty spans". If we get a span like 6..6, we
2217+
// want to just display a `^` at 6, so convert that to
2218+
// 6..7. This is degenerate input, but it's best to degrade
2219+
// gracefully -- and the parser likes to supply a span like
2220+
// that for EOF, in particular.
2221+
2222+
if lo.col_display == hi.col_display && lo.line == hi.line {
2223+
hi.col_display += 1;
22422224
}
22432225

2244-
let (lo, hi) = fixup_lo_hi(span_label.span);
2226+
let label = label.as_ref().map(|m| emitter.translate_message(m, args).to_string());
22452227

22462228
if lo.line != hi.line {
22472229
let ml = MultilineAnnotation {
@@ -2250,23 +2232,17 @@ impl FileWithAnnotatedLines {
22502232
line_end: hi.line,
22512233
start_col: lo.col_display,
22522234
end_col: hi.col_display,
2253-
is_primary: span_label.is_primary,
2254-
label: span_label
2255-
.label
2256-
.as_ref()
2257-
.map(|m| emitter.translate_message(m, args).to_string()),
2235+
is_primary,
2236+
label,
22582237
overlaps_exactly: false,
22592238
};
22602239
multiline_annotations.push((lo.file, ml));
22612240
} else {
22622241
let ann = Annotation {
22632242
start_col: lo.col_display,
22642243
end_col: hi.col_display,
2265-
is_primary: span_label.is_primary,
2266-
label: span_label
2267-
.label
2268-
.as_ref()
2269-
.map(|m| emitter.translate_message(m, args).to_string()),
2244+
is_primary,
2245+
label,
22702246
annotation_type: AnnotationType::Singleline,
22712247
};
22722248
add_annotation_to_file(&mut output, lo.file, lo.line, ann);

0 commit comments

Comments
 (0)