Skip to content

Commit

Permalink
Add unstable -Zdefault-hidden-visibility cmdline flag for rustc.
Browse files Browse the repository at this point in the history
The new flag has been described in the Major Change Proposal at
rust-lang/compiler-team#656
  • Loading branch information
anforowicz committed Nov 17, 2023
1 parent 1be1e84 commit fc28049
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
tracked!(debug_info_for_profiling, true);
tracked!(debug_macros, true);
tracked!(default_hidden_visibility, None);
tracked!(dep_info_omit_d_target, true);
tracked!(dual_proc_macros, true);
tracked!(dwarf_version, Some(5));
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,8 @@ options! {
"compress debug info sections (none, zlib, zstd, default: none)"),
deduplicate_diagnostics: bool = (true, parse_bool, [UNTRACKED],
"deduplicate identical diagnostics (default: yes)"),
default_hidden_visibility: Option<bool> = (None, parse_opt_bool, [TRACKED],
"overrides the `default_hidden_visibility` setting of the target"),
dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED],
"in dep-info output, omit targets for tracking dependencies of the dep-info files \
themselves (default: no)"),
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,21 @@ impl Session {
termize::dimensions().map_or(default_column_width, |(w, _)| w)
}
}

pub fn default_hidden_visibility(&self) -> bool {
// FIXME:
//
// * Combine:

This comment has been minimized.

Copy link
@bridiver

This comment has been minimized.

Copy link
@bridiver

bridiver Nov 17, 2023

although obviously it would be better to cleanup the logic around this so it always checks the target and unstable_opts

// 1) `self.target.default_hidden_visibility` (or maybe
// `self.target.options.default_hidden_visibility`?)
// and
// 2) `self.opts.unstable_opts.default_hidden_visibility`
//
// * Grep the whole source for `default_hidden_visibility` and use
// `session.default_hidden_visibility()` instead of
// `sess.target.default_hidden_visibility`, etc.
todo!("DO NOT SUBMIT");
}
}

// JUSTIFICATION: defn of the suggested wrapper fns
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `default-hidden-visibility`

The tracking issue for this feature is: https://github.com/rust-lang/compiler-team/issues/656

------------------------

This flag can be used to override the target's
[`default_hidden_visibility`](https://doc.rust-lang.org/beta/nightly-rustc/rustc_target/spec/struct.TargetOptions.html#structfield.default_hidden_visibility)
setting.
Using `-Zdefault_hidden_visibility=yes` is roughly equivalent to Clang's
[`-fvisibility=hidden`](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fvisibility)
cmdline flag.
34 changes: 34 additions & 0 deletions tests/codegen/default-hidden-visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Verifies that `TargetOptions::default_hidden_visibility` is set when using the related cmdline
// flag. This is a regression test for https://github.com/rust-lang/compiler-team/issues/656.
// See also https://github.com/rust-lang/rust/issues/73295 and
// https://github.com/rust-lang/rust/issues/37530.
//
// revisions:NONE YES
//[YES] compile-flags: -Zdefault-hidden-visibility=yes
//
// FIXME: 3) also cover NO?

// The test scenario is specifically about visibility of symbols in Rust static libraries.
//
// This mimics the relevant part from https://github.com/rust-lang/rust/issues/73295 which
// says:
//
// > We build Rust code into these DSOs in the approved way, which is to aggregate a bunch of Rust
// > libraries (rlibs) into a separate Rust ***staticlib*** for each of the DSOs. (For example,
// > libbase_rust_deps.a and libservices_rust_deps.a). The final C++ linker links exactly one of
// > these staticlibs together with the C++ .a and .o in the final construction of the DSO.
#![crate_type = "staticlib"]

// The test scenario needs to use a public, but non-`#[no_mangle]` Rust symbol.
//
// We want to check the visibility of this symbol:
//
// FIXME: 1) confirm that this test file actually repros the problem at hand (I am not at all
// confident that it does)
// FIXME: 2) fix test expectations below ("internal" below seems unexpected for NONE scenario? no
// idea what should be the expectation for YES scenario)
//
// NONE: @_ZN25default_hidden_visibility15exported_symbol17hc5deee4a42a30cf5E = internal constant
// YES: ???????????
#[used]
pub static exported_symbol: [u8; 6] = *b"foobar";

0 comments on commit fc28049

Please sign in to comment.