-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Add new unstable attribute: #[export_visibility = ...].
#151431
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
anforowicz
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
anforowicz:export-visibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
anforowicz marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Verifies that `#[export_visibility = ...]` can override the visibility | ||
| // that is normally implied by `#[export_name]` or `#[no_mangle]`. | ||
| // | ||
| // High-level test expectations for items with `#[export_name = ...]` | ||
| // (or with `#[no_mangle]`) and: | ||
| // | ||
| // * Without `#[export_visibility = ...]` => public | ||
| // * `#[export_visibility = "target_default"]` => value inherited from the target | ||
| // platform or from the `-Zdefault-visibility=...` command-line flag | ||
| // (this expectation depends on whether the `...-HIDDEN` vs `...-PROTECTED` | ||
| // test revisions are used). | ||
| // | ||
| // Note that what we call "public" in the expectations above is also referred | ||
| // to as "default" in LLVM docs - see | ||
| // https://llvm.org/docs/LangRef.html#visibility-styles | ||
|
|
||
| //@ revisions: LINUX-X86-HIDDEN LINUX-X86-PROTECTED | ||
| //@[LINUX-X86-HIDDEN] compile-flags: -Zdefault-visibility=hidden | ||
| //@[LINUX-X86-PROTECTED] compile-flags: -Zdefault-visibility=protected | ||
|
|
||
| // Exact LLVM IR differs depending on the target triple (e.g. `hidden constant` | ||
| // vs `internal constant` vs `constant`). Because of this, we only apply the | ||
| // specific test expectations below to one specific target triple. | ||
| // | ||
| // Note that `tests/run-make/cdylib-export-visibility` provides similar | ||
| // test coverage, but in an LLVM-IR-agnostic / platform-agnostic way. | ||
| //@[LINUX-X86-HIDDEN] needs-llvm-components: x86 | ||
| //@[LINUX-X86-HIDDEN] compile-flags: --target x86_64-unknown-linux-gnu | ||
| //@[LINUX-X86-PROTECTED] needs-llvm-components: x86 | ||
| //@[LINUX-X86-PROTECTED] compile-flags: --target x86_64-unknown-linux-gnu | ||
|
|
||
| // This test focuses on rlib to exercise the scenario described in | ||
| // https://github.com/rust-lang/rust/issues/73958#issuecomment-2891711649 | ||
| #![crate_type = "rlib"] | ||
| #![feature(export_visibility)] | ||
| // Relying on `minicore` makes it easier to run the test, even if the host is | ||
| // not a linux-x86 machine. | ||
| //@ add-minicore | ||
| //@ edition: 2024 | ||
| #![feature(no_core)] | ||
| #![no_core] | ||
| use minicore::*; | ||
|
|
||
| /////////////////////////////////////////////////////////////////////// | ||
| // The tests below focus on how `#[export_visibility = ...]` works for | ||
| // a `static`. The tests are based on similar tests in | ||
| // `tests/codegen/default-visibility.rs` | ||
|
|
||
| #[unsafe(export_name = "static_export_name_no_attr")] | ||
| pub static TEST_STATIC_NO_ATTR: u32 = 1101; | ||
|
|
||
| #[unsafe(export_name = "static_export_name_target_default")] | ||
| #[export_visibility = "target_default"] | ||
| pub static TESTED_STATIC_ATTR_ASKS_TO_TARGET_DEFAULT: u32 = 1102; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub static static_no_mangle_no_attr: u32 = 1201; | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| #[export_visibility = "target_default"] | ||
| pub static static_no_mangle_target_default: u32 = 1202; | ||
|
|
||
| // LINUX-X86-HIDDEN: @static_export_name_no_attr = local_unnamed_addr constant | ||
| // LINUX-X86-HIDDEN: @static_export_name_target_default = hidden local_unnamed_addr constant | ||
| // LINUX-X86-HIDDEN: @static_no_mangle_no_attr = local_unnamed_addr constant | ||
| // LINUX-X86-HIDDEN: @static_no_mangle_target_default = hidden local_unnamed_addr constant | ||
|
|
||
| // LINUX-X86-PROTECTED: @static_export_name_no_attr = local_unnamed_addr constant | ||
| // LINUX-X86-PROTECTED: @static_export_name_target_default = protected local_unnamed_addr constant | ||
| // LINUX-X86-PROTECTED: @static_no_mangle_no_attr = local_unnamed_addr constant | ||
| // LINUX-X86-PROTECTED: @static_no_mangle_target_default = protected local_unnamed_addr constant | ||
|
|
||
| /////////////////////////////////////////////////////////////////////// | ||
| // The tests below focus on how `#[export_visibility = ...]` works for | ||
| // a `fn`. | ||
| // | ||
| // The tests below try to mimics how `cxx` exports known/hardcoded helpers (e.g. | ||
| // `cxxbridge1$string$drop` [1]) as well as build-time-generated thunks (e.g. | ||
| // `serde_json_lenient$cxxbridge1$decode_json` from https://crbug.com/418073233#comment7). | ||
| // | ||
| // [1] | ||
| // https://github.com/dtolnay/cxx/blob/ebdd6a0c63ae10dc5224ed21970b7a0504657434/src/symbols/rust_string.rs#L83-L86 | ||
|
|
||
| #[unsafe(export_name = "test_fn_no_attr")] | ||
| unsafe extern "C" fn test_fn_no_attr() -> u32 { | ||
| // We return a unique integer to ensure that each function has a unique body | ||
| // and therefore that identical code folding (ICF) won't fold the functions | ||
| // when linking. | ||
| 2001 | ||
| } | ||
|
|
||
| #[unsafe(export_name = "test_fn_target_default")] | ||
| #[export_visibility = "target_default"] | ||
| unsafe extern "C" fn test_fn_asks_for_target_default() -> u32 { | ||
| 2002 | ||
| } | ||
|
|
||
| // LINUX-X86-HIDDEN: define noundef i32 @test_fn_no_attr | ||
| // LINUX-X86-HIDDEN: define hidden noundef i32 @test_fn_target_default | ||
|
|
||
| // LINUX-X86-PROTECTED: define noundef i32 @test_fn_no_attr | ||
| // LINUX-X86-PROTECTED: define protected noundef i32 @test_fn_target_default |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #![crate_type = "cdylib"] | ||
| #![feature(export_visibility)] | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| unsafe extern "C" fn test_fn_no_attr() -> u32 { | ||
| // Using `line!()` means that the functions return different results | ||
| // and therefore identical code folding (ICF) in the linker won't apply. | ||
| line!() | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| #[export_visibility = "target_default"] | ||
| unsafe extern "C" fn test_fn_export_visibility_asks_for_target_default() -> u32 { | ||
| line!() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.