Skip to content

#[used(compiler)] and #[used(linker)] should work on functions. #94348

Open
@AzureMarker

Description

@AzureMarker

Related to #93798
cc @cynecx since you did the implementation.
cc @Meziu and @ian-h-chamberlain

The #[used(compiler)] and #[used(linker)] should work on functions. They weren't explicitly one of the main motivating use cases brought up in #47384, but expanding them to support functions would be very beneficial. For example, in https://github.com/Meziu/pthread-3ds and https://github.com/Meziu/rust-linker-fix-3ds we have to have an empty "init" function just so the exported functions don't get thrown out.

I was expecting these annotations to fix our problem, so I was excited to try them out when I heard they hit nightly. However, the following fails to compile:

#![feature(used_with_arg)]

#[used(compiler)]
#[no_mangle]
pub unsafe extern "C" fn foo() {}

playground link

error: attribute must be applied to a `static` variable
 --> src/lib.rs:3:1
  |
3 | #[used(compiler)]
  | ^^^^^^^^^^^^^^^^^

Interestingly, changing to #[used(linker)] results in an extra message (Edit: fixed by #94377):

error: `used(compiler)` and `used(linker)` can't be used together
 --> src/lib.rs:3:1
  |
3 | #[used(linker)]
  | ^^^^^^^^^^^^^^^
4 | #[no_mangle]
  | ^^^^^^^^^^^^

Does no_mangle imply used(compiler)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesF-used_with_arg`#![feature(used_with_arg)]`T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions