Skip to content

Commit 0fd257d

Browse files
committed
Remove a couple of uses of interior mutability around statics
1 parent a4cb1c7 commit 0fd257d

File tree

8 files changed

+33
-28
lines changed

8 files changed

+33
-28
lines changed

compiler/rustc_codegen_gcc/src/consts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
6767
}
6868

6969
#[cfg_attr(not(feature = "master"), allow(unused_mut))]
70-
fn codegen_static(&self, def_id: DefId) {
70+
fn codegen_static(&mut self, def_id: DefId) {
7171
let attrs = self.tcx.codegen_fn_attrs(def_id);
7272

7373
let Ok((value, alloc)) = codegen_static_initializer(self, def_id) else {
@@ -162,11 +162,11 @@ impl<'gcc, 'tcx> StaticCodegenMethods for CodegenCx<'gcc, 'tcx> {
162162
}
163163

164164
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of i8*.
165-
fn add_used_global(&self, _global: RValue<'gcc>) {
165+
fn add_used_global(&mut self, _global: RValue<'gcc>) {
166166
// TODO(antoyo)
167167
}
168168

169-
fn add_compiler_used_global(&self, global: RValue<'gcc>) {
169+
fn add_compiler_used_global(&mut self, global: RValue<'gcc>) {
170170
// NOTE: seems like GCC does not make the distinction between compiler.used and used.
171171
self.add_used_global(global);
172172
}

compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,11 @@ pub(crate) fn compile_codegen_unit(
115115
}
116116

117117
// Create the llvm.used and llvm.compiler.used variables.
118-
if !cx.used_statics.borrow().is_empty() {
119-
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
118+
if !cx.used_statics.is_empty() {
119+
cx.create_used_variable_impl(c"llvm.used", &cx.used_statics);
120120
}
121-
if !cx.compiler_used_statics.borrow().is_empty() {
122-
cx.create_used_variable_impl(
123-
c"llvm.compiler.used",
124-
&*cx.compiler_used_statics.borrow(),
125-
);
121+
if !cx.compiler_used_statics.is_empty() {
122+
cx.create_used_variable_impl(c"llvm.compiler.used", &cx.compiler_used_statics);
126123
}
127124

128125
// Run replace-all-uses-with for statics that need it. This must

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl<'ll> CodegenCx<'ll, '_> {
411411
g
412412
}
413413

414-
fn codegen_static_item(&self, def_id: DefId) {
414+
fn codegen_static_item(&mut self, def_id: DefId) {
415415
unsafe {
416416
assert!(
417417
llvm::LLVMGetInitializer(
@@ -571,18 +571,18 @@ impl<'ll> StaticCodegenMethods for CodegenCx<'ll, '_> {
571571
self.const_pointercast(gv, self.type_ptr())
572572
}
573573

574-
fn codegen_static(&self, def_id: DefId) {
574+
fn codegen_static(&mut self, def_id: DefId) {
575575
self.codegen_static_item(def_id)
576576
}
577577

578578
/// Add a global value to a list to be stored in the `llvm.used` variable, an array of ptr.
579-
fn add_used_global(&self, global: &'ll Value) {
580-
self.used_statics.borrow_mut().push(global);
579+
fn add_used_global(&mut self, global: &'ll Value) {
580+
self.used_statics.push(global);
581581
}
582582

583583
/// Add a global value to a list to be stored in the `llvm.compiler.used` variable,
584584
/// an array of ptr.
585-
fn add_compiler_used_global(&self, global: &'ll Value) {
586-
self.compiler_used_statics.borrow_mut().push(global);
585+
fn add_compiler_used_global(&mut self, global: &'ll Value) {
586+
self.compiler_used_statics.push(global);
587587
}
588588
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Borrow;
22
use std::cell::{Cell, RefCell};
33
use std::ffi::{CStr, c_char, c_uint};
44
use std::marker::PhantomData;
5-
use std::ops::Deref;
5+
use std::ops::{Deref, DerefMut};
66
use std::str;
77

88
use rustc_abi::{HasDataLayout, Size, TargetDataLayout, VariantIdx};
@@ -77,6 +77,13 @@ impl<'ll, T: Borrow<SCx<'ll>>> Deref for GenericCx<'ll, T> {
7777
}
7878
}
7979

80+
impl<'ll, T: Borrow<SCx<'ll>>> DerefMut for GenericCx<'ll, T> {
81+
#[inline]
82+
fn deref_mut(&mut self) -> &mut Self::Target {
83+
&mut self.0
84+
}
85+
}
86+
8087
pub(crate) type SimpleCx<'ll> = GenericCx<'ll, SCx<'ll>>;
8188

8289
/// There is one `CodegenCx` per codegen unit. Each one has its own LLVM
@@ -110,11 +117,11 @@ pub(crate) struct FullCx<'ll, 'tcx> {
110117

111118
/// Statics that will be placed in the llvm.used variable
112119
/// See <https://llvm.org/docs/LangRef.html#the-llvm-used-global-variable> for details
113-
pub used_statics: RefCell<Vec<&'ll Value>>,
120+
pub used_statics: Vec<&'ll Value>,
114121

115122
/// Statics that will be placed in the llvm.compiler.used variable
116123
/// See <https://llvm.org/docs/LangRef.html#the-llvm-compiler-used-global-variable> for details
117-
pub compiler_used_statics: RefCell<Vec<&'ll Value>>,
124+
pub compiler_used_statics: Vec<&'ll Value>,
118125

119126
/// Mapping of non-scalar types to llvm types.
120127
pub type_lowering: RefCell<FxHashMap<(Ty<'tcx>, Option<VariantIdx>), &'ll Type>>,
@@ -606,8 +613,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
606613
const_str_cache: Default::default(),
607614
const_globals: Default::default(),
608615
statics_to_rauw: RefCell::new(Vec::new()),
609-
used_statics: RefCell::new(Vec::new()),
610-
compiler_used_statics: RefCell::new(Vec::new()),
616+
used_statics: Vec::new(),
617+
compiler_used_statics: Vec::new(),
611618
type_lowering: Default::default(),
612619
scalar_lltypes: Default::default(),
613620
coverage_cx,

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod unused;
2727
///
2828
/// Those sections are then read and understood by LLVM's `llvm-cov` tool,
2929
/// which is distributed in the `llvm-tools` rustup component.
30-
pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
30+
pub(crate) fn finalize(cx: &mut CodegenCx<'_, '_>) {
3131
let tcx = cx.tcx;
3232

3333
// Ensure that LLVM is using a version of the coverage mapping format that
@@ -62,6 +62,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
6262
.sorted_by_cached_key(|&instance| tcx.symbol_name(instance).name)
6363
.filter_map(|instance| prepare_covfun_record(tcx, instance, true))
6464
.collect::<Vec<_>>();
65+
drop(instances_used);
6566

6667
// In a single designated CGU, also prepare covfun records for functions
6768
// in this crate that were instrumented for coverage, but are unused.
@@ -206,7 +207,7 @@ impl VirtualFileMapping {
206207
/// Generates the contents of the covmap record for this CGU, which mostly
207208
/// consists of a header and a list of filenames. The record is then stored
208209
/// as a global variable in the `__llvm_covmap` section.
209-
fn generate_covmap_record<'ll>(cx: &CodegenCx<'ll, '_>, version: u32, filenames_buffer: &[u8]) {
210+
fn generate_covmap_record<'ll>(cx: &mut CodegenCx<'ll, '_>, version: u32, filenames_buffer: &[u8]) {
210211
// A covmap record consists of four target-endian u32 values, followed by
211212
// the encoded filenames table. Two of the header fields are unused in
212213
// modern versions of the LLVM coverage mapping format, and are always 0.

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn fill_region_tables<'tcx>(
181181
/// contains the function's coverage mapping data. The record is then stored
182182
/// as a global variable in the `__llvm_covfun` section.
183183
pub(crate) fn generate_covfun_record<'tcx>(
184-
cx: &CodegenCx<'_, 'tcx>,
184+
cx: &mut CodegenCx<'_, 'tcx>,
185185
global_file_table: &GlobalFileTable,
186186
covfun: &CovfunRecord<'tcx>,
187187
) {

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'ll, 'tcx> CguCoverageContext<'ll, 'tcx> {
5656
}
5757

5858
impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
59-
pub(crate) fn coverageinfo_finalize(&self) {
59+
pub(crate) fn coverageinfo_finalize(&mut self) {
6060
mapgen::finalize(self)
6161
}
6262

compiler/rustc_codegen_ssa/src/traits/statics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ use super::BackendTypes;
55

66
pub trait StaticCodegenMethods: BackendTypes {
77
fn static_addr_of(&self, cv: Self::Value, align: Align, kind: Option<&str>) -> Self::Value;
8-
fn codegen_static(&self, def_id: DefId);
8+
fn codegen_static(&mut self, def_id: DefId);
99

1010
/// Mark the given global value as "used", to prevent the compiler and linker from potentially
1111
/// removing a static variable that may otherwise appear unused.
12-
fn add_used_global(&self, global: Self::Value);
12+
fn add_used_global(&mut self, global: Self::Value);
1313

1414
/// Same as add_used_global(), but only prevent the compiler from potentially removing an
1515
/// otherwise unused symbol. The linker is still permitted to drop it.
1616
///
1717
/// This corresponds to the documented semantics of the `#[used]` attribute, although
1818
/// on some targets (non-ELF), we may use `add_used_global` for `#[used]` statics
1919
/// instead.
20-
fn add_compiler_used_global(&self, global: Self::Value);
20+
fn add_compiler_used_global(&mut self, global: Self::Value);
2121
}
2222

2323
pub trait StaticBuilderMethods: BackendTypes {

0 commit comments

Comments
 (0)