#[export_visibility = ...]
attribute
#881
Labels
major-change
A proposal to make a major change to rustc
T-compiler
Add this label so rfcbot knows to poll the compiler team
Proposal
I propose to add
#[export_visibility = …]
attribute, which could be used to override the default visibility that is currently forced whenever using#[export_name = …]
or#[no_mangle]
.I propose that the new attribute can only be applied to items that are exported as
SymbolExportLevel::C
/contains_extern_indicator
.I propose that the visibility would be computed as follows:
#[export_name = …]
nor#[no_mangle]
): no changes = visibility is based on the platform, or on the-Zdefault-visibility=...
cmdline flag#[export_name = …]
or#[no_mangle]
without the new attribute: no changes = public export#[export_name = …]
or#[no_mangle]
with the new attribute:-Zdefault-visibility=...
(e.g.hidden
,protected
, orinterposable
) would result in this specific visibility.#[export_visibility = “inherit”]
value would result in using the target platform’s or cmdline flag’s visibility (basically this would mean: don’t let#[export_name]
/#[no_mangle]
affect the baseline visibility).Motivation
I believe that this proposal is a tractable way to resolve rust-lang/rust#73958. (The direct motivation for me is https://crbug.com/418073233 which has the same root cause.)
Feedback please?
I hope to get 3 kinds of feedback:
Process feedback. For example:
High-level / directional feedback. For example:
#[export_visibility = …]
-based approach?cxx
? (/cc @dtolnay)#[export_visibility = …]
incxx/src/symbols
can potentially be controlled by a new crate-level feature#[export_visibility = …]
in#[cxx::bridge]
-generated code may be a bit trickier: it is unclear 1) how to opt into emitting#[export_visibility = …]
(cxxbridge-macro
build-time crate feature?#[cxx::bridge(..., export_visibility = …)]
? Some other mechanism?) and 2) who/where is responsible for uttering the#![feature(export_visibility)]
incantation to opt into the proposed unstable feature.third_party/rust/chromium_crates_io/vendor/…
for an example of a quick&dirty way to test the new attribute incxx
.Low-level / technical feedback. For example:
export_visibility
?link_visibility
?symbol_visibility
? else?)Alternatives
Instead of adopting this proposal, we could also consider one of the following alternatives:
#[export_name = …]
and/or#[no_mangle]
at an edition boundary.rustc
rather than by an external linker such asldd
- I am now thinking that maybe I was wrong about this. But I still don’t understand why global, link-time reasoning is needed (e.g. considering “symbols reachable from the public API of the top-level crate”)#[export_name = …]
and#[no_mangle]
so that:-Zdefault-visibility=...
). If a publicly exported symbol was desirable, then this could be expressed with either A)#[used]
or B)#[export_visibility = …]
.#[export_name = …]
in a future Rust edition seems compatible with the#[export_visibility = …]
proposal. It seems that the visibility attribute would be useful in the current world and may also continue to be useful in the (hypothetical) new edition behavior (in both cases/editions overriding the default behavior of#[export_name = …]
may sometimes be desirable).-Zdefault-visibility-for-c-exports=...
#[export_name = …]
and#[no_mangle]
. Implementation-wise, I think that this could be done by changing the code herecxx
-generated symbols, but still 2) export some other symbols.#[link_name = …]
could be allowed on definitions (IIUC it is only allowed onextern “C”
declarations today). And then#[link_name = …]
could mean the same thing as#[export_name = …] #[export_visibility = “inherit”]
from this proposal.#[used]
attribute so that it can specify the scope at which a symbol should be retained / considered used. (This is inspired by the ideas shared in Ability from the top-level of the compilation not to mark #[no_mangle] items exported from shared library rust#73958 (comment))Other notes
unsafe
(i.e. it seems sufficient that#[export_name = …]
and#[no_mangle]
areunsafe
). But maybe this aspect should be treated as an open question (to be resolved before stabilization).Mentors or Reviewers
Maybe @bjorn3, who has participated in the discussion in rust-lang/rust#73958?
Process
The main points of the Major Change Process are as follows:
@rustbot second
.-C flag
, then full team check-off is required.@rfcbot fcp merge
on either the MCP or the PR.You can read more about Major Change Proposals on forge.
The text was updated successfully, but these errors were encountered: