Skip to content

Commit

Permalink
Rollup merge of rust-lang#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 rust-lang#46643
  • Loading branch information
Manishearth committed Feb 25, 2018
2 parents 657a643 + cd7ce71 commit 2b6cc53
Show file tree
Hide file tree
Showing 1,223 changed files with 6,823 additions and 6,654 deletions.
2 changes: 2 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
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
6 changes: 4 additions & 2 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,13 @@ pub fn build_session_with_codemap(sopts: config::Options,
}
(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
33 changes: 27 additions & 6 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use std::collections::HashMap;
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 @@ -107,6 +109,7 @@ pub struct EmitterWriter {
cm: Option<Rc<CodeMapper>>,
short_message: bool,
teach: bool,
ui_testing: bool,
}

struct FileWithAnnotatedLines {
Expand All @@ -128,13 +131,15 @@ impl EmitterWriter {
cm: code_map,
short_message,
teach,
ui_testing: false,
}
} else {
EmitterWriter {
dst: Raw(Box::new(io::stderr())),
cm: code_map,
short_message,
teach,
ui_testing: false,
}
}
}
Expand All @@ -149,6 +154,19 @@ impl EmitterWriter {
cm: code_map,
short_message,
teach,
ui_testing: false,
}
}

pub fn ui_testing(self, ui_testing: bool) -> Self {
Self { 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 @@ -305,7 +323,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 @@ -1126,8 +1144,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 @@ -1201,7 +1219,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 @@ -1253,8 +1271,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 2b6cc53

Please sign in to comment.