rustdoc: Properly detect syntactically invalid doctests (to fix a regression) by fmease · Pull Request #148101 · rust-lang/rust (original) (raw)
Previously, we would only consider doctests to be syntactically invalid (for our crude static analysis pass) if (we failed to lex it or if) the parser returned an Err(_)1.
However, since the parser can actually recover from a slew of invalid forms, it may return Ok(node) even in the presence of syntax errors. E.g., stray outer attrs may just get jettisoned. Combine that with the fact that we (send all diagnostics into the ether and) only forward selected spans of the original source to the final runnable doctest, it led to issues like #147999.
To remedy that, check if dcx.has_errors().
Fixes #147999 (regressed in #138104).
FIXME: Okay, due to the existence of error code checks like compile_fail,E0726 I actually have to preserve some existing behavior here and continue wrapping certain classes of syntactically invalid doctests in functions, sigh.
Footnotes
- Or if the one of the expr stmts was an
ExprKind::Err(_)(a special case / hack to paper over things). ↩