Skip to content

Commit 2b6cc53

Browse files
committed
Rollup merge of rust-lang#48449 - petrochenkov:uidiff, r=nikomatsakis
Anonymize some line numbers in UI test output New unstable flag `-Z ui-testing` is introduced. This flag changes diagnostic output of the compiler *in some way* making it more suitable for UI testing (this is intentionally vague). At the moment this flag anonymizes line numbers at line starts thus solving the largest issue with UI test diffs. If diffs continue to be too noisy, some other tweaks could be applied (e.g. anonymizing lines/columns in `--> $DIR/file.rs:line:column`), but this needs some time and experience (we shouldn't diverge too much from the actual output in general). If comment `// disable-ui-testing-normalization` is added to an UI test, then `-Z ui-testing` is not passed. Closes rust-lang#46643
2 parents 657a643 + cd7ce71 commit 2b6cc53

File tree

1,223 files changed

+6823
-6654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,223 files changed

+6823
-6654
lines changed

Diff for: src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13221322
epoch). Crates compiled with different epochs can be linked together."),
13231323
run_dsymutil: Option<bool> = (None, parse_opt_bool, [TRACKED],
13241324
"run `dsymutil` and delete intermediate object files"),
1325+
ui_testing: bool = (false, parse_bool, [UNTRACKED],
1326+
"format compiler diagnostics in a way that's better suitable for UI testing"),
13251327
}
13261328

13271329
pub fn default_lib_output() -> CrateType {

Diff for: src/librustc/session/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,13 @@ pub fn build_session_with_codemap(sopts: config::Options,
919919
}
920920
(config::ErrorOutputType::Json(pretty), None) => {
921921
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(),
922-
pretty, sopts.debugging_opts.approximate_suggestions))
922+
pretty, sopts.debugging_opts.approximate_suggestions)
923+
.ui_testing(sopts.debugging_opts.ui_testing))
923924
}
924925
(config::ErrorOutputType::Json(pretty), Some(dst)) => {
925926
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(),
926-
pretty, sopts.debugging_opts.approximate_suggestions))
927+
pretty, sopts.debugging_opts.approximate_suggestions)
928+
.ui_testing(sopts.debugging_opts.ui_testing))
927929
}
928930
(config::ErrorOutputType::Short(color_config), None) => {
929931
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false))

Diff for: src/librustc_errors/emitter.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use std::collections::HashMap;
2525
use std::cmp::min;
2626
use unicode_width;
2727

28+
const ANONYMIZED_LINE_NUM: &str = "LL";
29+
2830
/// Emitter trait for emitting errors.
2931
pub trait Emitter {
3032
/// Emit a structured diagnostic.
@@ -107,6 +109,7 @@ pub struct EmitterWriter {
107109
cm: Option<Rc<CodeMapper>>,
108110
short_message: bool,
109111
teach: bool,
112+
ui_testing: bool,
110113
}
111114

112115
struct FileWithAnnotatedLines {
@@ -128,13 +131,15 @@ impl EmitterWriter {
128131
cm: code_map,
129132
short_message,
130133
teach,
134+
ui_testing: false,
131135
}
132136
} else {
133137
EmitterWriter {
134138
dst: Raw(Box::new(io::stderr())),
135139
cm: code_map,
136140
short_message,
137141
teach,
142+
ui_testing: false,
138143
}
139144
}
140145
}
@@ -149,6 +154,19 @@ impl EmitterWriter {
149154
cm: code_map,
150155
short_message,
151156
teach,
157+
ui_testing: false,
158+
}
159+
}
160+
161+
pub fn ui_testing(self, ui_testing: bool) -> Self {
162+
Self { ui_testing, ..self }
163+
}
164+
165+
fn maybe_anonymized(&self, line_num: usize) -> String {
166+
if self.ui_testing {
167+
ANONYMIZED_LINE_NUM.to_string()
168+
} else {
169+
line_num.to_string()
152170
}
153171
}
154172

@@ -305,7 +323,7 @@ impl EmitterWriter {
305323
buffer.puts(line_offset, code_offset, &source_string, Style::Quotation);
306324
buffer.puts(line_offset,
307325
0,
308-
&(line.line_index.to_string()),
326+
&self.maybe_anonymized(line.line_index),
309327
Style::LineNumber);
310328

311329
draw_col_separator(buffer, line_offset, width_offset - 2);
@@ -1126,8 +1144,8 @@ impl EmitterWriter {
11261144

11271145
buffer.puts(last_buffer_line_num,
11281146
0,
1129-
&(annotated_file.lines[line_idx + 1].line_index - 1)
1130-
.to_string(),
1147+
&self.maybe_anonymized(annotated_file.lines[line_idx + 1]
1148+
.line_index - 1),
11311149
Style::LineNumber);
11321150
draw_col_separator(&mut buffer,
11331151
last_buffer_line_num,
@@ -1201,7 +1219,7 @@ impl EmitterWriter {
12011219
// Print the span column to avoid confusion
12021220
buffer.puts(row_num,
12031221
0,
1204-
&((line_start + line_pos).to_string()),
1222+
&self.maybe_anonymized(line_start + line_pos),
12051223
Style::LineNumber);
12061224
// print the suggestion
12071225
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
@@ -1253,8 +1271,11 @@ impl EmitterWriter {
12531271
span: &MultiSpan,
12541272
children: &Vec<SubDiagnostic>,
12551273
suggestions: &[CodeSuggestion]) {
1256-
let max_line_num = self.get_max_line_num(span, children);
1257-
let max_line_num_len = max_line_num.to_string().len();
1274+
let max_line_num_len = if self.ui_testing {
1275+
ANONYMIZED_LINE_NUM.len()
1276+
} else {
1277+
self.get_max_line_num(span, children).to_string().len()
1278+
};
12581279

12591280
match self.emit_message_default(span,
12601281
message,

Diff for: src/libsyntax/json.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct JsonEmitter {
4040
pretty: bool,
4141
/// Whether "approximate suggestions" are enabled in the config
4242
approximate_suggestions: bool,
43+
ui_testing: bool,
4344
}
4445

4546
impl JsonEmitter {
@@ -53,6 +54,7 @@ impl JsonEmitter {
5354
cm: code_map,
5455
pretty,
5556
approximate_suggestions,
57+
ui_testing: false,
5658
}
5759
}
5860

@@ -73,8 +75,13 @@ impl JsonEmitter {
7375
cm: code_map,
7476
pretty,
7577
approximate_suggestions,
78+
ui_testing: false,
7679
}
7780
}
81+
82+
pub fn ui_testing(self, ui_testing: bool) -> Self {
83+
Self { ui_testing, ..self }
84+
}
7885
}
7986

8087
impl Emitter for JsonEmitter {
@@ -199,7 +206,8 @@ impl Diagnostic {
199206
}
200207
let buf = BufWriter::default();
201208
let output = buf.clone();
202-
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false).emit(db);
209+
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false)
210+
.ui_testing(je.ui_testing).emit(db);
203211
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
204212
let output = String::from_utf8(output).unwrap();
205213

Diff for: src/test/ui-fulldeps/custom-derive/issue-36935.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: proc-macro derive panicked
22
--> $DIR/issue-36935.rs:18:15
33
|
4-
18 | #[derive(Foo, Bar)] //~ ERROR proc-macro derive panicked
4+
LL | #[derive(Foo, Bar)] //~ ERROR proc-macro derive panicked
55
| ^^^
66
|
77
= help: message: lolnope

Diff for: src/test/ui-fulldeps/deprecated-derive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
warning: derive(Encodable) is deprecated in favor of derive(RustcEncodable)
22
--> $DIR/deprecated-derive.rs:18:10
33
|
4-
18 | #[derive(Encodable)]
4+
LL | #[derive(Encodable)]
55
| ^^^^^^^^^
66

Diff for: src/test/ui-fulldeps/lint-group-plugin.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
warning: item is named 'lintme'
22
--> $DIR/lint-group-plugin.rs:18:1
33
|
4-
18 | fn lintme() { } //~ WARNING item is named 'lintme'
4+
LL | fn lintme() { } //~ WARNING item is named 'lintme'
55
| ^^^^^^^^^^^^^^^
66
|
77
= note: #[warn(test_lint)] on by default
88

99
warning: item is named 'pleaselintme'
1010
--> $DIR/lint-group-plugin.rs:19:1
1111
|
12-
19 | fn pleaselintme() { } //~ WARNING item is named 'pleaselintme'
12+
LL | fn pleaselintme() { } //~ WARNING item is named 'pleaselintme'
1313
| ^^^^^^^^^^^^^^^^^^^^^
1414
|
1515
= note: #[warn(please_lint)] on by default
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
warning: function is never used: `lintme`
22
--> $DIR/lint-plugin-cmdline-allow.rs:20:1
33
|
4-
20 | fn lintme() { }
4+
LL | fn lintme() { }
55
| ^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-plugin-cmdline-allow.rs:17:9
99
|
10-
17 | #![warn(unused)]
10+
LL | #![warn(unused)]
1111
| ^^^^^^
1212
= note: #[warn(dead_code)] implied by #[warn(unused)]
1313

Diff for: src/test/ui-fulldeps/lint-plugin-cmdline-load.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: item is named 'lintme'
22
--> $DIR/lint-plugin-cmdline-load.rs:18:1
33
|
4-
18 | fn lintme() { } //~ WARNING item is named 'lintme'
4+
LL | fn lintme() { } //~ WARNING item is named 'lintme'
55
| ^^^^^^^^^^^^^^^
66
|
77
= note: #[warn(test_lint)] on by default

Diff for: src/test/ui-fulldeps/lint-plugin-forbid-attrs.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
error: item is named 'lintme'
22
--> $DIR/lint-plugin-forbid-attrs.rs:18:1
33
|
4-
18 | fn lintme() { } //~ ERROR item is named 'lintme'
4+
LL | fn lintme() { } //~ ERROR item is named 'lintme'
55
| ^^^^^^^^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/lint-plugin-forbid-attrs.rs:16:11
99
|
10-
16 | #![forbid(test_lint)]
10+
LL | #![forbid(test_lint)]
1111
| ^^^^^^^^^
1212

1313
error[E0453]: allow(test_lint) overruled by outer forbid(test_lint)
1414
--> $DIR/lint-plugin-forbid-attrs.rs:20:9
1515
|
16-
16 | #![forbid(test_lint)]
16+
LL | #![forbid(test_lint)]
1717
| --------- `forbid` level set here
1818
...
19-
20 | #[allow(test_lint)]
19+
LL | #[allow(test_lint)]
2020
| ^^^^^^^^^ overruled by previous forbid
2121

2222
error: aborting due to 2 previous errors

Diff for: src/test/ui-fulldeps/lint-plugin.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
warning: item is named 'lintme'
22
--> $DIR/lint-plugin.rs:18:1
33
|
4-
18 | fn lintme() { } //~ WARNING item is named 'lintme'
4+
LL | fn lintme() { } //~ WARNING item is named 'lintme'
55
| ^^^^^^^^^^^^^^^
66
|
77
= note: #[warn(test_lint)] on by default

0 commit comments

Comments
 (0)