Skip to content

Commit 29f5c69

Browse files
committed
Auto 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 bedbad6 + 9f9183d commit 29f5c69

File tree

1,230 files changed

+6878
-6708
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,230 files changed

+6878
-6708
lines changed

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 {

src/librustc/session/mod.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -909,21 +909,24 @@ pub fn build_session_with_codemap(sopts: config::Options,
909909

910910
let emitter: Box<Emitter> = match (sopts.error_format, emitter_dest) {
911911
(config::ErrorOutputType::HumanReadable(color_config), None) => {
912-
Box::new(EmitterWriter::stderr(color_config,
913-
Some(codemap.clone()),
914-
false,
915-
sopts.debugging_opts.teach))
912+
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()),
913+
false, sopts.debugging_opts.teach)
914+
.ui_testing(sopts.debugging_opts.ui_testing))
916915
}
917916
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
918-
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false, false))
917+
Box::new(EmitterWriter::new(dst, Some(codemap.clone()),
918+
false, false)
919+
.ui_testing(sopts.debugging_opts.ui_testing))
919920
}
920921
(config::ErrorOutputType::Json(pretty), None) => {
921922
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(),
922-
pretty, sopts.debugging_opts.approximate_suggestions))
923+
pretty, sopts.debugging_opts.approximate_suggestions)
924+
.ui_testing(sopts.debugging_opts.ui_testing))
923925
}
924926
(config::ErrorOutputType::Json(pretty), Some(dst)) => {
925927
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(),
926-
pretty, sopts.debugging_opts.approximate_suggestions))
928+
pretty, sopts.debugging_opts.approximate_suggestions)
929+
.ui_testing(sopts.debugging_opts.ui_testing))
927930
}
928931
(config::ErrorOutputType::Short(color_config), None) => {
929932
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false))

src/librustc_errors/emitter.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use std::collections::{HashMap, HashSet};
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.
@@ -108,6 +110,7 @@ pub struct EmitterWriter {
108110
short_message: bool,
109111
teach: bool,
110112
error_codes: HashSet<String>,
113+
ui_testing: bool,
111114
}
112115

113116
struct FileWithAnnotatedLines {
@@ -157,6 +160,7 @@ impl EmitterWriter {
157160
short_message,
158161
teach,
159162
error_codes: HashSet::new(),
163+
ui_testing: false,
160164
}
161165
} else {
162166
EmitterWriter {
@@ -165,6 +169,7 @@ impl EmitterWriter {
165169
short_message,
166170
teach,
167171
error_codes: HashSet::new(),
172+
ui_testing: false,
168173
}
169174
}
170175
}
@@ -180,6 +185,20 @@ impl EmitterWriter {
180185
short_message,
181186
teach,
182187
error_codes: HashSet::new(),
188+
ui_testing: false,
189+
}
190+
}
191+
192+
pub fn ui_testing(mut self, ui_testing: bool) -> Self {
193+
self.ui_testing = ui_testing;
194+
self
195+
}
196+
197+
fn maybe_anonymized(&self, line_num: usize) -> String {
198+
if self.ui_testing {
199+
ANONYMIZED_LINE_NUM.to_string()
200+
} else {
201+
line_num.to_string()
183202
}
184203
}
185204

@@ -336,7 +355,7 @@ impl EmitterWriter {
336355
buffer.puts(line_offset, code_offset, &source_string, Style::Quotation);
337356
buffer.puts(line_offset,
338357
0,
339-
&(line.line_index.to_string()),
358+
&self.maybe_anonymized(line.line_index),
340359
Style::LineNumber);
341360

342361
draw_col_separator(buffer, line_offset, width_offset - 2);
@@ -1159,8 +1178,8 @@ impl EmitterWriter {
11591178

11601179
buffer.puts(last_buffer_line_num,
11611180
0,
1162-
&(annotated_file.lines[line_idx + 1].line_index - 1)
1163-
.to_string(),
1181+
&self.maybe_anonymized(annotated_file.lines[line_idx + 1]
1182+
.line_index - 1),
11641183
Style::LineNumber);
11651184
draw_col_separator(&mut buffer,
11661185
last_buffer_line_num,
@@ -1235,7 +1254,7 @@ impl EmitterWriter {
12351254
// Print the span column to avoid confusion
12361255
buffer.puts(row_num,
12371256
0,
1238-
&((line_start + line_pos).to_string()),
1257+
&self.maybe_anonymized(line_start + line_pos),
12391258
Style::LineNumber);
12401259
// print the suggestion
12411260
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
@@ -1288,8 +1307,11 @@ impl EmitterWriter {
12881307
span: &MultiSpan,
12891308
children: &Vec<SubDiagnostic>,
12901309
suggestions: &[CodeSuggestion]) {
1291-
let max_line_num = self.get_max_line_num(span, children);
1292-
let max_line_num_len = max_line_num.to_string().len();
1310+
let max_line_num_len = if self.ui_testing {
1311+
ANONYMIZED_LINE_NUM.len()
1312+
} else {
1313+
self.get_max_line_num(span, children).to_string().len()
1314+
};
12931315

12941316
match self.emit_message_default(span,
12951317
message,

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

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
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

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
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

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

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

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

src/test/ui-fulldeps/proc-macro/load-panic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: proc-macro derive panicked
22
--> $DIR/load-panic.rs:17:10
33
|
4-
17 | #[derive(A)]
4+
LL | #[derive(A)]
55
| ^
66
|
77
= help: message: nope!

0 commit comments

Comments
 (0)