Skip to content

Commit 47ec2e7

Browse files
authored
Unrolled build for #143662
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``````
2 parents a65b04d + 45231fa commit 47ec2e7

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,13 @@ impl Item {
763763
.iter()
764764
.filter_map(|attr| match attr {
765765
hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) => {
766-
Some(format!("#[link_section = \"{name}\"]"))
766+
Some(format!("#[unsafe(link_section = \"{name}\")]"))
767767
}
768768
hir::Attribute::Parsed(AttributeKind::NoMangle(..)) => {
769-
Some("#[no_mangle]".to_string())
769+
Some("#[unsafe(no_mangle)]".to_string())
770770
}
771771
hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) => {
772-
Some(format!("#[export_name = \"{name}\"]"))
772+
Some(format!("#[unsafe(export_name = \"{name}\")]"))
773773
}
774774
hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) => {
775775
Some("#[non_exhaustive]".to_string())

tests/rustdoc/attribute-rendering.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![crate_name = "foo"]
22

33
//@ has 'foo/fn.f.html'
4-
//@ has - //*[@'class="rust item-decl"]' '#[export_name = "f"] pub fn f()'
5-
#[export_name = "\
6-
f"]
4+
//@ has - //*[@'class="rust item-decl"]' '#[unsafe(export_name = "f")] pub fn f()'
5+
#[unsafe(export_name = "\
6+
f")]
77
pub fn f() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ edition: 2021
2+
#![crate_name = "foo"]
3+
4+
//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
5+
#[no_mangle]
6+
pub extern "C" fn f() {}
7+
8+
//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "bar")]'
9+
#[export_name = "bar"]
10+
pub extern "C" fn g() {}
11+
12+
//@ has foo/fn.example.html '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".text")]'
13+
#[link_section = ".text"]
14+
pub extern "C" fn example() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Tests that attributes are correctly copied onto a re-exported item.
2+
//@ edition:2024
3+
#![crate_name = "re_export"]
4+
5+
//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
6+
pub use thingymod::thingy as thingy2;
7+
8+
mod thingymod {
9+
#[unsafe(no_mangle)]
10+
pub fn thingy() {
11+
12+
}
13+
}

tests/rustdoc/attributes-re-export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ edition:2021
33
#![crate_name = "re_export"]
44

5-
//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[no_mangle]'
5+
//@ has 're_export/fn.thingy2.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
66
pub use thingymod::thingy as thingy2;
77

88
mod thingymod {

tests/rustdoc/attributes.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
//@ edition: 2024
12
#![crate_name = "foo"]
23

3-
//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[no_mangle]'
4-
#[no_mangle]
4+
//@ has foo/fn.f.html '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
5+
#[unsafe(no_mangle)]
56
pub extern "C" fn f() {}
67

7-
//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[export_name = "bar"]'
8-
#[export_name = "bar"]
8+
//@ has foo/fn.g.html '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "bar")]'
9+
#[unsafe(export_name = "bar")]
910
pub extern "C" fn g() {}
1011

12+
//@ has foo/fn.example.html '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".text")]'
13+
#[unsafe(link_section = ".text")]
14+
pub extern "C" fn example() {}
15+
1116
//@ has foo/struct.Repr.html '//pre[@class="rust item-decl"]' '#[repr(C, align(8))]'
1217
#[repr(C, align(8))]
1318
pub struct Repr;

tests/rustdoc/reexport/reexport-attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
extern crate reexports_attrs;
66

7-
//@ has 'foo/fn.f0.html' '//pre[@class="rust item-decl"]' '#[no_mangle]'
7+
//@ has 'foo/fn.f0.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
88
pub use reexports_attrs::f0;
99

10-
//@ has 'foo/fn.f1.html' '//pre[@class="rust item-decl"]' '#[link_section = ".here"]'
10+
//@ has 'foo/fn.f1.html' '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".here")]'
1111
pub use reexports_attrs::f1;
1212

13-
//@ has 'foo/fn.f2.html' '//pre[@class="rust item-decl"]' '#[export_name = "f2export"]'
13+
//@ has 'foo/fn.f2.html' '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "f2export")]'
1414
pub use reexports_attrs::f2;
1515

1616
//@ has 'foo/enum.T0.html' '//pre[@class="rust item-decl"]' '#[repr(u8)]'

0 commit comments

Comments
 (0)