-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,8 @@ use rustc_abi::{Align, ExternAbi}; | |
| use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode}; | ||
| use rustc_ast::{LitKind, MetaItem, MetaItemInner}; | ||
| use rustc_hir::attrs::{ | ||
| AttributeKind, EiiImplResolution, InlineAttr, Linkage, RtsanSetting, UsedBy, | ||
| AttributeKind, EiiImplResolution, ExportVisibilityAttrValue, InlineAttr, Linkage, RtsanSetting, | ||
| UsedBy, | ||
| }; | ||
| use rustc_hir::def::DefKind; | ||
| use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId}; | ||
|
|
@@ -75,6 +76,13 @@ fn process_builtin_attrs( | |
| match attr { | ||
| AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD, | ||
| AttributeKind::ExportName { name, .. } => codegen_fn_attrs.symbol_name = Some(*name), | ||
| AttributeKind::ExportVisibility { visibility, .. } => { | ||
| codegen_fn_attrs.export_visibility = Some(match visibility { | ||
| ExportVisibilityAttrValue::TargetDefault => { | ||
| tcx.sess.default_visibility().into() | ||
| } | ||
| }); | ||
| } | ||
| AttributeKind::Inline(inline, span) => { | ||
| codegen_fn_attrs.inline = *inline; | ||
| interesting_spans.inline = Some(*span); | ||
|
|
@@ -542,6 +550,25 @@ fn handle_lang_items( | |
| } | ||
| err.emit(); | ||
| } | ||
|
|
||
| if codegen_fn_attrs.export_visibility.is_some() { | ||
| let export_visibility_span = | ||
| find_attr!(attrs, AttributeKind::ExportVisibility{span, ..} => *span) | ||
| .unwrap_or_default(); | ||
| if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) { | ||
| tcx.dcx().span_err( | ||
| export_visibility_span, | ||
| "#[export_visibility = ...]` cannot be used on internal language items", | ||
| ); | ||
| } | ||
| if !codegen_fn_attrs.contains_extern_indicator() { | ||
| tcx.dcx().span_err( | ||
| export_visibility_span, | ||
| "#[export_visibility = ...]` will be ignored without \ | ||
| `export_name`, `no_mangle`, or similar attribute", | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make these struct errors?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please help me understand what you mean by "struct errors"? Can you point at a piece of code that I should mimic? Are you asking me to hoist the error string into
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In |
||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ impl AttributeKind { | |
| EiiImpls(..) => No, | ||
| ExportName { .. } => Yes, | ||
| ExportStable => No, | ||
| ExportVisibility { .. } => Yes, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is exporting this attribute needed? (Not sure, genuine question)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am also not sure. I think if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
| FfiConst(..) => No, | ||
| FfiPure(..) => No, | ||
| Fundamental { .. } => Yes, | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of how complex and target-specific everything linker-related is, I think an end-to-end test in To allow each object file type to be built on all platforms with just
//@ add-minicore).
|
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,92 @@ | ||||||||||||||
| // 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 revision is 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:HIDDEN PROTECTED | ||||||||||||||
| //@[HIDDEN] compile-flags: -Zdefault-visibility=hidden | ||||||||||||||
| //@[PROTECTED] compile-flags: -Zdefault-visibility=protected | ||||||||||||||
|
|
||||||||||||||
| // 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)] | ||||||||||||||
|
|
||||||||||||||
| // 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. If needed, | ||||||||||||||
| // additional targets can be covered by adding copies of this test file with | ||||||||||||||
| // a different `only-X` directive. | ||||||||||||||
| // | ||||||||||||||
| //@ only-x86_64-unknown-linux-gnu | ||||||||||||||
|
Comment on lines
+26
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to use //@ add-minicore
//@ compile-flags: --target x86_64-unknown-linux-gnu
#![feature(no_core)]
#![no_core]Possibly also roll this into the revisions so different targets wouldn't need different files. //@ revisions: LINUX-X86-HIDDEN LINUX-X86-PROTECTED
//@ [LINUX-X86-HIDDEN,LINUX-X86-PROTECTED] compile-flags: --target x86_64-unknown-linux-gnu
...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding And also another error that I don't quite understand: I think I'd rather avoid multiplying test expectations and revisions by multiple target platforms. I think focusing on x86_64 in this test is ok. FWIW I tried explaining this in the comment in the new test file: For now maybe I can leave things as-is here, but also try to add
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Could it just return a different int rather than
Huh, this definitely happened with rust/tests/auxiliary/minicore.rs Lines 58 to 63 in d222ddc
For reference https://github.com/rust-lang/rust/blob/d222ddc4d90743dfc1e53b610be8fc9d95893d2c/src/doc/rustc-dev-guide/src/tests/minicore.md
I agree that testing only a x86-64 target is good. But |
||||||||||||||
|
|
||||||||||||||
| /////////////////////////////////////////////////////////////////////// | ||||||||||||||
| // 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 = "test_static_no_attr")] | ||||||||||||||
| pub static TEST_STATIC_NO_ATTR: [u8; 7] = *b"static1"; | ||||||||||||||
|
|
||||||||||||||
| #[unsafe(export_name = "test_static_target_default")] | ||||||||||||||
| #[export_visibility = "target_default"] | ||||||||||||||
| pub static TESTED_STATIC_ATTR_ASKS_TO_TARGET_DEFAULT: [u8; 7] = *b"static2"; | ||||||||||||||
|
|
||||||||||||||
| #[unsafe(no_mangle)] | ||||||||||||||
| pub static test_static_no_mangle_no_attr: [u8; 10] = *b"no_mangle1"; | ||||||||||||||
|
|
||||||||||||||
| #[unsafe(no_mangle)] | ||||||||||||||
| #[export_visibility = "target_default"] | ||||||||||||||
| pub static test_static_no_mangle_target_default: [u8; 10] = *b"no_mangle2"; | ||||||||||||||
|
|
||||||||||||||
| // HIDDEN: @test_static_no_attr = local_unnamed_addr constant | ||||||||||||||
| // HIDDEN: @test_static_target_default = hidden local_unnamed_addr constant | ||||||||||||||
| // HIDDEN: @test_static_no_mangle_no_attr = local_unnamed_addr constant | ||||||||||||||
| // HIDDEN: @test_static_no_mangle_target_default = hidden local_unnamed_addr constant | ||||||||||||||
|
|
||||||||||||||
| // PROTECTED: @test_static_no_attr = local_unnamed_addr constant | ||||||||||||||
| // PROTECTED: @test_static_target_default = protected local_unnamed_addr constant | ||||||||||||||
| // PROTECTED: @test_static_no_mangle_no_attr = local_unnamed_addr constant | ||||||||||||||
| // PROTECTED: @test_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). | ||||||||||||||
| // | ||||||||||||||
| // We use `line!()` to ensure that each function has a unique body | ||||||||||||||
| // (and therefore that the functions won't get folded together). | ||||||||||||||
| // | ||||||||||||||
| // [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 { | ||||||||||||||
| line!() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[unsafe(export_name = "test_fn_target_default")] | ||||||||||||||
| #[export_visibility = "target_default"] | ||||||||||||||
| unsafe extern "C" fn test_fn_asks_for_target_default() -> u32 { | ||||||||||||||
| line!() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // HIDDEN: define noundef i32 @test_fn_no_attr | ||||||||||||||
| // HIDDEN: define hidden noundef i32 @test_fn_target_default | ||||||||||||||
|
|
||||||||||||||
| // PROTECTED: define noundef i32 @test_fn_no_attr | ||||||||||||||
| // PROTECTED: define protected noundef i32 @test_fn_target_default | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // This test verfies that `#[export_visibility = ...]` will report an error | ||
| // when applied to an item that also has `#[rustc_std_internal_symbol]` | ||
| // attribute. | ||
| #![feature(export_visibility)] | ||
| #![feature(rustc_attrs)] | ||
| #[export_visibility = "target_default"] | ||
| //~^ERROR: #[export_visibility = ...]` cannot be used on internal language items | ||
| #[rustc_std_internal_symbol] | ||
| pub static TESTED_STATIC: [u8; 6] = *b"foobar"; | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| error: #[export_visibility = ...]` cannot be used on internal language items | ||
| --> $DIR/export-visibility-with-rustc-std-internal-symbol.rs:6:1 | ||
| | | ||
| LL | #[export_visibility = "target_default"] | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
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?