# No\_mangle crate-level attribute

**URL:** <https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344>\
**Category:** language design\
**Created:** [September 17, 2021, 8:52am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344 "2021-09-17T08:52:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentincestino](https://avatars.discourse-cdn.com/v4/letter/q/71e660/32.png) [@Quentincestino](https://internals.rust-lang.org/u/Quentincestino)\
**Post date:** [September 17, 2021, 8:52am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/1 "2021-09-17T08:52:48Z")

</div>

Hello, Rust is an incredible language and I think it has a great potential in many software industries, especially in the embedded's one. But at the moment rustc is producing a really awful assembly, and I understand we have no choice. The biggest problem for me is the name mangling. I think Rust lacks a no\_mangling crate-level attribute, which would disable any name mangling for all symbols that are not generated for tricky languages features. I guess it would be possible for example, a code like this:

```rust
struct Foo;

impl Foo {
    pub fn new() -> Self {
        Self {}
    }
}

```

to get something like:

```rust
Foo::new:
    ;instructions here

```

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [September 17, 2021, 8:59am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/2 "2021-09-17T08:59:46Z")

</div>

What benefit would this provide over tagging the specific items you need?

---

<div class="post-metadata">

**Author:** ![Quentincestino](https://avatars.discourse-cdn.com/v4/letter/q/71e660/32.png) [@Quentincestino](https://internals.rust-lang.org/u/Quentincestino)\
**Post date:** [September 17, 2021, 9:07am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/3 "2021-09-17T09:07:35Z")

</div>

Tagging everything directly would: 1/ Improve code readability, no no\_mangle attribute everywhere 2/ Way clearer assembly since the one generated by rustc is so chaotic

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [September 17, 2021, 10:26am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/4 "2021-09-17T10:26:49Z")

</div>

It would also come at a cost: by using a crate-level attribute, a reader might think that e.g. generic fns or methods will also not be mangled. But that is impossible, and thus the attribute would be misleading.

---

<div class="post-metadata">

**Author:** ![Quentincestino](https://avatars.discourse-cdn.com/v4/letter/q/71e660/32.png) [@Quentincestino](https://internals.rust-lang.org/u/Quentincestino)\
**Post date:** [September 17, 2021, 11:31am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/5 "2021-09-17T11:31:34Z")

</div>

There is way clearer ways for a generic function/method to be named than something like \_ZN4test4main17hd3e36f845d6357eeE

---

<div class="post-metadata">

**Author:** ![InfernoDeity](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/infernodeity/32/7588_2.png) [@InfernoDeity](https://internals.rust-lang.org/u/InfernoDeity)\
**Post date:** [September 17, 2021, 11:47am UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/6 "2021-09-17T11:47:25Z")

</div>

Does `_ZN4test4mainEv` work?

Joking asside, `Foo::bar` isn't a valid symbol name on all platforms. How would this work if the unmangled name could not possibly be used on the platform?

---

<div class="post-metadata">

**Author:** ![Quentincestino](https://avatars.discourse-cdn.com/v4/letter/q/71e660/32.png) [@Quentincestino](https://internals.rust-lang.org/u/Quentincestino)\
**Post date:** [September 17, 2021, 12:10pm UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/7 "2021-09-17T12:10:14Z")

</div>

It was an example, there is many ways to format symbols clearer than name mangling

---

<div class="post-metadata">

**Author:** ![InfernoDeity](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/infernodeity/32/7588_2.png) [@InfernoDeity](https://internals.rust-lang.org/u/InfernoDeity)\
**Post date:** [September 17, 2021, 12:17pm UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/8 "2021-09-17T12:17:35Z")

</div>

Symbols aren't supposed to be human readable by default. Mangled names are an efficient, backwards compatible, representation allowing symbols with the same stem names to differ. The `#[no_mangle]` attribute spceifically makes the symbol name equal to the stem or declaration name, you're propsing something that has different behaviour (it keeps name scoping intact, but in a more "readable form") If you have a better name mangling/name decoration scheme, that may be reasonable to pitch, but I'd argue that's a compiler-domain thing, not a language-domain thing (rust the language does not currently have any kind of rule imposing mangling).

If you want these names just for human use of generated assembly, this already can be done. I believe rustc has a demangle option, and many disassembles do.

---

<div class="post-metadata">

**Author:** ![sollyucko](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sollyucko/32/6101_2.png) [@sollyucko](https://internals.rust-lang.org/u/sollyucko)\
**Post date:** [September 17, 2021, 7:29pm UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/9 "2021-09-17T19:29:14Z")

</div>

Is your goal about linking or about readable assembly? If it's merely about readability, what about a tool similar to `c++filt` that demangles mangled symbol names in whatever file/stream you give it? (EDIT: [`rustfilt`](https://crates.io/crates/rustfilt))

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 16, 2021, 7:29pm UTC](https://internals.rust-lang.org/t/no-mangle-crate-level-attribute/15344/10 "2021-12-16T19:29:48Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
