-
-
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
3
commits 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.
+456
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this use
cx.expected_specific_argument_stringsinstead?(not sure)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also not sure. Right now the new attribute expects a string literal as an argument (e.g.
#[export_visibility = "target_default"]- this is the syntax that has been used so far by the RFC) . And it seems thatexpected_specific_argument_stringsis meant to be used with symbols rather than with string literals (e.g.#[export_visibility = target_default]). Do you think the new attribute should use the latter syntax?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the attribute should continue to use the
#[export_visibility = "target_default"]syntax.If
expected_specific_argument_stringsdoes not give the proper suggestions, could you make a new method that does?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies for the delay in replying. I appreciate the feedback and didn't forget about this comment. I just need more time to figure out how to do this right.
(My hesitation to use
expected_specific_argument_stringsmostly stems from not wanting to define possible attribute values as symbols. But I agree that having a helper that can take&[&str]of possible inputs would indeed be nice here. And I probably should introduce such a separate helper, but when doing this 1) I think I should also add a UI test that checks if a slightly misspelled value results in a proper fix/edit suggestion, and 2) I should spend some time trying to understand howexpected_specific_argument_stringsworks so that the new helper can reuse as much code as possible.)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From taking another look at the code, it seems like
expected_specific_argument_stringsdoes already suggest quotes in the error messages. It uses a symbol as an argument, but renders it as a quoted stringFor example, see: https://github.com/rust-lang/rust/blob/1dbe831e471710b71e886bd1f6a97bc47b17b1ea/tests/ui/coverage-attr/name-value.stderr
Regarding your "1" point, this would actually be really nice to have :3 But that is better to do in a separate PR