From 46dc4a3c2584f8b183a92044ed3b7b9f5dce4316 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 13:14:18 -0700 Subject: [PATCH 1/9] Unwrap no_mangle --- src/abi.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/abi.md b/src/abi.md index c0f7ed78a..f9f107751 100644 --- a/src/abi.md +++ b/src/abi.md @@ -65,17 +65,13 @@ r[abi.no_mangle] ## The `no_mangle` attribute r[abi.no_mangle.intro] -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. +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. r[abi.no_mangle.publicly-exported] -Additionally, the item will be publicly exported from the produced library or -object file, similar to the [`used` attribute](#the-used-attribute). +Additionally, the item will be publicly exported from the produced library or object file, similar to the [`used` attribute](#the-used-attribute). r[abi.no_mangle.unsafe] -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. +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. ```rust #[unsafe(no_mangle)] From bb0fb2833ffacafefe5efdbc0b9a4df591c25c9d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:21:50 -0700 Subject: [PATCH 2/9] Move abi.no_mangle.publicly-exported and tweak wording This moves it below to match the template of having behavioral rules after the syntax. --- src/abi.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/abi.md b/src/abi.md index f9f107751..c3ad084b9 100644 --- a/src/abi.md +++ b/src/abi.md @@ -67,9 +67,6 @@ r[abi.no_mangle] r[abi.no_mangle.intro] 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. -r[abi.no_mangle.publicly-exported] -Additionally, the item will be publicly exported from the produced library or object file, similar to the [`used` attribute](#the-used-attribute). - r[abi.no_mangle.unsafe] 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. @@ -82,6 +79,9 @@ r[abi.no_mangle.edition2024] > [!EDITION-2024] > Before the 2024 edition it is allowed to use the `no_mangle` attribute without the `unsafe` qualification. +r[abi.no_mangle.publicly-exported] +In addition to disabling name mangling, the item will be publicly exported from the produced library or object file, similar to the [`used` attribute](#the-used-attribute). + r[abi.link_section] ## The `link_section` attribute From b1499696ccf179dbe9bdbbc1c256f0ad0509c560 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:23:11 -0700 Subject: [PATCH 3/9] Move abi.no_mangle.unsafe and tweak wording This is to match the template order. --- src/abi.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/abi.md b/src/abi.md index c3ad084b9..e26d45e38 100644 --- a/src/abi.md +++ b/src/abi.md @@ -67,14 +67,14 @@ r[abi.no_mangle] r[abi.no_mangle.intro] 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. -r[abi.no_mangle.unsafe] -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. - ```rust #[unsafe(no_mangle)] extern "C" fn foo() {} ``` +r[abi.no_mangle.unsafe] +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. + r[abi.no_mangle.edition2024] > [!EDITION-2024] > Before the 2024 edition it is allowed to use the `no_mangle` attribute without the `unsafe` qualification. From 82760b3b0159d09a94eb4f1cc67bdbddcef14499 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:23:59 -0700 Subject: [PATCH 4/9] Update the no_mangle introduction It doesn't really work with any item, it is functions and statics. --- src/abi.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abi.md b/src/abi.md index e26d45e38..08000f6d2 100644 --- a/src/abi.md +++ b/src/abi.md @@ -65,7 +65,7 @@ r[abi.no_mangle] ## The `no_mangle` attribute r[abi.no_mangle.intro] -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. +The *`no_mangle` attribute* may be used on functions and statics to disable standard symbol name mangling. The symbol for the item will be the identifier of the item's name. ```rust #[unsafe(no_mangle)] From 25f95c0059ccd67e0e11ac6ac8f8db7214f8153c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:24:46 -0700 Subject: [PATCH 5/9] Add no_mangle template rules --- src/abi.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/abi.md b/src/abi.md index 08000f6d2..b3e90ce16 100644 --- a/src/abi.md +++ b/src/abi.md @@ -72,6 +72,34 @@ The *`no_mangle` attribute* may be used on functions and statics to disable stan extern "C" fn foo() {} ``` +r[abi.no_mangle.syntax] +The `no_mangle` attribute uses the [MetaWord] syntax and thus does not take any inputs. + +r[abi.no_mangle.allowed-positions] +The `no_mangle` attribute may only be applied to: + +- [Static items][items.static] +- [Free functions][items.fn] +- [Inherent associated functions][items.associated.fn] +- [Trait impl functions][items.impl.trait] + +It may not be used with a [closure]. + +> [!NOTE] +> `rustc` currently warns in other positions, but this may be rejected in the future. + + + + + +r[abi.no_mangle.duplicates] +Only the first instance of `no_mangle` on an item is honored. Subsequent `no_mangle` attributes are ignored. + +> [!NOTE] +> `rustc` currently warns on subsequent duplicate `no_mangle` attributes. + r[abi.no_mangle.unsafe] 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. @@ -133,6 +161,7 @@ r[abi.export_name.edition2024] [`static` items]: items/static-items.md [attribute]: attributes.md +[closure]: expr.closure [extern functions]: items/functions.md#extern-function-qualifier [external blocks]: items/external-blocks.md [function]: items/functions.md From 083fb715339144a0849d0385003ae1ca3e3aceb8 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:25:13 -0700 Subject: [PATCH 6/9] Add abi.no_mangle.export_name This specifies the conflict between no_mangle and export_name. --- src/abi.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/abi.md b/src/abi.md index b3e90ce16..9b26522d0 100644 --- a/src/abi.md +++ b/src/abi.md @@ -100,6 +100,9 @@ Only the first instance of `no_mangle` on an item is honored. Subsequent `no_man > [!NOTE] > `rustc` currently warns on subsequent duplicate `no_mangle` attributes. +r[abi.no_mangle.export_name] +If `no_mangle` is used with [`export_name`][abi.export_name], then the `export_name` is used instead. + r[abi.no_mangle.unsafe] 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. From 6914872d92e81f9c7e9e0b8f1c3e3ffe8f4b311f Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 14:25:32 -0700 Subject: [PATCH 7/9] Add abi.no_mangle.ascii-only This is a restriction that wasn't previously documented. --- src/abi.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/abi.md b/src/abi.md index 9b26522d0..3026fbe08 100644 --- a/src/abi.md +++ b/src/abi.md @@ -113,6 +113,9 @@ r[abi.no_mangle.edition2024] r[abi.no_mangle.publicly-exported] In addition to disabling name mangling, the item will be publicly exported from the produced library or object file, similar to the [`used` attribute](#the-used-attribute). +r[abi.no_mangle.ascii-only] +`no_mangle` may only be used on items with a name that only contains ASCII characters. + r[abi.link_section] ## The `link_section` attribute From 93068d3753490b060dc07e1b4d88c5e27d909e32 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Jun 2025 16:45:41 -0700 Subject: [PATCH 8/9] Move no_mangle example into block --- src/abi.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/abi.md b/src/abi.md index 3026fbe08..186eaaa83 100644 --- a/src/abi.md +++ b/src/abi.md @@ -67,10 +67,11 @@ r[abi.no_mangle] r[abi.no_mangle.intro] The *`no_mangle` attribute* may be used on functions and statics to disable standard symbol name mangling. The symbol for the item will be the identifier of the item's name. -```rust -#[unsafe(no_mangle)] -extern "C" fn foo() {} -``` +> [!EXAMPLE] +> ```rust +> #[unsafe(no_mangle)] +> extern "C" fn foo() {} +> ``` r[abi.no_mangle.syntax] The `no_mangle` attribute uses the [MetaWord] syntax and thus does not take any inputs. From 2aa36bbd554cf1f0afd0107bfb0ae8bc71d52d86 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 30 Jun 2025 14:47:58 -0700 Subject: [PATCH 9/9] Add abi.no_mangle.generic --- src/abi.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/abi.md b/src/abi.md index 186eaaa83..7094f0c16 100644 --- a/src/abi.md +++ b/src/abi.md @@ -117,6 +117,9 @@ In addition to disabling name mangling, the item will be publicly exported from r[abi.no_mangle.ascii-only] `no_mangle` may only be used on items with a name that only contains ASCII characters. +r[abi.no_mangle.generic] +`no_mangle` has no effect on generic items. + r[abi.link_section] ## The `link_section` attribute