Skip to content

Commit 7a807c5

Browse files
authored
Simplify EmitterWriter::emit_suggestion_default
Make function return early if source map is not present
1 parent 21724ed commit 7a807c5

File tree

1 file changed

+107
-104
lines changed

1 file changed

+107
-104
lines changed

src/librustc_errors/emitter.rs

+107-104
Original file line numberDiff line numberDiff line change
@@ -1431,122 +1431,125 @@ impl EmitterWriter {
14311431
level: &Level,
14321432
max_line_num_len: usize,
14331433
) -> io::Result<()> {
1434-
if let Some(ref sm) = self.sm {
1435-
let mut buffer = StyledBuffer::new();
1434+
let sm = match self.sm {
1435+
Some(ref sm) => sm,
1436+
None => return Ok(())
1437+
};
14361438

1437-
// Render the suggestion message
1438-
let level_str = level.to_string();
1439-
if !level_str.is_empty() {
1440-
buffer.append(0, &level_str, Style::Level(level.clone()));
1441-
buffer.append(0, ": ", Style::HeaderMsg);
1439+
let mut buffer = StyledBuffer::new();
1440+
1441+
// Render the suggestion message
1442+
let level_str = level.to_string();
1443+
if !level_str.is_empty() {
1444+
buffer.append(0, &level_str, Style::Level(level.clone()));
1445+
buffer.append(0, ": ", Style::HeaderMsg);
1446+
}
1447+
self.msg_to_buffer(
1448+
&mut buffer,
1449+
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1450+
max_line_num_len,
1451+
"suggestion",
1452+
Some(Style::HeaderMsg),
1453+
);
1454+
1455+
// Render the replacements for each suggestion
1456+
let suggestions = suggestion.splice_lines(&**sm);
1457+
1458+
let mut row_num = 2;
1459+
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) {
1460+
// Only show underline if the suggestion spans a single line and doesn't cover the
1461+
// entirety of the code output. If you have multiple replacements in the same line
1462+
// of code, show the underline.
1463+
let show_underline = !(parts.len() == 1
1464+
&& parts[0].snippet.trim() == complete.trim())
1465+
&& complete.lines().count() == 1;
1466+
1467+
let lines = sm.span_to_lines(parts[0].span).unwrap();
1468+
1469+
assert!(!lines.lines.is_empty());
1470+
1471+
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
1472+
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
1473+
let mut line_pos = 0;
1474+
let mut lines = complete.lines();
1475+
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
1476+
// Print the span column to avoid confusion
1477+
buffer.puts(row_num,
1478+
0,
1479+
&self.maybe_anonymized(line_start + line_pos),
1480+
Style::LineNumber);
1481+
// print the suggestion
1482+
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1483+
buffer.append(row_num, line, Style::NoStyle);
1484+
line_pos += 1;
1485+
row_num += 1;
14421486
}
1443-
self.msg_to_buffer(
1444-
&mut buffer,
1445-
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1446-
max_line_num_len,
1447-
"suggestion",
1448-
Some(Style::HeaderMsg),
1449-
);
1450-
1451-
// Render the replacements for each suggestion
1452-
let suggestions = suggestion.splice_lines(&**sm);
1453-
1454-
let mut row_num = 2;
1455-
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) {
1456-
// Only show underline if the suggestion spans a single line and doesn't cover the
1457-
// entirety of the code output. If you have multiple replacements in the same line
1458-
// of code, show the underline.
1459-
let show_underline = !(parts.len() == 1
1460-
&& parts[0].snippet.trim() == complete.trim())
1461-
&& complete.lines().count() == 1;
1462-
1463-
let lines = sm.span_to_lines(parts[0].span).unwrap();
1464-
1465-
assert!(!lines.lines.is_empty());
1466-
1467-
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
1468-
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
1469-
let mut line_pos = 0;
1470-
let mut lines = complete.lines();
1471-
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
1472-
// Print the span column to avoid confusion
1473-
buffer.puts(row_num,
1474-
0,
1475-
&self.maybe_anonymized(line_start + line_pos),
1476-
Style::LineNumber);
1477-
// print the suggestion
1478-
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1479-
buffer.append(row_num, line, Style::NoStyle);
1480-
line_pos += 1;
1481-
row_num += 1;
1482-
}
14831487

1484-
// This offset and the ones below need to be signed to account for replacement code
1485-
// that is shorter than the original code.
1486-
let mut offset: isize = 0;
1487-
// Only show an underline in the suggestions if the suggestion is not the
1488-
// entirety of the code being shown and the displayed code is not multiline.
1489-
if show_underline {
1490-
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1491-
for part in parts {
1492-
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
1493-
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
1494-
1495-
// Do not underline the leading...
1496-
let start = part.snippet.len()
1497-
.saturating_sub(part.snippet.trim_start().len());
1498-
// ...or trailing spaces. Account for substitutions containing unicode
1499-
// characters.
1500-
let sub_len = part.snippet.trim().chars()
1501-
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1502-
.sum();
1503-
1504-
let underline_start = (span_start_pos + start) as isize + offset;
1505-
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
1506-
for p in underline_start..underline_end {
1488+
// This offset and the ones below need to be signed to account for replacement code
1489+
// that is shorter than the original code.
1490+
let mut offset: isize = 0;
1491+
// Only show an underline in the suggestions if the suggestion is not the
1492+
// entirety of the code being shown and the displayed code is not multiline.
1493+
if show_underline {
1494+
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1495+
for part in parts {
1496+
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
1497+
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
1498+
1499+
// Do not underline the leading...
1500+
let start = part.snippet.len()
1501+
.saturating_sub(part.snippet.trim_start().len());
1502+
// ...or trailing spaces. Account for substitutions containing unicode
1503+
// characters.
1504+
let sub_len = part.snippet.trim().chars()
1505+
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1506+
.sum();
1507+
1508+
let underline_start = (span_start_pos + start) as isize + offset;
1509+
let underline_end = (span_start_pos + start + sub_len) as isize + offset;
1510+
for p in underline_start..underline_end {
1511+
buffer.putc(row_num,
1512+
max_line_num_len + 3 + p as usize,
1513+
'^',
1514+
Style::UnderlinePrimary);
1515+
}
1516+
// underline removals too
1517+
if underline_start == underline_end {
1518+
for p in underline_start-1..underline_start+1 {
15071519
buffer.putc(row_num,
15081520
max_line_num_len + 3 + p as usize,
1509-
'^',
1510-
Style::UnderlinePrimary);
1511-
}
1512-
// underline removals too
1513-
if underline_start == underline_end {
1514-
for p in underline_start-1..underline_start+1 {
1515-
buffer.putc(row_num,
1516-
max_line_num_len + 3 + p as usize,
1517-
'-',
1518-
Style::UnderlineSecondary);
1519-
}
1521+
'-',
1522+
Style::UnderlineSecondary);
15201523
}
1521-
1522-
// length of the code after substitution
1523-
let full_sub_len = part.snippet.chars()
1524-
.map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1525-
.sum() as isize;
1526-
1527-
// length of the code to be substituted
1528-
let snippet_len = span_end_pos as isize - span_start_pos as isize;
1529-
// For multiple substitutions, use the position *after* the previous
1530-
// substitutions have happened.
1531-
offset += full_sub_len - snippet_len;
15321524
}
1533-
row_num += 1;
1534-
}
15351525

1536-
// if we elided some lines, add an ellipsis
1537-
if lines.next().is_some() {
1538-
buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
1539-
} else if !show_underline {
1540-
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
1541-
row_num += 1;
1526+
// length of the code after substitution
1527+
let full_sub_len = part.snippet.chars()
1528+
.map(|ch| acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
1529+
.sum() as isize;
1530+
1531+
// length of the code to be substituted
1532+
let snippet_len = span_end_pos as isize - span_start_pos as isize;
1533+
// For multiple substitutions, use the position *after* the previous
1534+
// substitutions have happened.
1535+
offset += full_sub_len - snippet_len;
15421536
}
1537+
row_num += 1;
15431538
}
1544-
if suggestions.len() > MAX_SUGGESTIONS {
1545-
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
1546-
buffer.puts(row_num, 0, &msg, Style::NoStyle);
1539+
1540+
// if we elided some lines, add an ellipsis
1541+
if lines.next().is_some() {
1542+
buffer.puts(row_num, max_line_num_len - 1, "...", Style::LineNumber);
1543+
} else if !show_underline {
1544+
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
1545+
row_num += 1;
15471546
}
1548-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
15491547
}
1548+
if suggestions.len() > MAX_SUGGESTIONS {
1549+
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
1550+
buffer.puts(row_num, 0, &msg, Style::NoStyle);
1551+
}
1552+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
15501553
Ok(())
15511554
}
15521555

0 commit comments

Comments
 (0)