about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-03-27 17:43:29 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2025-03-27 17:43:29 +0100
commit5d274408d4b8b845f490912dcdadcc717f18fcca (patch)
tree5fc6db0f316a66ce413206048150e80b985377e4 /src
parent49f1e9cd8d31b65416408f4975feb5eded4d6158 (diff)
downloadrust-5d274408d4b8b845f490912dcdadcc717f18fcca.tar.gz
rust-5d274408d4b8b845f490912dcdadcc717f18fcca.zip
Only take outer attributes into account when generating content between first non-crate items and the crate items
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/doctest/make.rs8
-rw-r--r--src/librustdoc/doctest/tests.rs131
2 files changed, 19 insertions, 120 deletions
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index 55b57292b33..56b1e76ae8c 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -7,7 +7,7 @@ use std::sync::Arc;
 
 use rustc_ast::token::{Delimiter, TokenKind};
 use rustc_ast::tokenstream::TokenTree;
-use rustc_ast::{self as ast, HasAttrs, StmtKind};
+use rustc_ast::{self as ast, AttrStyle, HasAttrs, StmtKind};
 use rustc_errors::ColorConfig;
 use rustc_errors::emitter::stderr_destination;
 use rustc_parse::new_parser_from_source_str;
@@ -388,7 +388,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
             for attr in &item.attrs {
                 let attr_name = attr.name_or_empty();
 
-                if attr.style == ast::AttrStyle::Outer || not_crate_attrs.contains(&attr_name) {
+                if attr.style == AttrStyle::Outer || not_crate_attrs.contains(&attr_name) {
                     // There is one exception to these attributes:
                     // `#![allow(internal_features)]`. If this attribute is used, we need to
                     // consider it only as a crate-level attribute.
@@ -447,7 +447,9 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
                 // Weirdly enough, the `Stmt` span doesn't include its attributes, so we need to
                 // tweak the span to include the attributes as well.
                 let mut span = stmt.span;
-                if let Some(attr) = stmt.kind.attrs().first() {
+                if let Some(attr) =
+                    stmt.kind.attrs().iter().find(|attr| attr.style == AttrStyle::Outer)
+                {
                     span = span.with_lo(attr.span.lo());
                 }
                 if info.everything_else.is_empty()
diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs
index 949ed837db3..49add73e9d6 100644
--- a/src/librustdoc/doctest/tests.rs
+++ b/src/librustdoc/doctest/tests.rs
@@ -448,133 +448,30 @@ fn main() {}"
     assert_eq!((output, len), (expected, 1));
 }
 
+// This test ensures that the only attributes taken into account when we switch between
+// "crate level" content and the rest doesn't include inner attributes span, as it would
+// include part of the item and generate broken code.
 #[test]
-fn comments() {
+fn inner_attributes() {
     let opts = default_global_opts("");
-    let input = r##"
+    let input = r#"
 //! A doc comment that applies to the implicit anonymous module of this crate
 
 pub mod outer_module {
-
-    //!  - Inner line doc
     //!! - Still an inner line doc (but with a bang at the beginning)
-
-    /*!  - Inner block doc */
-    /*!! - Still an inner block doc (but with a bang at the beginning) */
-
-    //   - Only a comment
-    ///  - Outer line doc (exactly 3 slashes)
-    //// - Only a comment
-
-    /*   - Only a comment */
-    /**  - Outer block doc (exactly) 2 asterisks */
-    /*** - Only a comment */
-
-    pub mod inner_module {}
-
-    pub mod nested_comments {
-        /* In Rust /* we can /* nest comments */ */ */
-
-        // All three types of block comments can contain or be nested inside
-        // any other type:
-
-        /*   /* */  /** */  /*! */  */
-        /*!  /* */  /** */  /*! */  */
-        /**  /* */  /** */  /*! */  */
-        pub mod dummy_item {}
-    }
-
-    pub mod degenerate_cases {
-        // empty inner line doc
-        //!
-
-        // empty inner block doc
-        /*!*/
-
-        // empty line comment
-        //
-
-        // empty outer line doc
-        ///
-
-        // empty block comment
-        /**/
-
-        pub mod dummy_item {}
-
-        // empty 2-asterisk block isn't a doc block, it is a block comment
-        /***/
-
-    }
-
-    /* The next one isn't allowed because outer doc comments
-       require an item that will receive the doc */
-
-    /// Where is my item?
 }
-"##;
-    let expected = "
+"#;
+    let expected = "#![allow(unused)]
+
 //! A doc comment that applies to the implicit anonymous module of this crate
 
-pub mod outer_module {
 
-    //!  - Inner line doc
+fn main() {
+pub mod outer_module {
     //!! - Still an inner line doc (but with a bang at the beginning)
-
-    /*!  - Inner block doc */
-    /*!! - Still an inner block doc (but with a bang at the beginning) */
-
-    //   - Only a comment
-    ///  - Outer line doc (exactly 3 slashes)
-    //// - Only a comment
-
-    /*   - Only a comment */
-    /**  - Outer block doc (exactly) 2 asterisks */
-    /*** - Only a comment */
-
-    pub mod inner_module {}
-
-    pub mod nested_comments {
-        /* In Rust /* we can /* nest comments */ */ */
-
-        // All three types of block comments can contain or be nested inside
-        // any other type:
-
-        /*   /* */  /** */  /*! */  */
-        /*!  /* */  /** */  /*! */  */
-        /**  /* */  /** */  /*! */  */
-        pub mod dummy_item {}
-    }
-
-    pub mod degenerate_cases {
-        // empty inner line doc
-        //!
-
-        // empty inner block doc
-        /*!*/
-
-        // empty line comment
-        //
-
-        // empty outer line doc
-        ///
-
-        // empty block comment
-        /**/
-
-        pub mod dummy_item {}
-
-        // empty 2-asterisk block isn't a doc block, it is a block comment
-        /***/
-
-    }
-
-    /* The next one isn't allowed because outer doc comments
-       require an item that will receive the doc */
-
-    /// Where is my item?
 }
-".to_string();
+}"
+    .to_string();
     let (output, len) = make_test(input, None, false, &opts, None);
-    assert_eq!((output, len), (expected, 0));
-}
\ No newline at end of file
+    assert_eq!((output, len), (expected, 2));
+}