Skip to content

Commit 6f123ce

Browse files
committed
Support flag -Z ui-testing for tweaking diagnostic output for UI tests
1 parent 28a1e4f commit 6f123ce

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 2 deletions
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))

src/librustc_errors/emitter.rs

Lines changed: 21 additions & 3 deletions
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,9 +154,14 @@ impl EmitterWriter {
149154
cm: code_map,
150155
short_message,
151156
teach,
157+
ui_testing: false,
152158
}
153159
}
154160

161+
pub fn ui_testing(self, ui_testing: bool) -> Self {
162+
Self { ui_testing, ..self }
163+
}
164+
155165
fn preprocess_annotations(&mut self, msp: &MultiSpan) -> Vec<FileWithAnnotatedLines> {
156166
fn add_annotation_to_file(file_vec: &mut Vec<FileWithAnnotatedLines>,
157167
file: Rc<FileMap>,
@@ -303,9 +313,14 @@ impl EmitterWriter {
303313

304314
// First create the source line we will highlight.
305315
buffer.puts(line_offset, code_offset, &source_string, Style::Quotation);
316+
let line_index = if self.ui_testing {
317+
ANONYMIZED_LINE_NUM.to_string()
318+
} else {
319+
line.line_index.to_string()
320+
};
306321
buffer.puts(line_offset,
307322
0,
308-
&(line.line_index.to_string()),
323+
&line_index,
309324
Style::LineNumber);
310325

311326
draw_col_separator(buffer, line_offset, width_offset - 2);
@@ -1253,8 +1268,11 @@ impl EmitterWriter {
12531268
span: &MultiSpan,
12541269
children: &Vec<SubDiagnostic>,
12551270
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();
1271+
let max_line_num_len = if self.ui_testing {
1272+
ANONYMIZED_LINE_NUM.len()
1273+
} else {
1274+
self.get_max_line_num(span, children).to_string().len()
1275+
};
12581276

12591277
match self.emit_message_default(span,
12601278
message,

src/libsyntax/json.rs

Lines changed: 9 additions & 1 deletion
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/tools/compiletest/src/runtest.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,13 +1624,12 @@ impl<'test> TestCx<'test> {
16241624
rustc.args(&["--error-format", "json"]);
16251625
}
16261626
}
1627-
Ui => if !self.props
1628-
.compile_flags
1629-
.iter()
1630-
.any(|s| s.starts_with("--error-format"))
1631-
{
1632-
rustc.args(&["--error-format", "json"]);
1633-
},
1627+
Ui => {
1628+
rustc.arg("-Zui-testing");
1629+
if !self.props.compile_flags.iter().any(|s| s.starts_with("--error-format")) {
1630+
rustc.args(&["--error-format", "json"]);
1631+
}
1632+
}
16341633
MirOpt => {
16351634
rustc.args(&[
16361635
"-Zdump-mir=all",

0 commit comments

Comments
 (0)