Skip to content

Commit d9c8f15

Browse files
committed
Address review comments
1 parent eb55252 commit d9c8f15

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

compiler/rustc_errors/src/emitter.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_error_messages::{FluentArgs, SpanLabel};
2222
use rustc_lint_defs::pluralize;
2323
use rustc_span::hygiene::{ExpnKind, MacroKind};
2424
use rustc_span::source_map::SourceMap;
25-
use rustc_span::{FileLines, FileName, SourceFile, Span, char_width};
25+
use rustc_span::{FileLines, FileName, SourceFile, Span, char_width, str_width};
2626
use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
2727
use tracing::{debug, instrument, trace, warn};
2828

@@ -113,8 +113,12 @@ impl Margin {
113113
fn was_cut_right(&self, line_len: usize) -> bool {
114114
let right =
115115
if self.computed_right == self.span_right || self.computed_right == self.label_right {
116+
// FIXME: This comment refers to the only callsite of this method.
117+
// Rephrase it or refactor it, so it can stand on its own.
116118
// Account for the "..." padding given above. Otherwise we end up with code lines
117119
// that do fit but end in "..." as if they were trimmed.
120+
// FIXME: Don't hard-code this offset. Is this meant to represent
121+
// `2 * str_width(self.margin())`?
118122
self.computed_right - 6
119123
} else {
120124
self.computed_right
@@ -673,6 +677,7 @@ impl HumanEmitter {
673677
// Create the source line we will highlight.
674678
let left = margin.left(line_len);
675679
let right = margin.right(line_len);
680+
// FIXME: The following code looks fishy. See #132860.
676681
// On long lines, we strip the source line, accounting for unicode.
677682
let mut taken = 0;
678683
let code: String = source_string
@@ -695,7 +700,7 @@ impl HumanEmitter {
695700
buffer.puts(line_offset, code_offset, placeholder, Style::LineNumber);
696701
}
697702
if margin.was_cut_right(line_len) {
698-
let padding: usize = placeholder.chars().map(|ch| char_width(ch)).sum();
703+
let padding = str_width(placeholder);
699704
// We have stripped some code after the rightmost span end, make it clear we did so.
700705
buffer.puts(line_offset, code_offset + taken - padding, placeholder, Style::LineNumber);
701706
}
@@ -744,6 +749,7 @@ impl HumanEmitter {
744749
// Left trim
745750
let left = margin.left(source_string.len());
746751

752+
// FIXME: This looks fishy. See #132860.
747753
// Account for unicode characters of width !=0 that were removed.
748754
let left = source_string.chars().take(left).map(|ch| char_width(ch)).sum();
749755

@@ -1068,21 +1074,15 @@ impl HumanEmitter {
10681074

10691075
if pos > 1 && (annotation.has_label() || annotation.takes_space()) {
10701076
for p in line_offset + 1..=line_offset + pos {
1071-
if let AnnotationType::MultilineLine(_) = annotation.annotation_type {
1072-
buffer.putc(
1073-
p,
1074-
(code_offset + annotation.start_col.display).saturating_sub(left),
1075-
underline.multiline_vertical,
1076-
underline.style,
1077-
);
1078-
} else {
1079-
buffer.putc(
1080-
p,
1081-
(code_offset + annotation.start_col.display).saturating_sub(left),
1082-
underline.vertical_text_line,
1083-
underline.style,
1084-
);
1085-
}
1077+
buffer.putc(
1078+
p,
1079+
(code_offset + annotation.start_col.display).saturating_sub(left),
1080+
match annotation.annotation_style {
1081+
AnnotationType::MultilineLine(_) => underline.multiline_vertical,
1082+
_ => underline.vertical_text_line,
1083+
},
1084+
underline.style,
1085+
);
10861086
}
10871087
if let AnnotationType::MultilineStart(_) = annotation.annotation_type {
10881088
buffer.putc(
@@ -2134,7 +2134,7 @@ impl HumanEmitter {
21342134
}
21352135

21362136
let placeholder = self.margin();
2137-
let padding: usize = placeholder.chars().map(|ch| char_width(ch)).sum();
2137+
let padding = str_width(placeholder);
21382138
buffer.puts(
21392139
row_num,
21402140
max_line_num_len.saturating_sub(padding),
@@ -2224,11 +2224,11 @@ impl HumanEmitter {
22242224
};
22252225
// ...or trailing spaces. Account for substitutions containing unicode
22262226
// characters.
2227-
let sub_len: usize =
2228-
if is_whitespace_addition { &part.snippet } else { part.snippet.trim() }
2229-
.chars()
2230-
.map(|ch| char_width(ch))
2231-
.sum();
2227+
let sub_len: usize = str_width(if is_whitespace_addition {
2228+
&part.snippet
2229+
} else {
2230+
part.snippet.trim()
2231+
});
22322232

22332233
let offset: isize = offsets
22342234
.iter()
@@ -2266,8 +2266,7 @@ impl HumanEmitter {
22662266
}
22672267

22682268
// length of the code after substitution
2269-
let full_sub_len =
2270-
part.snippet.chars().map(|ch| char_width(ch)).sum::<usize>() as isize;
2269+
let full_sub_len = str_width(&part.snippet) as isize;
22712270

22722271
// length of the code to be substituted
22732272
let snippet_len = span_end_pos as isize - span_start_pos as isize;
@@ -2282,7 +2281,7 @@ impl HumanEmitter {
22822281
// if we elided some lines, add an ellipsis
22832282
if lines.next().is_some() {
22842283
let placeholder = self.margin();
2285-
let padding: usize = placeholder.chars().map(|ch| char_width(ch)).sum();
2284+
let padding = str_width(placeholder);
22862285
buffer.puts(
22872286
row_num,
22882287
max_line_num_len.saturating_sub(padding),

compiler/rustc_session/src/config.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1772,8 +1772,8 @@ pub fn parse_error_format(
17721772
color,
17731773
));
17741774
early_dcx.early_fatal(format!(
1775-
"argument for `--error-format` must be `human`, `json` or \
1776-
`short` (instead was `{arg}`)"
1775+
"argument for `--error-format` must be `human`, `human-annotate-rs`, \
1776+
`human-unicode`, `json`, `pretty-json` or `short` (instead was `{arg}`)"
17771777
))
17781778
}
17791779
}
@@ -1826,21 +1826,21 @@ pub fn parse_crate_edition(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches
18261826
fn check_error_format_stability(
18271827
early_dcx: &EarlyDiagCtxt,
18281828
unstable_opts: &UnstableOptions,
1829-
error_format: ErrorOutputType,
1829+
format: ErrorOutputType,
18301830
) {
1831-
if !unstable_opts.unstable_options {
1832-
if let ErrorOutputType::Json { pretty: true, .. } = error_format {
1833-
early_dcx.early_fatal("`--error-format=pretty-json` is unstable");
1834-
}
1835-
if let ErrorOutputType::HumanReadable(HumanReadableErrorType::AnnotateSnippet, _) =
1836-
error_format
1837-
{
1838-
early_dcx.early_fatal("`--error-format=human-annotate-rs` is unstable");
1839-
}
1840-
if let ErrorOutputType::HumanReadable(HumanReadableErrorType::Unicode, _) = error_format {
1841-
early_dcx.early_fatal("`--error-format=human-unicode` is unstable");
1842-
}
1843-
}
1831+
if unstable_opts.unstable_options {
1832+
return;
1833+
}
1834+
let format = match format {
1835+
ErrorOutputType::Json { pretty: true, .. } => "pretty-json",
1836+
ErrorOutputType::HumanReadable(format, _) => match format {
1837+
HumanReadableErrorType::AnnotateSnippet => "human-annotate-rs",
1838+
HumanReadableErrorType::Unicode => "human-unicode",
1839+
_ => return,
1840+
},
1841+
_ => return,
1842+
};
1843+
early_dcx.early_fatal(format!("`--error-format={format}` is unstable"))
18441844
}
18451845

18461846
fn parse_output_types(

compiler/rustc_span/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,10 @@ pub fn char_width(ch: char) -> usize {
22232223
}
22242224
}
22252225

2226+
pub fn str_width(s: &str) -> usize {
2227+
s.chars().map(char_width).sum()
2228+
}
2229+
22262230
/// Normalizes the source code and records the normalizations.
22272231
fn normalize_src(src: &mut String) -> Vec<NormalizedPos> {
22282232
let mut normalized_pos = vec![];

0 commit comments

Comments
 (0)