Skip to content

Commit

Permalink
Auto merge of rust-lang#91149 - notriddle:notriddle/rustdoc-doctest-s…
Browse files Browse the repository at this point in the history
…emicolon, r=jyn514

fix(doctest): detect extern crate items in statement doctests

This partially reverts rust-lang#91026, because rustdoc needs to detect the extern statements, even when they appear inside implicit `main()`. It does not entirely revert it, so the old bug is still fixed, by duplicating some of the logic from `parse_mod` instead of trying to use it directly.

Fixes rust-lang#91134
  • Loading branch information
bors committed Nov 23, 2021
2 parents 65c55bf + 1e21dfa commit de4b242
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ impl<'a> Parser<'a> {
Ok(P(T::recovered(Some(QSelf { ty, path_span, position: 0 }), path)))
}

pub(super) fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
pub fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
if self.eat(&token::Semi) {
let mut err = self.struct_span_err(self.prev_token.span, "expected item, found `;`");
err.span_suggestion_short(
Expand Down
21 changes: 14 additions & 7 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_ast::{self as ast, token};
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;
use rustc_errors::{ColorConfig, ErrorReported, FatalError};
Expand Down Expand Up @@ -537,6 +537,7 @@ crate fn make_test(
use rustc_errors::emitter::{Emitter, EmitterWriter};
use rustc_errors::Handler;
use rustc_parse::maybe_new_parser_from_source_str;
use rustc_parse::parser::ForceCollect;
use rustc_session::parse::ParseSess;
use rustc_span::source_map::FilePathMapping;

Expand Down Expand Up @@ -572,9 +573,9 @@ crate fn make_test(
}
};

match parser.parse_mod(&token::Eof) {
Ok((_attrs, items, _span)) => {
for item in items {
loop {
match parser.parse_item(ForceCollect::No) {
Ok(Some(item)) => {
if !found_main {
if let ast::ItemKind::Fn(..) = item.kind {
if item.ident.name == sym::main {
Expand Down Expand Up @@ -606,10 +607,16 @@ crate fn make_test(
break;
}
}
Ok(None) => break,
Err(mut e) => {
e.cancel();
break;
}
}
Err(mut e) => {
e.cancel();
}

// The supplied slice is only used for diagnostics,
// which are swallowed here anyway.
parser.maybe_consume_incorrect_semicolon(&[]);
}

// Reset errors so that they won't be reported as compiler bugs when dropping the
Expand Down
3 changes: 3 additions & 0 deletions src/test/rustdoc-ui/auxiliary/empty-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// no-prefer-dynamic
#![crate_type = "lib"]
pub fn empty() {}
14 changes: 14 additions & 0 deletions src/test/rustdoc-ui/issue-91134.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// compile-flags: --test --crate-name=empty_fn --extern=empty_fn --test-args=--test-threads=1
// aux-build:empty-fn.rs
// check-pass
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
// edition:2021

/// <https://github.com/rust-lang/rust/issues/91134>
///
/// ```
/// extern crate empty_fn;
/// empty_fn::empty();
/// ```
pub struct Something;
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/issue-91134.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

running 1 test
test $DIR/issue-91134.rs - Something (line 10) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

0 comments on commit de4b242

Please sign in to comment.