Skip to content

Commit 743d5a6

Browse files
Fix case where AST failed to parse to give better errors
1 parent bed435a commit 743d5a6

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/librustdoc/doctest.rs

Lines changed: 16 additions & 14 deletions
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(),
@@ -846,7 +846,7 @@ pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{
846846
end_line: 0,
847847
end_col: 0,
848848
compile_fail: false,
849-
no_run: false,
849+
no_run: {no_run},
850850
should_panic: test::ShouldPanic::{should_panic},
851851
test_type: test::TestType::UnitTest,
852852
}},
@@ -860,6 +860,7 @@ pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{
860860
ignore = self.ignore,
861861
file = self.file,
862862
line = self.line,
863+
no_run = self.no_run,
863864
should_panic = if !self.no_run && self.lang_string.should_panic { "Yes" } else { "No" },
864865
// Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
865866
// don't give it the function to run.
@@ -942,11 +943,10 @@ pub(crate) fn make_test(
942943
Ok(p) => p,
943944
Err(errs) => {
944945
errs.into_iter().for_each(|err| err.cancel());
945-
return (found_main, found_extern_crate, found_macro, true);
946+
return (found_main, found_extern_crate, found_macro);
946947
}
947948
};
948949

949-
let mut has_errors = false;
950950
loop {
951951
match parser.parse_item(ForceCollect::No) {
952952
Ok(Some(item)) => {
@@ -977,7 +977,6 @@ pub(crate) fn make_test(
977977
Ok(None) => break,
978978
Err(e) => {
979979
e.cancel();
980-
has_errors = true;
981980
break;
982981
}
983982
}
@@ -987,14 +986,13 @@ pub(crate) fn make_test(
987986
parser.maybe_consume_incorrect_semicolon(&[]);
988987
}
989988

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

997-
(found_main, found_extern_crate, found_macro, has_errors)
995+
(found_main, found_extern_crate, found_macro)
998996
})
999997
});
1000998

@@ -1003,7 +1001,7 @@ pub(crate) fn make_test(
10031001
Ignore::None => false,
10041002
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
10051003
};
1006-
let Ok((mut main_fn_span, already_has_extern_crate, found_macro, has_errors)) = result else {
1004+
let Ok((mut main_fn_span, already_has_extern_crate, found_macro)) = result else {
10071005
// If the parser panicked due to a fatal error, pass the test code through unchanged.
10081006
// The error will be reported during compilation.
10091007
return DocTest {
@@ -1059,7 +1057,7 @@ pub(crate) fn make_test(
10591057
lang_string,
10601058
line,
10611059
file,
1062-
failed_ast: has_errors,
1060+
failed_ast: false,
10631061
rustdoc_test_options,
10641062
outdir,
10651063
test_id,
@@ -1320,6 +1318,10 @@ impl DocTestKinds {
13201318
#![feature(coverage_attribute)]\n"
13211319
.to_string();
13221320

1321+
for doctest in &doctests {
1322+
output.push_str(&doctest.crate_attrs);
1323+
}
1324+
13231325
DocTest::push_attrs(&mut output, &opts, &mut 0);
13241326
output.push_str("extern crate test;\n");
13251327

0 commit comments

Comments
 (0)