Skip to content

Commit

Permalink
Fix case where AST failed to parse to give better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 15, 2024
1 parent 4235fbe commit bff1867
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ impl DocTest {
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
test_id: Option<&str>,
) -> (String, usize) {
if self.failed_ast {
// If the AST failed to compile, no need to go generate a complete doctest, the error
// will be better this way.
return (self.everything_else.clone(), 0);
}
let mut line_offset = 0;
let mut prog = String::with_capacity(
self.test_code.len() + self.crate_attrs.len() + self.crates.len(),
Expand Down Expand Up @@ -942,11 +947,10 @@ pub(crate) fn make_test(
Ok(p) => p,
Err(errs) => {
errs.into_iter().for_each(|err| err.cancel());
return (found_main, found_extern_crate, found_macro, true);
return (found_main, found_extern_crate, found_macro);
}
};

let mut has_errors = false;
loop {
match parser.parse_item(ForceCollect::No) {
Ok(Some(item)) => {
Expand Down Expand Up @@ -977,7 +981,6 @@ pub(crate) fn make_test(
Ok(None) => break,
Err(e) => {
e.cancel();
has_errors = true;
break;
}
}
Expand All @@ -987,14 +990,13 @@ pub(crate) fn make_test(
parser.maybe_consume_incorrect_semicolon(&[]);
}

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

(found_main, found_extern_crate, found_macro, has_errors)
(found_main, found_extern_crate, found_macro)
})
});

Expand All @@ -1003,7 +1005,7 @@ pub(crate) fn make_test(
Ignore::None => false,
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
};
let Ok((mut main_fn_span, already_has_extern_crate, found_macro, has_errors)) = result else {
let Ok((mut main_fn_span, already_has_extern_crate, found_macro)) = result else {
// If the parser panicked due to a fatal error, pass the test code through unchanged.
// The error will be reported during compilation.
return DocTest {
Expand Down Expand Up @@ -1059,7 +1061,7 @@ pub(crate) fn make_test(
lang_string,
line,
file,
failed_ast: has_errors,
failed_ast: false,
rustdoc_test_options,
outdir,
test_id,
Expand Down

0 comments on commit bff1867

Please sign in to comment.