about summary refs log tree commit diff
path: root/tests/rustdoc
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-08-02 11:24:21 +0200
committerGitHub <noreply@github.com>2025-08-02 11:24:21 +0200
commit4eddebd42b87c688bb7ce8720b9da0d0e798f57a (patch)
tree0620957c1645f3a6910befe08ca64f24644d0c9a /tests/rustdoc
parent93917ea97c1b5b98b30fae1f30a5f653dacfa7cf (diff)
parent45231fa599583edc95843aaa4f23b12f762e01e7 (diff)
downloadrust-4eddebd42b87c688bb7ce8720b9da0d0e798f57a.tar.gz
rust-4eddebd42b87c688bb7ce8720b9da0d0e798f57a.zip
Rollup merge of #143662 - obi1kenobi:pg/unsafe-attribute-wrappers, r=t-rustdoc
[rustdoc] Display unsafe attrs with edition 2024 `unsafe()` wrappers.

Use Rust 2024 edition representation for unsafe attributes in rustdoc HTML:
- `#[no_mangle]` -> `#[unsafe(no_mangle)]`
- `#[export_name = "foo"]` -> `#[unsafe(export_name = "foo")]`
- `#[link_section = ".text"]` -> `#[unsafe(link_section = ".text")]`

The 2024 edition representation is used regardless of the crate's own edition. This ensures that Rustaceans don't have to learn the rules of an outdated edition (e.g. that `unsafe()` wasn't always necessary) in order to understand a crate's documentation.

After some looking through the `T-rustdoc` issues, I was not able to find an existing issue for this. Apologies if I missed it.

r? ``````@aDotInTheVoid``````
Diffstat (limited to 'tests/rustdoc')
-rw-r--r--tests/rustdoc/attribute-rendering.rs6
-rw-r--r--tests/rustdoc/attributes-2021-edition.rs14
-rw-r--r--tests/rustdoc/attributes-re-export-2021-edition.rs13
-rw-r--r--tests/rustdoc/attributes-re-export.rs2
-rw-r--r--tests/rustdoc/attributes.rs13
-rw-r--r--tests/rustdoc/reexport/reexport-attrs.rs6
6 files changed, 43 insertions, 11 deletions
diff --git a/tests/rustdoc/attribute-rendering.rs b/tests/rustdoc/attribute-rendering.rs
index 841533814c3..bf9b81077f3 100644
--- a/tests/rustdoc/attribute-rendering.rs
+++ b/tests/rustdoc/attribute-rendering.rs
@@ -1,7 +1,7 @@
 #![crate_name = "foo"]
 
 //@ has 'foo/fn.f.html'
-//@ has - //*[@'class="rust item-decl"]' '#[export_name = "f"] pub fn f()'
-#[export_name = "\
-f"]
+//@ has - //*[@'class="rust item-decl"]' '#[unsafe(export_name = "f")] pub fn f()'
+#[unsafe(export_name = "\
+f")]
 pub fn f() {}
diff --git a/tests/rustdoc/attributes-2021-edition.rs b/tests/rustdoc/attributes-2021-edition.rs
new file mode 100644
index 00000000000..b5028d8c852
--- /dev/null
+++ b/tests/rustdoc/attributes-2021-edition.rs
@@ -0,0 +1,14 @@
+//@ edition: 2021
+#![crate_name = "foo"]
+
+//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
+#[no_mangle]
+pub extern "C" fn f() {}
+
+//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "bar")]'
+#[export_name = "bar"]
+pub extern "C" fn g() {}
+
+//@ has foo/fn.example.html '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".text")]'
+#[link_section = ".text"]
+pub extern "C" fn example() {}
diff --git a/tests/rustdoc/attributes-re-export-2021-edition.rs b/tests/rustdoc/attributes-re-export-2021-edition.rs
new file mode 100644
index 00000000000..04ee6c273dd
--- /dev/null
+++ b/tests/rustdoc/attributes-re-export-2021-edition.rs
@@ -0,0 +1,13 @@
+// Tests that attributes are correctly copied onto a re-exported item.
+//@ edition:2024
+#![crate_name = "re_export"]
+
+//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
+pub use thingymod::thingy as thingy2;
+
+mod thingymod {
+    #[unsafe(no_mangle)]
+    pub fn thingy() {
+
+    }
+}
diff --git a/tests/rustdoc/attributes-re-export.rs b/tests/rustdoc/attributes-re-export.rs
index 458826ea8a3..820276f83c0 100644
--- a/tests/rustdoc/attributes-re-export.rs
+++ b/tests/rustdoc/attributes-re-export.rs
@@ -2,7 +2,7 @@
 //@ edition:2021
 #![crate_name = "re_export"]
 
-//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[no_mangle]'
+//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
 pub use thingymod::thingy as thingy2;
 
 mod thingymod {
diff --git a/tests/rustdoc/attributes.rs b/tests/rustdoc/attributes.rs
index e34468a88b1..34487a89127 100644
--- a/tests/rustdoc/attributes.rs
+++ b/tests/rustdoc/attributes.rs
@@ -1,13 +1,18 @@
+//@ edition: 2024
 #![crate_name = "foo"]
 
-//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[no_mangle]'
-#[no_mangle]
+//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
+#[unsafe(no_mangle)]
 pub extern "C" fn f() {}
 
-//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[export_name = "bar"]'
-#[export_name = "bar"]
+//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "bar")]'
+#[unsafe(export_name = "bar")]
 pub extern "C" fn g() {}
 
+//@ has foo/fn.example.html '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".text")]'
+#[unsafe(link_section = ".text")]
+pub extern "C" fn example() {}
+
 //@ has foo/struct.Repr.html '//pre[@class="rust item-decl"]' '#[repr(C, align(8))]'
 #[repr(C, align(8))]
 pub struct Repr;
diff --git a/tests/rustdoc/reexport/reexport-attrs.rs b/tests/rustdoc/reexport/reexport-attrs.rs
index 0ec645841f0..aec0a11c0c6 100644
--- a/tests/rustdoc/reexport/reexport-attrs.rs
+++ b/tests/rustdoc/reexport/reexport-attrs.rs
@@ -4,13 +4,13 @@
 
 extern crate reexports_attrs;
 
-//@ has 'foo/fn.f0.html' '//pre[@class="rust item-decl"]' '#[no_mangle]'
+//@ has 'foo/fn.f0.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
 pub use reexports_attrs::f0;
 
-//@ has 'foo/fn.f1.html' '//pre[@class="rust item-decl"]' '#[link_section = ".here"]'
+//@ has 'foo/fn.f1.html' '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".here")]'
 pub use reexports_attrs::f1;
 
-//@ has 'foo/fn.f2.html' '//pre[@class="rust item-decl"]' '#[export_name = "f2export"]'
+//@ has 'foo/fn.f2.html' '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "f2export")]'
 pub use reexports_attrs::f2;
 
 //@ has 'foo/enum.T0.html' '//pre[@class="rust item-decl"]' '#[repr(u8)]'