Skip to content

[rustdoc] Display unsafe attrs with edition 2024 unsafe() wrappers. #143662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,13 +768,13 @@ impl Item {
.iter()
.filter_map(|attr| match attr {
hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) => {
Some(format!("#[link_section = \"{name}\"]"))
Some(format!("#[unsafe(link_section = \"{name}\")]"))
}
hir::Attribute::Parsed(AttributeKind::NoMangle(..)) => {
Some("#[no_mangle]".to_string())
Some("#[unsafe(no_mangle)]".to_string())
}
hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) => {
Some(format!("#[export_name = \"{name}\"]"))
Some(format!("#[unsafe(export_name = \"{name}\")]"))
}
hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) => {
Some("#[non_exhaustive]".to_string())
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc/attribute-rendering.rs
Original file line number Diff line number Diff line change
@@ -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() {}
14 changes: 14 additions & 0 deletions tests/rustdoc/attributes-2021-edition.rs
Original file line number Diff line number Diff line change
@@ -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() {}
13 changes: 13 additions & 0 deletions tests/rustdoc/attributes-re-export-2021-edition.rs
Original file line number Diff line number Diff line change
@@ -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() {

}
}
2 changes: 1 addition & 1 deletion tests/rustdoc/attributes-re-export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions tests/rustdoc/attributes.rs
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 3 additions & 3 deletions tests/rustdoc/reexport/reexport-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]'
Expand Down
Loading