-
-
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 |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| use rustc_hir::attrs::{CoverageAttrKind, OptimizeAttr, RtsanSetting, SanitizerSet, UsedBy}; | ||
| use std::str::FromStr; | ||
|
|
||
| use rustc_hir::attrs::{ | ||
| CoverageAttrKind, ExportVisibilityAttrValue, OptimizeAttr, RtsanSetting, SanitizerSet, UsedBy, | ||
| }; | ||
| use rustc_session::parse::feature_err; | ||
|
|
||
| use super::prelude::*; | ||
| use crate::session_diagnostics::{ | ||
| NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, NullOnObjcSelector, | ||
| ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral, | ||
| InvalidExportVisibility, NakedFunctionIncompatibleAttribute, NullOnExport, NullOnObjcClass, | ||
| NullOnObjcSelector, ObjcClassExpectedStringLiteral, ObjcSelectorExpectedStringLiteral, | ||
| }; | ||
| use crate::target_checking::Policy::AllowSilent; | ||
|
|
||
|
|
@@ -153,6 +157,36 @@ impl<S: Stage> SingleAttributeParser<S> for ExportNameParser { | |
| } | ||
| } | ||
|
|
||
| pub(crate) struct ExportVisibilityParser; | ||
|
|
||
| impl<S: Stage> SingleAttributeParser<S> for ExportVisibilityParser { | ||
| const PATH: &[rustc_span::Symbol] = &[sym::export_visibility]; | ||
| const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost; | ||
| const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error; | ||
| const ALLOWED_TARGETS: AllowedTargets = | ||
| AllowedTargets::AllowListWarnRest(&[Allow(Target::Fn), Allow(Target::Static)]); | ||
| const TEMPLATE: AttributeTemplate = template!(NameValueStr: "visibility"); | ||
|
|
||
| fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> { | ||
| let Some(nv) = args.name_value() else { | ||
| cx.expected_name_value(cx.attr_span, None); | ||
| return None; | ||
| }; | ||
| let Some(sv) = nv.value_as_str() else { | ||
| cx.expected_string_literal(nv.value_span, Some(nv.value_as_lit())); | ||
| return None; | ||
| }; | ||
| let Ok(visibility) = ExportVisibilityAttrValue::from_str(sv.as_str()) else { | ||
| cx.emit_err(InvalidExportVisibility { | ||
|
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 this use |
||
| span: nv.value_span, | ||
| unrecognized_visibility: sv.to_string(), | ||
| }); | ||
| return None; | ||
| }; | ||
| Some(AttributeKind::ExportVisibility { visibility, span: cx.attr_span }) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct ObjcClassParser; | ||
|
|
||
| impl<S: Stage> SingleAttributeParser<S> for ObjcClassParser { | ||
|
|
||
| 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, attr}; | ||
| 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}; | ||
|
|
@@ -132,6 +133,13 @@ fn process_builtin_attrs( | |
| codegen_fn_attrs.inline = *inline; | ||
| interesting_spans.inline = Some(*span); | ||
| } | ||
| AttributeKind::ExportVisibility { visibility, .. } => { | ||
| codegen_fn_attrs.export_visibility = Some(match visibility { | ||
| ExportVisibilityAttrValue::TargetDefault => { | ||
| tcx.sess.default_visibility().into() | ||
| } | ||
| }); | ||
| } | ||
| AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED, | ||
| AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align), | ||
| AttributeKind::LinkName { name, .. } => { | ||
|
|
@@ -610,6 +618,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? |
||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use std::borrow::Cow; | ||
| use std::path::PathBuf; | ||
| use std::str::FromStr; | ||
|
|
||
| pub use ReprAttr::*; | ||
| use rustc_abi::Align; | ||
|
|
@@ -187,6 +188,26 @@ impl Deprecation { | |
| } | ||
| } | ||
|
|
||
| /// Pre-parsed value of `#[export_visibility = ...]` attribute. | ||
| /// | ||
| /// In a future RFC we may consider adding support for `Hidden`, `Protected`, and/or | ||
| /// `Interposable`. | ||
| #[derive(Clone, Copy, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)] | ||
| pub enum ExportVisibilityAttrValue { | ||
| TargetDefault, | ||
| } | ||
|
|
||
| impl FromStr for ExportVisibilityAttrValue { | ||
| type Err = String; | ||
|
|
||
| fn from_str(s: &str) -> Result<ExportVisibilityAttrValue, Self::Err> { | ||
| match s { | ||
| "target_default" => Ok(ExportVisibilityAttrValue::TargetDefault), | ||
| _ => Err(format!("'{s}' is not a valid value for #[export_visibility = ...]",)), | ||
|
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 this string used anywhere? If not, could we make the Err type |
||
| } | ||
| } | ||
| } | ||
|
|
||
| /// There are three valid forms of the attribute: | ||
| /// `#[used]`, which is equivalent to `#[used(linker)]` on targets that support it, but `#[used(compiler)]` if not. | ||
| /// `#[used(compiler)]` | ||
|
|
@@ -766,6 +787,9 @@ pub enum AttributeKind { | |
| /// Represents `#[export_stable]`. | ||
| ExportStable, | ||
|
|
||
| /// Represents [`#[export_visibility = ...]`](https://github.com/rust-lang/rust/issues/151425) | ||
| ExportVisibility { visibility: ExportVisibilityAttrValue, span: Span }, | ||
|
|
||
| /// Represents `#[ffi_const]`. | ||
| FfiConst(Span), | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,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) |
||
| 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
... |
||
|
|
||
| /////////////////////////////////////////////////////////////////////// | ||
| // 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.
Is there a reason you choose
WarnResthere?I'd prefer for new attributes to default to
AllowedTargets::AllowList.