Skip to content

Commit 0e36868

Browse files
committed
Auto merge of #94638 - erikdesjardins:noextranull, r=nagisa
cleanup: remove unused ability to have LLVM null-terminate const strings (and the copied function in rustc_codegen_gcc) Noticed this while writing rust-lang/rust#94450 (comment). r? `@nagisa`
2 parents 92d1850 + a19138f commit 0e36868

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/common.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2626
bytes_in_context(self, bytes)
2727
}
2828

29-
fn const_cstr(&self, symbol: Symbol, _null_terminated: bool) -> LValue<'gcc> {
30-
// TODO(antoyo): handle null_terminated.
31-
if let Some(&value) = self.const_cstr_cache.borrow().get(&symbol) {
32-
return value;
33-
}
34-
35-
let global = self.global_string(symbol.as_str());
36-
37-
self.const_cstr_cache.borrow_mut().insert(symbol, global);
38-
global
39-
}
40-
4129
fn global_string(&self, string: &str) -> LValue<'gcc> {
4230
// TODO(antoyo): handle non-null-terminated strings.
4331
let string = self.context.new_string_literal(&*string);
@@ -171,8 +159,12 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
171159
}
172160

173161
fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
174-
let len = s.as_str().len();
175-
let cs = self.const_ptrcast(self.const_cstr(s, false).get_address(None),
162+
let s_str = s.as_str();
163+
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
164+
self.global_string(s_str)
165+
});
166+
let len = s_str.len();
167+
let cs = self.const_ptrcast(str_global.get_address(None),
176168
self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
177169
);
178170
(cs, self.const_usize(len as u64))

src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
8585
pub const_globals: RefCell<FxHashMap<RValue<'gcc>, RValue<'gcc>>>,
8686

8787
/// Cache of constant strings,
88-
pub const_cstr_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
88+
pub const_str_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
8989

9090
/// Cache of globals.
9191
pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,
@@ -195,7 +195,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
195195
function_instances: Default::default(),
196196
vtables: Default::default(),
197197
const_globals: Default::default(),
198-
const_cstr_cache: Default::default(),
198+
const_str_cache: Default::default(),
199199
globals: Default::default(),
200200
scalar_types: Default::default(),
201201
types: Default::default(),

0 commit comments

Comments
 (0)