Skip to content

Commit 603630b

Browse files
authored
Merge pull request #1904 from ehuss/no_mangle
Update `no_mangle` to use the attribute template
2 parents 3c5add6 + 19802a4 commit 603630b

1 file changed

Lines changed: 53 additions & 10 deletions

File tree

src/abi.md

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,64 @@ r[abi.no_mangle]
6565
## The `no_mangle` attribute
6666
6767
r[abi.no_mangle.intro]
68-
The *`no_mangle` attribute* may be used on any [item] to disable standard symbol name mangling. The symbol for the item will be the identifier of the item's name.
68+
The *`no_mangle` [attribute]* disables the standard symbol name mangling on a [function] or [static]. The symbol for the item is the item's identifier.
6969
70-
r[abi.no_mangle.publicly-exported]
71-
Additionally, the item will be publicly exported from the produced library or object file, similar to the [`used` attribute](#the-used-attribute).
70+
> [!EXAMPLE]
71+
> ```rust
72+
> #[unsafe(no_mangle)]
73+
> extern "C" fn foo() {}
74+
> ```
7275
73-
r[abi.no_mangle.unsafe]
74-
This attribute is unsafe as an unmangled symbol may collide with another symbol with the same name (or with a well-known symbol), leading to undefined behavior.
76+
r[abi.no_mangle.syntax]
77+
The `no_mangle` attribute uses the [MetaWord] syntax.
7578
76-
```rust
77-
#[unsafe(no_mangle)]
78-
extern "C" fn foo() {}
79-
```
79+
r[abi.no_mangle.allowed-positions]
80+
The `no_mangle` attribute may only be applied to:
81+
82+
- [Static items][items.static]
83+
- [Free functions][items.fn]
84+
- [Inherent associated functions][items.associated.fn]
85+
- [Trait impl functions][items.impl.trait]
86+
87+
> [!NOTE]
88+
> `rustc` lints against use in other positions. This may become an error in the future.
89+
90+
<!-- TODO: Currently it works on a trait function with a body, but generates a warning about being phased out. how do we document that?
91+
https://github.com/rust-lang/rust/pull/86492#issuecomment-885682960
92+
-->
93+
94+
<!-- TODO: should this clarify that external block items are already unmangled?, and thus the attribute does nothing? Currently it is "phased out" warning. -->
95+
96+
r[abi.no_mangle.closures]
97+
The `no_mangle` attribute may not be used with a [closure].
98+
99+
r[abi.no_mangle.generics]
100+
The `no_mangle` attribute may not be used on an item with generic parameters.
101+
102+
r[abi.no_mangle.duplicates]
103+
Only the first use of `no_mangle` on an item has effect.
104+
105+
> [!NOTE]
106+
> `rustc` lints against any use following the first.
107+
108+
r[abi.no_mangle.export_name]
109+
When the `no_mangle` and [`export_name`][abi.export_name] attributes are both applied to the same item, the symbol name from `export_name` is used, and `no_mangle` has no effect.
110+
111+
> [!NOTE]
112+
> `rustc` lints against this combination.
113+
114+
r[abi.no_mangle.unsafe]
115+
The `no_mangle` attribute must be marked with [`unsafe`][attributes.safety] because an unmangled symbol may collide with another symbol with the same name (or with a well-known symbol), leading to undefined behavior.
80116
81117
r[abi.no_mangle.edition2024]
82118
> [!EDITION-2024]
83-
> Before the 2024 edition it is allowed to use the `no_mangle` attribute without the `unsafe` qualification.
119+
> Before the 2024 edition, it is allowed to use the `no_mangle` attribute without `unsafe`.
120+
121+
r[abi.no_mangle.publicly-exported]
122+
In addition to disabling name mangling, the `no_mangle` attribute causes the symbol to be publicly exported from the produced library or object file, similar to the [`used`][abi.used] attribute.
123+
124+
r[abi.no_mangle.ascii-only]
125+
The `no_mangle` attribute may only be used on items with a name that contains only ASCII characters.
84126
85127
r[abi.link_section]
86128
## The `link_section` attribute
@@ -141,6 +183,7 @@ r[abi.export_name.edition2024]
141183
> Before the 2024 edition it is allowed to use the `export_name` attribute without the `unsafe` qualification.
142184
143185
[attribute]: attributes.md
186+
[closure]: expr.closure
144187
[extern functions]: items/functions.md#extern-function-qualifier
145188
[external blocks]: items/external-blocks.md
146189
[function]: items/functions.md

0 commit comments

Comments
 (0)