Skip to content

Commit 4f8ada3

Browse files
authored
Unrolled build for rust-lang#140220
Rollup merge of rust-lang#140220 - GuillaumeGomez:doctest-main-wrapping, r=fmease Fix detection of main function if there are expressions around it Fixes rust-lang#140162. Fixes rust-lang#139651. Once this is merged, we can backport and I'll send a follow-up to emit a warning in case a `main` function is about to be "wrapped" (and therefore not run). r? `@fmease` try-job: x86_64-mingw-1
2 parents 7d65abf + aa69e3a commit 4f8ada3

8 files changed

+77
-32
lines changed

src/librustdoc/doctest/make.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -407,17 +407,27 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
407407
push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi);
408408
}
409409
}
410+
let mut has_non_items = false;
410411
for stmt in &body.stmts {
411412
let mut is_extern_crate = false;
412413
match stmt.kind {
413414
StmtKind::Item(ref item) => {
414415
is_extern_crate = check_item(&item, &mut info, crate_name);
415416
}
416-
StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => {
417-
reset_error_count(&psess);
418-
return Err(());
417+
StmtKind::Expr(ref expr) => {
418+
if matches!(expr.kind, ast::ExprKind::Err(_)) {
419+
reset_error_count(&psess);
420+
return Err(());
421+
}
422+
has_non_items = true;
419423
}
420-
StmtKind::MacCall(ref mac_call) if !info.has_main_fn => {
424+
// We assume that the macro calls will expand to item(s) even though they could
425+
// expand to statements and expressions. And the simple fact that we're trying
426+
// to retrieve a `main` function inside it is a terrible idea.
427+
StmtKind::MacCall(ref mac_call) => {
428+
if info.has_main_fn {
429+
continue;
430+
}
421431
let mut iter = mac_call.mac.args.tokens.iter();
422432

423433
while let Some(token) = iter.next() {
@@ -437,7 +447,9 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
437447
}
438448
}
439449
}
440-
_ => {}
450+
_ => {
451+
has_non_items = true;
452+
}
441453
}
442454

443455
// Weirdly enough, the `Stmt` span doesn't include its attributes, so we need to
@@ -462,6 +474,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
462474
push_to_s(&mut info.crates, source, span, &mut prev_span_hi);
463475
}
464476
}
477+
if has_non_items {
478+
// FIXME: if `info.has_main_fn` is `true`, emit a warning here to mention that
479+
// this code will not be called.
480+
info.has_main_fn = false;
481+
}
465482
Ok(info)
466483
}
467484
Err(e) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use std::string::String;

tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
//@ compile-flags:--test
55
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
66
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
7-
//@ failure-status: 101
7+
//@ check-pass
88

99
/// <https://github.com/rust-lang/rust/issues/91014>
1010
///
1111
/// ```rust
12-
/// struct S {}; // unexpected semicolon after struct def
12+
/// struct S {};
1313
///
1414
/// fn main() {
1515
/// assert_eq!(0, 1);
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
11

22
running 1 test
3-
test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... FAILED
3+
test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... ok
44

5-
failures:
6-
7-
---- $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) stdout ----
8-
error: expected item, found `;`
9-
--> $DIR/failed-doctest-extra-semicolon-on-item.rs:12:12
10-
|
11-
LL | struct S {}; // unexpected semicolon after struct def
12-
| ^
13-
|
14-
= help: braced struct declarations are not followed by a semicolon
15-
help: remove this semicolon
16-
|
17-
LL - struct S {}; // unexpected semicolon after struct def
18-
LL + struct S {} // unexpected semicolon after struct def
19-
|
20-
21-
error: aborting due to 1 previous error
22-
23-
Couldn't compile the test.
24-
25-
failures:
26-
$DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11)
27-
28-
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
296

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test checks a corner case where the macro calls used to be skipped,
2+
// making them considered as statement, and therefore some cases where
3+
// `include!` macro was then put into a function body, making the doctest
4+
// compilation fail.
5+
6+
//@ compile-flags:--test
7+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
8+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
9+
//@ check-pass
10+
11+
//! ```
12+
//! include!("./auxiliary/macro-after-main.rs");
13+
//!
14+
//! fn main() {}
15+
//! eprintln!();
16+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/macro-after-main.rs - (line 11) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test ensures that if there is an expression alongside a `main`
2+
// function, it will not consider the entire code to be part of the `main`
3+
// function and will generate its own function to wrap everything.
4+
//
5+
// This is a regression test for:
6+
// * <https://github.com/rust-lang/rust/issues/140162>
7+
// * <https://github.com/rust-lang/rust/issues/139651>
8+
//@ compile-flags:--test
9+
//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
10+
//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
11+
//@ check-pass
12+
13+
#![crate_name = "foo"]
14+
15+
//! ```
16+
//! # if cfg!(miri) { return; }
17+
//! use std::ops::Deref;
18+
//!
19+
//! fn main() {
20+
//! println!("Hi!");
21+
//! }
22+
//! ```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/test-main-alongside-exprs.rs - (line 15) ... 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)