Skip to content

Commit 3259f2f

Browse files
Rollup merge of rust-lang#118417 - anforowicz:default-hidden-visibility, r=TaKO8Ki
Add unstable `-Zdefault-hidden-visibility` cmdline flag for `rustc`. The new flag has been described in the Major Change Proposal at rust-lang/compiler-team#656
2 parents 78df546 + 1a0829c commit 3259f2f

File tree

10 files changed

+64
-8
lines changed

10 files changed

+64
-8
lines changed

compiler/rustc_codegen_gcc/src/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn create_wrapper_function(
9090
.collect();
9191
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, from_name, false);
9292

93-
if tcx.sess.target.options.default_hidden_visibility {
93+
if tcx.sess.default_hidden_visibility() {
9494
#[cfg(feature="master")]
9595
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
9696
}

compiler/rustc_codegen_llvm/src/allocator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(crate) unsafe fn codegen(
7676
// __rust_alloc_error_handler_should_panic
7777
let name = OomStrategy::SYMBOL;
7878
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
79-
if tcx.sess.target.default_hidden_visibility {
79+
if tcx.sess.default_hidden_visibility() {
8080
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
8181
}
8282
let val = tcx.sess.opts.unstable_opts.oom.should_panic();
@@ -85,7 +85,7 @@ pub(crate) unsafe fn codegen(
8585

8686
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
8787
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
88-
if tcx.sess.target.default_hidden_visibility {
88+
if tcx.sess.default_hidden_visibility() {
8989
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
9090
}
9191
let llval = llvm::LLVMConstInt(i8, 0, False);
@@ -130,7 +130,7 @@ fn create_wrapper_function(
130130
None
131131
};
132132

133-
if tcx.sess.target.default_hidden_visibility {
133+
if tcx.sess.default_hidden_visibility() {
134134
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
135135
}
136136
if tcx.sess.must_emit_unwind_tables() {

compiler/rustc_codegen_llvm/src/declare.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
8484
fn_type: &'ll Type,
8585
) -> &'ll Value {
8686
// Declare C ABI functions with the visibility used by C by default.
87-
let visibility = if self.tcx.sess.target.default_hidden_visibility {
87+
let visibility = if self.tcx.sess.default_hidden_visibility() {
8888
llvm::Visibility::Hidden
8989
} else {
9090
llvm::Visibility::Default
@@ -107,7 +107,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
107107
unnamed: llvm::UnnamedAddr,
108108
fn_type: &'ll Type,
109109
) -> &'ll Value {
110-
let visibility = if self.tcx.sess.target.default_hidden_visibility {
110+
let visibility = if self.tcx.sess.default_hidden_visibility() {
111111
llvm::Visibility::Hidden
112112
} else {
113113
llvm::Visibility::Default

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ fn test_unstable_options_tracking_hash() {
748748
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
749749
tracked!(debug_info_for_profiling, true);
750750
tracked!(debug_macros, true);
751+
tracked!(default_hidden_visibility, Some(true));
751752
tracked!(dep_info_omit_d_target, true);
752753
tracked!(dual_proc_macros, true);
753754
tracked!(dwarf_version, Some(5));

compiler/rustc_monomorphize/src/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ fn mono_item_visibility<'tcx>(
883883
}
884884

885885
fn default_visibility(tcx: TyCtxt<'_>, id: DefId, is_generic: bool) -> Visibility {
886-
if !tcx.sess.target.default_hidden_visibility {
886+
if !tcx.sess.default_hidden_visibility() {
887887
return Visibility::Default;
888888
}
889889

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,8 @@ options! {
15481548
"compress debug info sections (none, zlib, zstd, default: none)"),
15491549
deduplicate_diagnostics: bool = (true, parse_bool, [UNTRACKED],
15501550
"deduplicate identical diagnostics (default: yes)"),
1551+
default_hidden_visibility: Option<bool> = (None, parse_opt_bool, [TRACKED],
1552+
"overrides the `default_hidden_visibility` setting of the target"),
15511553
dep_info_omit_d_target: bool = (false, parse_bool, [TRACKED],
15521554
"in dep-info output, omit targets for tracking dependencies of the dep-info files \
15531555
themselves (default: no)"),

compiler/rustc_session/src/session.rs

+8
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,14 @@ impl Session {
961961
termize::dimensions().map_or(default_column_width, |(w, _)| w)
962962
}
963963
}
964+
965+
/// Whether the default visibility of symbols should be "hidden" rather than "default".
966+
pub fn default_hidden_visibility(&self) -> bool {
967+
self.opts
968+
.unstable_opts
969+
.default_hidden_visibility
970+
.unwrap_or(self.target.options.default_hidden_visibility)
971+
}
964972
}
965973

966974
// JUSTIFICATION: defn of the suggested wrapper fns

compiler/rustc_target/src/spec/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,11 @@ pub struct TargetOptions {
20912091
pub no_builtins: bool,
20922092

20932093
/// The default visibility for symbols in this target should be "hidden"
2094-
/// rather than "default"
2094+
/// rather than "default".
2095+
///
2096+
/// This value typically shouldn't be accessed directly, but through
2097+
/// the `rustc_session::Session::default_hidden_visibility` method, which
2098+
/// allows `rustc` users to override this setting using cmdline flags.
20952099
pub default_hidden_visibility: bool,
20962100

20972101
/// Whether a .debug_gdb_scripts section will be added to the output object file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `default-hidden-visibility`
2+
3+
The tracking issue for this feature is: https://github.com/rust-lang/compiler-team/issues/656
4+
5+
------------------------
6+
7+
This flag can be used to override the target's
8+
[`default_hidden_visibility`](https://doc.rust-lang.org/beta/nightly-rustc/rustc_target/spec/struct.TargetOptions.html#structfield.default_hidden_visibility)
9+
setting.
10+
Using `-Zdefault_hidden_visibility=yes` is roughly equivalent to Clang's
11+
[`-fvisibility=hidden`](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fvisibility)
12+
cmdline flag.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Verifies that `Session::default_hidden_visibility` is affected when using the related cmdline
2+
// flag. This is a regression test for https://github.com/rust-lang/compiler-team/issues/656. See
3+
// also https://github.com/rust-lang/rust/issues/73295 and
4+
// https://github.com/rust-lang/rust/issues/37530.
5+
//
6+
// revisions:NONE YES NO
7+
//[YES] compile-flags: -Zdefault-hidden-visibility=yes
8+
//[NO] compile-flags: -Zdefault-hidden-visibility=no
9+
//
10+
// `compiler/rustc_target/src/spec/base/wasm.rs` has a different default value of
11+
// `default_hidden_visibility` - it wouldn't match the test expectations below.
12+
// [NONE] ignore-wasm32
13+
14+
// The test scenario is specifically about visibility of symbols exported out of dynamically linked
15+
// libraries.
16+
#![crate_type = "dylib"]
17+
18+
// The test scenario needs to use a Rust-public, but non-explicitly-exported symbol
19+
// (e.g. the test doesn't use `#[no_mangle]`, because currently it implies that
20+
// the symbol should be exported; we don't want that - we want to test the *default*
21+
// export setting instead).
22+
// .
23+
// We want to verify that the cmdline flag affects the visibility of this symbol:
24+
//
25+
// NONE: @{{.*}}default_hidden_visibility{{.*}}exported_symbol{{.*}} = constant
26+
// YES: @{{.*}}default_hidden_visibility{{.*}}exported_symbol{{.*}} = hidden constant
27+
// NO: @{{.*}}default_hidden_visibility{{.*}}exported_symbol{{.*}} = constant
28+
#[used]
29+
pub static exported_symbol: [u8; 6] = *b"foobar";

0 commit comments

Comments
 (0)