about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-03-27 12:55:30 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-03-27 12:56:08 +0100
commitbe13105d737545a4a5f0aa0709342b4db8f2f1e3 (patch)
treeec24befe13ef0fb3126e845fba988d7b4dfa8cae
parent123ea25542ba00e92bf6d19084cad6e7a24453f0 (diff)
downloadrust-be13105d737545a4a5f0aa0709342b4db8f2f1e3.tar.gz
rust-be13105d737545a4a5f0aa0709342b4db8f2f1e3.zip
Improve comment and test for generated doctest with code comments
-rw-r--r--src/librustdoc/doctest/make.rs5
-rw-r--r--src/librustdoc/doctest/tests.rs24
2 files changed, 26 insertions, 3 deletions
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index 0ed2d37f74c..512343693bc 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -85,6 +85,8 @@ impl DocTestBuilder {
             maybe_crate_attrs,
         })) = result
         else {
+            // If the AST returned an error, we don't want this doctest to be merged with the
+            // others.
             return Self::invalid(
                 String::new(),
                 String::new(),
@@ -98,8 +100,7 @@ impl DocTestBuilder {
         debug!("crates:\n{crates}");
         debug!("after:\n{everything_else}");
 
-        // If the AST returned an error, we don't want this doctest to be merged with the
-        // others. Same if it contains `#[feature]` or `#[no_std]`.
+        // If it contains `#[feature]` or `#[no_std]`, we don't want it to be merged either.
         let can_be_merged = can_merge_doctests
             && !has_global_allocator
             && crate_attrs.is_empty()
diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs
index 59cc33558ed..4833ea04051 100644
--- a/src/librustdoc/doctest/tests.rs
+++ b/src/librustdoc/doctest/tests.rs
@@ -405,7 +405,9 @@ fn check_split_args() {
 
 #[test]
 fn comment_in_attrs() {
-    // if input already has a fn main, it should insert a space before it
+    // If there is an inline code comment after attributes, we need to ensure that
+    // a backline will be added to prevent generating code "inside" it (and thus generating)
+    // invalid code.
     let opts = default_global_opts("");
     let input = "\
 #![feature(rustdoc_internals)]
@@ -424,4 +426,24 @@ fn main() {
     .to_string();
     let (output, len) = make_test(input, None, false, &opts, None);
     assert_eq!((output, len), (expected, 2));
+
+    // And same, if there is a `main` function provided by the user, we ensure that it's
+    // correctly separated.
+    let input = "\
+#![feature(rustdoc_internals)]
+#![allow(internal_features)]
+#![doc(rust_logo)]
+//! This crate has the Rust(tm) branding on it.
+fn main() {}";
+    let expected = "\
+#![allow(unused)]
+#![feature(rustdoc_internals)]
+#![allow(internal_features)]
+#![doc(rust_logo)]
+//! This crate has the Rust(tm) branding on it.
+
+fn main() {}"
+        .to_string();
+    let (output, len) = make_test(input, None, false, &opts, None);
+    assert_eq!((output, len), (expected, 1));
 }