Skip to content

Commit 1584e5f

Browse files
committed
Squelch a noisy rustc_expand unittest
1 parent d908a5b commit 1584e5f

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

compiler/rustc_expand/src/parse/tests.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse};
1+
use crate::tests::{
2+
matches_codepattern, string_to_stream, with_error_checking_parse, with_expected_parse_error,
3+
};
24

35
use rustc_ast::ptr::P;
46
use rustc_ast::token::{self, Delimiter, Token};
@@ -51,11 +53,15 @@ fn string_to_item(source_str: String) -> Option<P<ast::Item>> {
5153
with_error_checking_parse(source_str, &sess(), |p| p.parse_item(ForceCollect::No))
5254
}
5355

54-
#[should_panic]
5556
#[test]
5657
fn bad_path_expr_1() {
58+
// This should trigger error: expected identifier, found keyword `return`
5759
create_default_session_globals_then(|| {
58-
string_to_expr("::abc::def::return".to_string());
60+
with_expected_parse_error(
61+
"::abc::def::return",
62+
"expected identifier, found keyword `return`",
63+
|p| p.parse_expr(),
64+
);
5965
})
6066
}
6167

compiler/rustc_expand/src/tests.rs

+48-21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ fn string_to_parser(ps: &ParseSess, source_str: String) -> Parser<'_> {
2222
new_parser_from_source_str(ps, PathBuf::from("bogofile").into(), source_str)
2323
}
2424

25+
fn create_test_handler() -> (Handler, Lrc<SourceMap>, Arc<Mutex<Vec<u8>>>) {
26+
let output = Arc::new(Mutex::new(Vec::new()));
27+
let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
28+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
29+
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
30+
false,
31+
);
32+
let emitter = EmitterWriter::new(
33+
Box::new(Shared { data: output.clone() }),
34+
Some(source_map.clone()),
35+
None,
36+
fallback_bundle,
37+
false,
38+
false,
39+
false,
40+
Some(140),
41+
false,
42+
false,
43+
TerminalUrl::No,
44+
);
45+
let handler = Handler::with_emitter(true, None, Box::new(emitter), None);
46+
(handler, source_map, output)
47+
}
48+
49+
/// Returns the result of parsing the given string via the given callback.
50+
///
51+
/// If there are any errors, this will panic.
2552
pub(crate) fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F) -> T
2653
where
2754
F: FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
@@ -32,6 +59,26 @@ where
3259
x
3360
}
3461

62+
/// Verifies that parsing the given string using the given callback will
63+
/// generate an error that contains the given text.
64+
pub(crate) fn with_expected_parse_error<T, F>(source_str: &str, expected_output: &str, f: F)
65+
where
66+
F: for<'a> FnOnce(&mut Parser<'a>) -> PResult<'a, T>,
67+
{
68+
let (handler, source_map, output) = create_test_handler();
69+
let ps = ParseSess::with_span_handler(handler, source_map);
70+
let mut p = string_to_parser(&ps, source_str.to_string());
71+
let result = f(&mut p);
72+
assert!(result.is_ok());
73+
74+
let bytes = output.lock().unwrap();
75+
let actual_output = str::from_utf8(&bytes).unwrap();
76+
println!("expected output:\n------\n{}------", expected_output);
77+
println!("actual output:\n------\n{}------", actual_output);
78+
79+
assert!(actual_output.contains(expected_output))
80+
}
81+
3582
/// Maps a string to tts, using a made-up filename.
3683
pub(crate) fn string_to_stream(source_str: String) -> TokenStream {
3784
let ps = ParseSess::new(
@@ -130,13 +177,7 @@ impl<T: Write> Write for Shared<T> {
130177

131178
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
132179
create_default_session_if_not_set_then(|_| {
133-
let output = Arc::new(Mutex::new(Vec::new()));
134-
135-
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
136-
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
137-
false,
138-
);
139-
let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
180+
let (handler, source_map, output) = create_test_handler();
140181
source_map.new_source_file(Path::new("test.rs").to_owned().into(), file_text.to_owned());
141182

142183
let primary_span = make_span(&file_text, &span_labels[0].start, &span_labels[0].end);
@@ -148,20 +189,6 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
148189
println!("text: {:?}", source_map.span_to_snippet(span));
149190
}
150191

151-
let emitter = EmitterWriter::new(
152-
Box::new(Shared { data: output.clone() }),
153-
Some(source_map.clone()),
154-
None,
155-
fallback_bundle,
156-
false,
157-
false,
158-
false,
159-
None,
160-
false,
161-
false,
162-
TerminalUrl::No,
163-
);
164-
let handler = Handler::with_emitter(true, None, Box::new(emitter), None);
165192
#[allow(rustc::untranslatable_diagnostic)]
166193
handler.span_err(msp, "foo");
167194

0 commit comments

Comments
 (0)