about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-04-27 00:12:32 +0200
committerJosh Stone <cuviper@gmail.com>2025-05-02 09:29:33 -0700
commit1f6a6b1008f1ebf6aa4a8cf1a0a00ebd7f401792 (patch)
treec6140771243f97185d0c1f28a6bf0c3701a64dab
parentc44a1d7c45709f0f79f5ffb981bc576844251e78 (diff)
downloadrust-1f6a6b1008f1ebf6aa4a8cf1a0a00ebd7f401792.tar.gz
rust-1f6a6b1008f1ebf6aa4a8cf1a0a00ebd7f401792.zip
Fix bad handling of macros if there is already a `main` function
(cherry picked from commit aa69e3a0cb8f1b2e086709a038baad6f39249150)
-rw-r--r--src/librustdoc/doctest/make.rs5
-rw-r--r--tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs1
-rw-r--r--tests/rustdoc-ui/doctest/macro-after-main.rs16
-rw-r--r--tests/rustdoc-ui/doctest/macro-after-main.stdout6
4 files changed, 27 insertions, 1 deletions
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index d0758c521e0..cdca25abec8 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -428,7 +428,10 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
                     // We assume that the macro calls will expand to item(s) even though they could
                     // expand to statements and expressions. And the simple fact that we're trying
                     // to retrieve a `main` function inside it is a terrible idea.
-                    StmtKind::MacCall(ref mac_call) if !info.has_main_fn => {
+                    StmtKind::MacCall(ref mac_call) => {
+                        if info.has_main_fn {
+                            continue;
+                        }
                         let mut iter = mac_call.mac.args.tokens.iter();
 
                         while let Some(token) = iter.next() {
diff --git a/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs b/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs
new file mode 100644
index 00000000000..ed7584b7425
--- /dev/null
+++ b/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs
@@ -0,0 +1 @@
+use std::string::String;
diff --git a/tests/rustdoc-ui/doctest/macro-after-main.rs b/tests/rustdoc-ui/doctest/macro-after-main.rs
new file mode 100644
index 00000000000..0a42343f1c2
--- /dev/null
+++ b/tests/rustdoc-ui/doctest/macro-after-main.rs
@@ -0,0 +1,16 @@
+// This test checks a corner case where the macro calls used to be skipped,
+// making them considered as statement, and therefore some cases where
+// `include!` macro was then put into a function body, making the doctest
+// compilation fail.
+
+//@ compile-flags:--test
+//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR"
+//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME"
+//@ check-pass
+
+//! ```
+//! include!("./auxiliary/macro-after-main.rs");
+//!
+//! fn main() {}
+//! eprintln!();
+//! ```
diff --git a/tests/rustdoc-ui/doctest/macro-after-main.stdout b/tests/rustdoc-ui/doctest/macro-after-main.stdout
new file mode 100644
index 00000000000..72ffe2b5a27
--- /dev/null
+++ b/tests/rustdoc-ui/doctest/macro-after-main.stdout
@@ -0,0 +1,6 @@
+
+running 1 test
+test $DIR/macro-after-main.rs - (line 11) ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
+