Skip to content

Commit cdbd8c2

Browse files
committed
Support flag -Z ui-testing for tweaking diagnostic output for UI tests
1 parent bedbad6 commit cdbd8c2

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
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

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

src/librustc_errors/emitter.rs

+21-3
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,9 +185,14 @@ impl EmitterWriter {
180185
short_message,
181186
teach,
182187
error_codes: HashSet::new(),
188+
ui_testing: false,
183189
}
184190
}
185191

192+
pub fn ui_testing(self, ui_testing: bool) -> Self {
193+
Self { ui_testing, ..self }
194+
}
195+
186196
fn preprocess_annotations(&mut self, msp: &MultiSpan) -> Vec<FileWithAnnotatedLines> {
187197
fn add_annotation_to_file(file_vec: &mut Vec<FileWithAnnotatedLines>,
188198
file: Rc<FileMap>,
@@ -334,9 +344,14 @@ impl EmitterWriter {
334344

335345
// First create the source line we will highlight.
336346
buffer.puts(line_offset, code_offset, &source_string, Style::Quotation);
347+
let line_index = if self.ui_testing {
348+
ANONYMIZED_LINE_NUM.to_string()
349+
} else {
350+
line.line_index.to_string()
351+
};
337352
buffer.puts(line_offset,
338353
0,
339-
&(line.line_index.to_string()),
354+
&line_index,
340355
Style::LineNumber);
341356

342357
draw_col_separator(buffer, line_offset, width_offset - 2);
@@ -1288,8 +1303,11 @@ impl EmitterWriter {
12881303
span: &MultiSpan,
12891304
children: &Vec<SubDiagnostic>,
12901305
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();
1306+
let max_line_num_len = if self.ui_testing {
1307+
ANONYMIZED_LINE_NUM.len()
1308+
} else {
1309+
self.get_max_line_num(span, children).to_string().len()
1310+
};
12931311

12941312
match self.emit_message_default(span,
12951313
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/tools/compiletest/src/runtest.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1627,15 +1627,12 @@ impl<'test> TestCx<'test> {
16271627
rustc.args(&["--error-format", "json"]);
16281628
}
16291629
}
1630-
Ui => if !self.props
1631-
.compile_flags
1632-
.iter()
1633-
.any(|s| s.starts_with("--error-format"))
1634-
{
1630+
Ui => {
16351631
// In case no "--error-format" has been given in the test, we'll compile
16361632
// a first time to get the compiler's output then compile with
16371633
// "--error-format json" to check if all expected errors are actually there
16381634
// and that no new one appeared.
1635+
rustc.arg("-Zui-testing");
16391636
}
16401637
MirOpt => {
16411638
rustc.args(&[

0 commit comments

Comments
 (0)