Skip to content

Commit de4b242

Browse files
committed
Auto merge of #91149 - notriddle:notriddle/rustdoc-doctest-semicolon, r=jyn514
fix(doctest): detect extern crate items in statement doctests This partially reverts #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 #91134
2 parents 65c55bf + 1e21dfa commit de4b242

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ impl<'a> Parser<'a> {
11211121
Ok(P(T::recovered(Some(QSelf { ty, path_span, position: 0 }), path)))
11221122
}
11231123

1124-
pub(super) fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
1124+
pub fn maybe_consume_incorrect_semicolon(&mut self, items: &[P<Item>]) -> bool {
11251125
if self.eat(&token::Semi) {
11261126
let mut err = self.struct_span_err(self.prev_token.span, "expected item, found `;`");
11271127
err.span_suggestion_short(

src/librustdoc/doctest.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{self as ast, token};
1+
use rustc_ast as ast;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_data_structures::sync::Lrc;
44
use rustc_errors::{ColorConfig, ErrorReported, FatalError};
@@ -537,6 +537,7 @@ crate fn make_test(
537537
use rustc_errors::emitter::{Emitter, EmitterWriter};
538538
use rustc_errors::Handler;
539539
use rustc_parse::maybe_new_parser_from_source_str;
540+
use rustc_parse::parser::ForceCollect;
540541
use rustc_session::parse::ParseSess;
541542
use rustc_span::source_map::FilePathMapping;
542543

@@ -572,9 +573,9 @@ crate fn make_test(
572573
}
573574
};
574575

575-
match parser.parse_mod(&token::Eof) {
576-
Ok((_attrs, items, _span)) => {
577-
for item in items {
576+
loop {
577+
match parser.parse_item(ForceCollect::No) {
578+
Ok(Some(item)) => {
578579
if !found_main {
579580
if let ast::ItemKind::Fn(..) = item.kind {
580581
if item.ident.name == sym::main {
@@ -606,10 +607,16 @@ crate fn make_test(
606607
break;
607608
}
608609
}
610+
Ok(None) => break,
611+
Err(mut e) => {
612+
e.cancel();
613+
break;
614+
}
609615
}
610-
Err(mut e) => {
611-
e.cancel();
612-
}
616+
617+
// The supplied slice is only used for diagnostics,
618+
// which are swallowed here anyway.
619+
parser.maybe_consume_incorrect_semicolon(&[]);
613620
}
614621

615622
// Reset errors so that they won't be reported as compiler bugs when dropping the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// no-prefer-dynamic
2+
#![crate_type = "lib"]
3+
pub fn empty() {}

src/test/rustdoc-ui/issue-91134.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: --test --crate-name=empty_fn --extern=empty_fn --test-args=--test-threads=1
2+
// aux-build:empty-fn.rs
3+
// check-pass
4+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
5+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
// edition:2021
7+
8+
/// <https://github.com/rust-lang/rust/issues/91134>
9+
///
10+
/// ```
11+
/// extern crate empty_fn;
12+
/// empty_fn::empty();
13+
/// ```
14+
pub struct Something;
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/issue-91134.rs - Something (line 10) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+

0 commit comments

Comments
 (0)