Skip to content

Commit d265073

Browse files
committed
[rustdoc] Display unsafe attrs with edition 2024 unsafe() wrappers.
1 parent ab68b0f commit d265073

13 files changed

+51
-28
lines changed

src/librustdoc/clean/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,15 @@ impl Item {
767767
.iter()
768768
.filter_map(|attr| {
769769
if let hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) = attr {
770-
Some(format!("#[link_section = \"{name}\"]"))
770+
Some(format!("#[unsafe(link_section = \"{name}\")]"))
771771
}
772772
// NoMangle is special cased, as it appears in HTML output, and we want to show it in source form, not HIR printing.
773773
// It is also used by cargo-semver-checks.
774774
else if let hir::Attribute::Parsed(AttributeKind::NoMangle(..)) = attr {
775-
Some("#[no_mangle]".to_string())
775+
Some("#[unsafe(no_mangle)]".to_string())
776776
} else if let hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) = attr
777777
{
778-
Some(format!("#[export_name = \"{name}\"]"))
778+
Some(format!("#[unsafe(export_name = \"{name}\")]"))
779779
} else if let hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) = attr {
780780
Some("#[non_exhaustive]".to_string())
781781
} else if is_json {

src/rustdoc-json-types/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3737
// will instead cause conflicts. See #94591 for more. (This paragraph and the "Latest feature" line
3838
// are deliberately not in a doc comment, because they need not be in public docs.)
3939
//
40-
// Latest feature: Pretty printing of no_mangle attributes changed
41-
pub const FORMAT_VERSION: u32 = 53;
40+
// Latest feature: Added edition 2024 style `unsafe()` wrappers around attrs
41+
pub const FORMAT_VERSION: u32 = 54;
4242

4343
/// The root of the emitted JSON blob.
4444
///
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition: 2021
22
#![no_std]
33

4-
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(export_name = \"altered\")]"]'
55
#[export_name = "altered"]
66
pub extern "C" fn example() {}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//@ edition: 2024
22
#![no_std]
33

4-
// The representation of `#[unsafe(export_name = ..)]` in rustdoc in edition 2024
5-
// is still `#[export_name = ..]` without the `unsafe` attribute wrapper.
6-
7-
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(export_name = \"altered\")]"]'
85
#[unsafe(export_name = "altered")]
96
pub extern "C" fn example() {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition: 2021
22
#![no_std]
33

4-
//@ is "$.index[?(@.name=='example')].attrs" '["#[link_section = \".text\"]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(link_section = \".text\")]"]'
55
#[link_section = ".text"]
66
pub extern "C" fn example() {}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//@ edition: 2024
22
#![no_std]
33

4-
// Since the 2024 edition the link_section attribute must use the unsafe qualification.
5-
// However, the unsafe qualification is not shown by rustdoc.
6-
7-
//@ is "$.index[?(@.name=='example')].attrs" '["#[link_section = \".text\"]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(link_section = \".text\")]"]'
85
#[unsafe(link_section = ".text")]
96
pub extern "C" fn example() {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition: 2021
22
#![no_std]
33

4-
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(no_mangle)]"]'
55
#[no_mangle]
66
pub extern "C" fn example() {}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//@ edition: 2024
22
#![no_std]
33

4-
// The representation of `#[unsafe(no_mangle)]` in rustdoc in edition 2024
5-
// is still `#[no_mangle]` without the `unsafe` attribute wrapper.
6-
7-
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[unsafe(no_mangle)]"]'
85
#[unsafe(no_mangle)]
96
pub extern "C" fn example() {}

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() {}

0 commit comments

Comments
 (0)