Skip to content

Commit

Permalink
Auto merge of #48449 - petrochenkov:uidiff, r=nikomatsakis
Browse files Browse the repository at this point in the history
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 #46643
  • Loading branch information
bors committed Feb 27, 2018
2 parents bedbad6 + 9f9183d commit 29f5c69
Show file tree
Hide file tree
Showing 1,230 changed files with 6,878 additions and 6,708 deletions.
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Expand Up @@ -1322,6 +1322,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
epoch). Crates compiled with different epochs can be linked together."),
run_dsymutil: Option<bool> = (None, parse_opt_bool, [TRACKED],
"run `dsymutil` and delete intermediate object files"),
ui_testing: bool = (false, parse_bool, [UNTRACKED],
"format compiler diagnostics in a way that's better suitable for UI testing"),
}

pub fn default_lib_output() -> CrateType {
Expand Down
17 changes: 10 additions & 7 deletions src/librustc/session/mod.rs
Expand Up @@ -909,21 +909,24 @@ pub fn build_session_with_codemap(sopts: config::Options,

let emitter: Box<Emitter> = match (sopts.error_format, emitter_dest) {
(config::ErrorOutputType::HumanReadable(color_config), None) => {
Box::new(EmitterWriter::stderr(color_config,
Some(codemap.clone()),
false,
sopts.debugging_opts.teach))
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()),
false, sopts.debugging_opts.teach)
.ui_testing(sopts.debugging_opts.ui_testing))
}
(config::ErrorOutputType::HumanReadable(_), Some(dst)) => {
Box::new(EmitterWriter::new(dst, Some(codemap.clone()), false, false))
Box::new(EmitterWriter::new(dst, Some(codemap.clone()),
false, false)
.ui_testing(sopts.debugging_opts.ui_testing))
}
(config::ErrorOutputType::Json(pretty), None) => {
Box::new(JsonEmitter::stderr(Some(registry), codemap.clone(),
pretty, sopts.debugging_opts.approximate_suggestions))
pretty, sopts.debugging_opts.approximate_suggestions)
.ui_testing(sopts.debugging_opts.ui_testing))
}
(config::ErrorOutputType::Json(pretty), Some(dst)) => {
Box::new(JsonEmitter::new(dst, Some(registry), codemap.clone(),
pretty, sopts.debugging_opts.approximate_suggestions))
pretty, sopts.debugging_opts.approximate_suggestions)
.ui_testing(sopts.debugging_opts.ui_testing))
}
(config::ErrorOutputType::Short(color_config), None) => {
Box::new(EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false))
Expand Down
34 changes: 28 additions & 6 deletions src/librustc_errors/emitter.rs
Expand Up @@ -25,6 +25,8 @@ use std::collections::{HashMap, HashSet};
use std::cmp::min;
use unicode_width;

const ANONYMIZED_LINE_NUM: &str = "LL";

/// Emitter trait for emitting errors.
pub trait Emitter {
/// Emit a structured diagnostic.
Expand Down Expand Up @@ -108,6 +110,7 @@ pub struct EmitterWriter {
short_message: bool,
teach: bool,
error_codes: HashSet<String>,
ui_testing: bool,
}

struct FileWithAnnotatedLines {
Expand Down Expand Up @@ -157,6 +160,7 @@ impl EmitterWriter {
short_message,
teach,
error_codes: HashSet::new(),
ui_testing: false,
}
} else {
EmitterWriter {
Expand All @@ -165,6 +169,7 @@ impl EmitterWriter {
short_message,
teach,
error_codes: HashSet::new(),
ui_testing: false,
}
}
}
Expand All @@ -180,6 +185,20 @@ impl EmitterWriter {
short_message,
teach,
error_codes: HashSet::new(),
ui_testing: false,
}
}

pub fn ui_testing(mut self, ui_testing: bool) -> Self {
self.ui_testing = ui_testing;
self
}

fn maybe_anonymized(&self, line_num: usize) -> String {
if self.ui_testing {
ANONYMIZED_LINE_NUM.to_string()
} else {
line_num.to_string()
}
}

Expand Down Expand Up @@ -336,7 +355,7 @@ impl EmitterWriter {
buffer.puts(line_offset, code_offset, &source_string, Style::Quotation);
buffer.puts(line_offset,
0,
&(line.line_index.to_string()),
&self.maybe_anonymized(line.line_index),
Style::LineNumber);

draw_col_separator(buffer, line_offset, width_offset - 2);
Expand Down Expand Up @@ -1159,8 +1178,8 @@ impl EmitterWriter {

buffer.puts(last_buffer_line_num,
0,
&(annotated_file.lines[line_idx + 1].line_index - 1)
.to_string(),
&self.maybe_anonymized(annotated_file.lines[line_idx + 1]
.line_index - 1),
Style::LineNumber);
draw_col_separator(&mut buffer,
last_buffer_line_num,
Expand Down Expand Up @@ -1235,7 +1254,7 @@ impl EmitterWriter {
// Print the span column to avoid confusion
buffer.puts(row_num,
0,
&((line_start + line_pos).to_string()),
&self.maybe_anonymized(line_start + line_pos),
Style::LineNumber);
// print the suggestion
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
Expand Down Expand Up @@ -1288,8 +1307,11 @@ impl EmitterWriter {
span: &MultiSpan,
children: &Vec<SubDiagnostic>,
suggestions: &[CodeSuggestion]) {
let max_line_num = self.get_max_line_num(span, children);
let max_line_num_len = max_line_num.to_string().len();
let max_line_num_len = if self.ui_testing {
ANONYMIZED_LINE_NUM.len()
} else {
self.get_max_line_num(span, children).to_string().len()
};

match self.emit_message_default(span,
message,
Expand Down
10 changes: 9 additions & 1 deletion src/libsyntax/json.rs
Expand Up @@ -40,6 +40,7 @@ pub struct JsonEmitter {
pretty: bool,
/// Whether "approximate suggestions" are enabled in the config
approximate_suggestions: bool,
ui_testing: bool,
}

impl JsonEmitter {
Expand All @@ -53,6 +54,7 @@ impl JsonEmitter {
cm: code_map,
pretty,
approximate_suggestions,
ui_testing: false,
}
}

Expand All @@ -73,8 +75,13 @@ impl JsonEmitter {
cm: code_map,
pretty,
approximate_suggestions,
ui_testing: false,
}
}

pub fn ui_testing(self, ui_testing: bool) -> Self {
Self { ui_testing, ..self }
}
}

impl Emitter for JsonEmitter {
Expand Down Expand Up @@ -199,7 +206,8 @@ impl Diagnostic {
}
let buf = BufWriter::default();
let output = buf.clone();
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false).emit(db);
EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false)
.ui_testing(je.ui_testing).emit(db);
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
let output = String::from_utf8(output).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/custom-derive/issue-36935.stderr
@@ -1,7 +1,7 @@
error: proc-macro derive panicked
--> $DIR/issue-36935.rs:18:15
|
18 | #[derive(Foo, Bar)] //~ ERROR proc-macro derive panicked
LL | #[derive(Foo, Bar)] //~ ERROR proc-macro derive panicked
| ^^^
|
= help: message: lolnope
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/deprecated-derive.stderr
@@ -1,6 +1,6 @@
warning: derive(Encodable) is deprecated in favor of derive(RustcEncodable)
--> $DIR/deprecated-derive.rs:18:10
|
18 | #[derive(Encodable)]
LL | #[derive(Encodable)]
| ^^^^^^^^^

4 changes: 2 additions & 2 deletions src/test/ui-fulldeps/lint-group-plugin.stderr
@@ -1,15 +1,15 @@
warning: item is named 'lintme'
--> $DIR/lint-group-plugin.rs:18:1
|
18 | fn lintme() { } //~ WARNING item is named 'lintme'
LL | fn lintme() { } //~ WARNING item is named 'lintme'
| ^^^^^^^^^^^^^^^
|
= note: #[warn(test_lint)] on by default

warning: item is named 'pleaselintme'
--> $DIR/lint-group-plugin.rs:19:1
|
19 | fn pleaselintme() { } //~ WARNING item is named 'pleaselintme'
LL | fn pleaselintme() { } //~ WARNING item is named 'pleaselintme'
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(please_lint)] on by default
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr
@@ -1,13 +1,13 @@
warning: function is never used: `lintme`
--> $DIR/lint-plugin-cmdline-allow.rs:20:1
|
20 | fn lintme() { }
LL | fn lintme() { }
| ^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/lint-plugin-cmdline-allow.rs:17:9
|
17 | #![warn(unused)]
LL | #![warn(unused)]
| ^^^^^^
= note: #[warn(dead_code)] implied by #[warn(unused)]

2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/lint-plugin-cmdline-load.stderr
@@ -1,7 +1,7 @@
warning: item is named 'lintme'
--> $DIR/lint-plugin-cmdline-load.rs:18:1
|
18 | fn lintme() { } //~ WARNING item is named 'lintme'
LL | fn lintme() { } //~ WARNING item is named 'lintme'
| ^^^^^^^^^^^^^^^
|
= note: #[warn(test_lint)] on by default
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui-fulldeps/lint-plugin-forbid-attrs.stderr
@@ -1,22 +1,22 @@
error: item is named 'lintme'
--> $DIR/lint-plugin-forbid-attrs.rs:18:1
|
18 | fn lintme() { } //~ ERROR item is named 'lintme'
LL | fn lintme() { } //~ ERROR item is named 'lintme'
| ^^^^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/lint-plugin-forbid-attrs.rs:16:11
|
16 | #![forbid(test_lint)]
LL | #![forbid(test_lint)]
| ^^^^^^^^^

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

error: aborting due to 2 previous errors
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/lint-plugin.stderr
@@ -1,7 +1,7 @@
warning: item is named 'lintme'
--> $DIR/lint-plugin.rs:18:1
|
18 | fn lintme() { } //~ WARNING item is named 'lintme'
LL | fn lintme() { } //~ WARNING item is named 'lintme'
| ^^^^^^^^^^^^^^^
|
= note: #[warn(test_lint)] on by default
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/proc-macro/load-panic.stderr
@@ -1,7 +1,7 @@
error: proc-macro derive panicked
--> $DIR/load-panic.rs:17:10
|
17 | #[derive(A)]
LL | #[derive(A)]
| ^
|
= help: message: nope!
Expand Down

0 comments on commit 29f5c69

Please sign in to comment.