Skip to content

Commit 2188a37

Browse files
Fix case where AST failed to parse to give better errors
1 parent 4235fbe commit 2188a37

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/librustdoc/doctest.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,7 @@ fn run_test(
459459
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
460460
stdin.write_all(test.as_bytes()).expect("could write out test sources");
461461
}
462-
let output = if !is_multiple_tests {
463-
let status = child.wait().expect("Failed to wait");
464-
process::Output { status, stdout: Vec::new(), stderr: Vec::new() }
465-
} else {
466-
child.wait_with_output().expect("Failed to read stdout")
467-
};
462+
let output = child.wait_with_output().expect("Failed to read stdout");
468463

469464
struct Bomb<'a>(&'a str);
470465
impl Drop for Bomb<'_> {
@@ -603,6 +598,11 @@ impl DocTest {
603598
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
604599
test_id: Option<&str>,
605600
) -> (String, usize) {
601+
if self.failed_ast {
602+
// If the AST failed to compile, no need to go generate a complete doctest, the error
603+
// will be better this way.
604+
return (self.everything_else.clone(), 0);
605+
}
606606
let mut line_offset = 0;
607607
let mut prog = String::with_capacity(
608608
self.test_code.len() + self.crate_attrs.len() + self.crates.len(),
@@ -942,11 +942,10 @@ pub(crate) fn make_test(
942942
Ok(p) => p,
943943
Err(errs) => {
944944
errs.into_iter().for_each(|err| err.cancel());
945-
return (found_main, found_extern_crate, found_macro, true);
945+
return (found_main, found_extern_crate, found_macro);
946946
}
947947
};
948948

949-
let mut has_errors = false;
950949
loop {
951950
match parser.parse_item(ForceCollect::No) {
952951
Ok(Some(item)) => {
@@ -977,7 +976,6 @@ pub(crate) fn make_test(
977976
Ok(None) => break,
978977
Err(e) => {
979978
e.cancel();
980-
has_errors = true;
981979
break;
982980
}
983981
}
@@ -987,14 +985,13 @@ pub(crate) fn make_test(
987985
parser.maybe_consume_incorrect_semicolon(&[]);
988986
}
989987

990-
has_errors = has_errors || psess.dcx.has_errors_or_delayed_bugs().is_some();
991988
// Reset errors so that they won't be reported as compiler bugs when dropping the
992989
// dcx. Any errors in the tests will be reported when the test file is compiled,
993990
// Note that we still need to cancel the errors above otherwise `Diag` will panic on
994991
// drop.
995992
psess.dcx.reset_err_count();
996993

997-
(found_main, found_extern_crate, found_macro, has_errors)
994+
(found_main, found_extern_crate, found_macro)
998995
})
999996
});
1000997

@@ -1003,7 +1000,7 @@ pub(crate) fn make_test(
10031000
Ignore::None => false,
10041001
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
10051002
};
1006-
let Ok((mut main_fn_span, already_has_extern_crate, found_macro, has_errors)) = result else {
1003+
let Ok((mut main_fn_span, already_has_extern_crate, found_macro)) = result else {
10071004
// If the parser panicked due to a fatal error, pass the test code through unchanged.
10081005
// The error will be reported during compilation.
10091006
return DocTest {
@@ -1059,7 +1056,7 @@ pub(crate) fn make_test(
10591056
lang_string,
10601057
line,
10611058
file,
1062-
failed_ast: has_errors,
1059+
failed_ast: false,
10631060
rustdoc_test_options,
10641061
outdir,
10651062
test_id,
@@ -1320,6 +1317,10 @@ impl DocTestKinds {
13201317
#![feature(coverage_attribute)]\n"
13211318
.to_string();
13221319

1320+
for doctest in &doctests {
1321+
output.push_str(&doctest.crate_attrs);
1322+
}
1323+
13231324
DocTest::push_attrs(&mut output, &opts, &mut 0);
13241325
output.push_str("extern crate test;\n");
13251326

0 commit comments

Comments
 (0)