Skip to content

Commit 7c4d431

Browse files
committed
Change SymbolName::name to a &str.
This eliminates a bunch of `Symbol::intern()` and `Symbol::as_str()` calls, which is good, because they require locking the interner. Note that the unsafety in `from_cycle_error()` is identical to the unsafety on other adjacent impls.
1 parent 567ad74 commit 7c4d431

File tree

23 files changed

+89
-83
lines changed

23 files changed

+89
-83
lines changed

src/librustc_codegen_llvm/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value
3535
return llfn;
3636
}
3737

38-
let sym = tcx.symbol_name(instance).name.as_str();
38+
let sym = tcx.symbol_name(instance).name;
3939
debug!("get_fn({:?}: {:?}) => {}", instance, instance.monomorphic_ty(cx.tcx()), sym);
4040

4141
let fn_abi = FnAbi::of_instance(cx, instance, &[]);

src/librustc_codegen_llvm/common.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_codegen_ssa::traits::*;
1515
use rustc_middle::bug;
1616
use rustc_middle::mir::interpret::{Allocation, GlobalAlloc, Scalar};
1717
use rustc_middle::ty::layout::TyAndLayout;
18-
use rustc_span::symbol::Symbol;
18+
use rustc_middle::ty::SymbolName;
1919
use rustc_target::abi::{self, HasDataLayout, LayoutOf, Pointer, Size};
2020

2121
use libc::{c_char, c_uint};
@@ -105,17 +105,16 @@ impl CodegenCx<'ll, 'tcx> {
105105
bytes_in_context(self.llcx, bytes)
106106
}
107107

108-
fn const_cstr(&self, s: Symbol, null_terminated: bool) -> &'ll Value {
108+
fn const_cstr(&self, s: SymbolName<'tcx>, null_terminated: bool) -> &'ll Value {
109109
unsafe {
110110
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
111111
return llval;
112112
}
113113

114-
let s_str = s.as_str();
115114
let sc = llvm::LLVMConstStringInContext(
116115
self.llcx,
117-
s_str.as_ptr() as *const c_char,
118-
s_str.len() as c_uint,
116+
s.name.as_ptr() as *const c_char,
117+
s.name.len() as c_uint,
119118
!null_terminated as Bool,
120119
);
121120
let sym = self.generate_local_symbol_name("str");
@@ -202,8 +201,8 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
202201
unsafe { llvm::LLVMConstReal(t, val) }
203202
}
204203

205-
fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
206-
let len = s.as_str().len();
204+
fn const_str(&self, s: SymbolName<'tcx>) -> (&'ll Value, &'ll Value) {
205+
let len = s.name.len();
207206
let cs = consts::ptrcast(
208207
self.const_cstr(s, false),
209208
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),

src/librustc_codegen_llvm/consts.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::mir::interpret::{
1818
use rustc_middle::mir::mono::MonoItem;
1919
use rustc_middle::ty::{self, Instance, Ty};
2020
use rustc_middle::{bug, span_bug};
21-
use rustc_span::symbol::{sym, Symbol};
21+
use rustc_span::symbol::sym;
2222
use rustc_span::Span;
2323
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Primitive, Scalar, Size};
2424

@@ -107,11 +107,10 @@ fn check_and_apply_linkage(
107107
cx: &CodegenCx<'ll, 'tcx>,
108108
attrs: &CodegenFnAttrs,
109109
ty: Ty<'tcx>,
110-
sym: Symbol,
110+
sym: &str,
111111
span: Span,
112112
) -> &'ll Value {
113113
let llty = cx.layout_of(ty).llvm_type(cx);
114-
let sym = sym.as_str();
115114
if let Some(linkage) = attrs.linkage {
116115
debug!("get_static: sym={} linkage={:?}", sym, linkage);
117116

@@ -215,14 +214,13 @@ impl CodegenCx<'ll, 'tcx> {
215214
// FIXME: refactor this to work without accessing the HIR
216215
let (g, attrs) = match self.tcx.hir().get(id) {
217216
Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => {
218-
let sym_str = sym.as_str();
219-
if let Some(g) = self.get_declared_value(&sym_str) {
217+
if let Some(g) = self.get_declared_value(sym) {
220218
if self.val_ty(g) != self.type_ptr_to(llty) {
221219
span_bug!(span, "Conflicting types for static");
222220
}
223221
}
224222

225-
let g = self.declare_global(&sym_str, llty);
223+
let g = self.declare_global(sym, llty);
226224

227225
if !self.tcx.is_reachable_non_generic(def_id) {
228226
unsafe {

src/librustc_codegen_llvm/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ use rustc_data_structures::small_c_str::SmallCStr;
1616
use rustc_middle::bug;
1717
use rustc_middle::mir::mono::CodegenUnit;
1818
use rustc_middle::ty::layout::{HasParamEnv, LayoutError, TyAndLayout};
19-
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
19+
use rustc_middle::ty::{self, Instance, SymbolName, Ty, TyCtxt};
2020
use rustc_session::config::{CFGuard, CrateType, DebugInfo};
2121
use rustc_session::Session;
2222
use rustc_span::source_map::{Span, DUMMY_SP};
23-
use rustc_span::symbol::Symbol;
2423
use rustc_target::abi::{HasDataLayout, LayoutOf, PointeeInfo, Size, TargetDataLayout, VariantIdx};
2524
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
2625

@@ -47,7 +46,7 @@ pub struct CodegenCx<'ll, 'tcx> {
4746
pub vtables:
4847
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
4948
/// Cache of constant strings,
50-
pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
49+
pub const_cstr_cache: RefCell<FxHashMap<SymbolName<'tcx>, &'ll Value>>,
5150

5251
/// Reverse-direction for const ptrs cast from globals.
5352
///

src/librustc_codegen_llvm/debuginfo/metadata.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2468,8 +2468,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
24682468
let variable_type = Instance::mono(cx.tcx, def_id).monomorphic_ty(cx.tcx);
24692469
let type_metadata = type_metadata(cx, variable_type, span);
24702470
let var_name = tcx.item_name(def_id).as_str();
2471-
let linkage_name: &str =
2472-
&mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name.as_str();
2471+
let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
24732472
// When empty, linkage_name field is omitted,
24742473
// which is what we want for no_mangle statics
24752474
let linkage_name = if var_name == linkage_name { "" } else { linkage_name };

src/librustc_codegen_llvm/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
267267
let substs = instance.substs.truncate_to(self.tcx(), generics);
268268
let template_parameters = get_template_parameters(self, &generics, substs, &mut name);
269269

270-
let linkage_name: &str = &mangled_name_of_instance(self, instance).name.as_str();
270+
let linkage_name = &mangled_name_of_instance(self, instance).name;
271271
// Omit the linkage_name if it is the same as subprogram name.
272272
let linkage_name = if &name == linkage_name { "" } else { linkage_name };
273273

src/librustc_codegen_llvm/debuginfo/namespace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::definitions::DefPathData;
1212
pub fn mangled_name_of_instance<'a, 'tcx>(
1313
cx: &CodegenCx<'a, 'tcx>,
1414
instance: Instance<'tcx>,
15-
) -> ty::SymbolName {
15+
) -> ty::SymbolName<'tcx> {
1616
let tcx = cx.tcx;
1717
tcx.symbol_name(instance)
1818
}

src/librustc_codegen_llvm/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
196196
// and/or monomorphization invalidates these assumptions.
197197
let coverageinfo = tcx.coverageinfo(caller_instance.def_id());
198198
let mangled_fn = tcx.symbol_name(caller_instance);
199-
let (mangled_fn_name, _len_val) = self.const_str(mangled_fn.name);
199+
let (mangled_fn_name, _len_val) = self.const_str(mangled_fn);
200200
let hash = self.const_u64(coverageinfo.hash);
201201
let num_counters = self.const_u32(coverageinfo.num_counters);
202202
use coverage::count_code_region_args::*;

src/librustc_codegen_ssa/back/symbol_export.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1616
use rustc_middle::ty::Instance;
1717
use rustc_middle::ty::{SymbolName, TyCtxt};
1818
use rustc_session::config::{CrateType, SanitizerSet};
19-
use rustc_span::symbol::sym;
2019

2120
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
2221
crates_export_threshold(&tcx.sess.crate_types())
@@ -117,9 +116,9 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
117116
// In general though we won't link right if these
118117
// symbols are stripped, and LTO currently strips them.
119118
match name {
120-
sym::rust_eh_personality
121-
| sym::rust_eh_register_frames
122-
| sym::rust_eh_unregister_frames =>
119+
"rust_eh_personality"
120+
| "rust_eh_register_frames"
121+
| "rust_eh_unregister_frames" =>
123122
SymbolExportLevel::C,
124123
_ => SymbolExportLevel::Rust,
125124
}
@@ -177,15 +176,15 @@ fn exported_symbols_provider_local(
177176
.collect();
178177

179178
if tcx.entry_fn(LOCAL_CRATE).is_some() {
180-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new("main"));
179+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, "main"));
181180

182181
symbols.push((exported_symbol, SymbolExportLevel::C));
183182
}
184183

185184
if tcx.allocator_kind().is_some() {
186185
for method in ALLOCATOR_METHODS {
187186
let symbol_name = format!("__rust_{}", method.name);
188-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(&symbol_name));
187+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
189188

190189
symbols.push((exported_symbol, SymbolExportLevel::Rust));
191190
}
@@ -199,7 +198,7 @@ fn exported_symbols_provider_local(
199198
["__llvm_profile_raw_version", "__llvm_profile_filename"];
200199

201200
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
202-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(sym));
201+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
203202
(exported_symbol, SymbolExportLevel::C)
204203
}));
205204
}
@@ -209,14 +208,14 @@ fn exported_symbols_provider_local(
209208
const MSAN_WEAK_SYMBOLS: [&str; 2] = ["__msan_track_origins", "__msan_keep_going"];
210209

211210
symbols.extend(MSAN_WEAK_SYMBOLS.iter().map(|sym| {
212-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(sym));
211+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
213212
(exported_symbol, SymbolExportLevel::C)
214213
}));
215214
}
216215

217216
if tcx.sess.crate_types().contains(&CrateType::Dylib) {
218217
let symbol_name = metadata_symbol_name(tcx);
219-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(&symbol_name));
218+
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));
220219

221220
symbols.push((exported_symbol, SymbolExportLevel::Rust));
222221
}

src/librustc_codegen_ssa/mir/block.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::mir;
1616
use rustc_middle::mir::interpret::{AllocId, ConstValue, Pointer, Scalar};
1717
use rustc_middle::mir::AssertKind;
1818
use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt};
19-
use rustc_middle::ty::{self, Instance, Ty, TypeFoldable};
19+
use rustc_middle::ty::{self, Instance, SymbolName, Ty, TypeFoldable};
2020
use rustc_span::source_map::Span;
2121
use rustc_span::{sym, Symbol};
2222
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
@@ -423,8 +423,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
423423
(lang_items::PanicBoundsCheckFnLangItem, vec![index, len, location])
424424
}
425425
_ => {
426-
let msg_str = Symbol::intern(msg.description());
427-
let msg = bx.const_str(msg_str);
426+
let msg = bx.const_str(SymbolName::new(bx.tcx(), msg.description()));
428427
// It's `pub fn panic(expr: &str)`, with the wide reference being passed
429428
// as two arguments, and `#[track_caller]` adds an implicit third argument.
430429
(lang_items::PanicFnLangItem, vec![msg.0, msg.1, location])
@@ -487,7 +486,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
487486
} else {
488487
format!("attempted to leave type `{}` uninitialized, which is invalid", ty)
489488
};
490-
let msg = bx.const_str(Symbol::intern(&msg_str));
489+
let msg = bx.const_str(SymbolName::new(bx.tcx(), &msg_str));
491490
let location = self.get_caller_location(bx, span).immediate();
492491

493492
// Obtain the panic entry point.

src/librustc_codegen_ssa/mono_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
6464
cx.codegen_unit().name()
6565
);
6666

67-
let symbol_name = self.symbol_name(cx.tcx()).name.as_str();
67+
let symbol_name = self.symbol_name(cx.tcx()).name;
6868

6969
debug!("symbol {}", &symbol_name);
7070

src/librustc_codegen_ssa/traits/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::BackendTypes;
22
use crate::mir::place::PlaceRef;
33
use rustc_middle::mir::interpret::{Allocation, Scalar};
44
use rustc_middle::ty::layout::TyAndLayout;
5-
use rustc_span::Symbol;
5+
use rustc_middle::ty::SymbolName;
66
use rustc_target::abi::{self, Size};
77

88
pub trait ConstMethods<'tcx>: BackendTypes {
@@ -20,7 +20,7 @@ pub trait ConstMethods<'tcx>: BackendTypes {
2020
fn const_u8(&self, i: u8) -> Self::Value;
2121
fn const_real(&self, t: Self::Type, val: f64) -> Self::Value;
2222

23-
fn const_str(&self, s: Symbol) -> (Self::Value, Self::Value);
23+
fn const_str(&self, s: SymbolName<'tcx>) -> (Self::Value, Self::Value);
2424
fn const_struct(&self, elts: &[Self::Value], packed: bool) -> Self::Value;
2525

2626
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;

src/librustc_metadata/rmeta/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'tcx> EncodeContext<'tcx> {
558558
// Encode exported symbols info. This is prefetched in `encode_metadata` so we encode
559559
// this late to give the prefetching as much time as possible to complete.
560560
i = self.position();
561-
let exported_symbols = self.tcx.exported_symbols(LOCAL_CRATE);
561+
let exported_symbols = tcx.exported_symbols(LOCAL_CRATE);
562562
let exported_symbols = self.encode_exported_symbols(&exported_symbols);
563563
let exported_symbols_bytes = self.position() - i;
564564

@@ -622,7 +622,7 @@ impl<'tcx> EncodeContext<'tcx> {
622622

623623
let total_bytes = self.position();
624624

625-
if self.tcx.sess.meta_stats() {
625+
if tcx.sess.meta_stats() {
626626
let mut zero_bytes = 0;
627627
for e in self.opaque.data.iter() {
628628
if *e == 0 {
@@ -1541,7 +1541,7 @@ impl EncodeContext<'tcx> {
15411541
) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]> {
15421542
// The metadata symbol name is special. It should not show up in
15431543
// downstream crates.
1544-
let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx));
1544+
let metadata_symbol_name = SymbolName::new(self.tcx, &metadata_symbol_name(self.tcx));
15451545

15461546
self.lazy(
15471547
exported_symbols

src/librustc_middle/middle/exported_symbols.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ pub enum ExportedSymbol<'tcx> {
2626
NonGeneric(DefId),
2727
Generic(DefId, SubstsRef<'tcx>),
2828
DropGlue(Ty<'tcx>),
29-
NoDefId(ty::SymbolName),
29+
NoDefId(ty::SymbolName<'tcx>),
3030
}
3131

3232
impl<'tcx> ExportedSymbol<'tcx> {
3333
/// This is the symbol name of an instance if it is instantiated in the
3434
/// local crate.
35-
pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName {
35+
pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
3636
match *self {
3737
ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
3838
ExportedSymbol::Generic(def_id, substs) => {

src/librustc_middle/mir/mono.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl<'tcx> MonoItem<'tcx> {
6868
}
6969
}
7070

71-
pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName {
71+
pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> SymbolName<'tcx> {
7272
match *self {
7373
MonoItem::Fn(instance) => tcx.symbol_name(instance),
7474
MonoItem::Static(def_id) => tcx.symbol_name(Instance::mono(tcx, def_id)),
7575
MonoItem::GlobalAsm(hir_id) => {
7676
let def_id = tcx.hir().local_def_id(hir_id);
77-
SymbolName { name: Symbol::intern(&format!("global_asm_{:?}", def_id)) }
77+
SymbolName::new(tcx, &format!("global_asm_{:?}", def_id))
7878
}
7979
}
8080
}
@@ -335,9 +335,9 @@ impl<'tcx> CodegenUnit<'tcx> {
335335
// The codegen tests rely on items being process in the same order as
336336
// they appear in the file, so for local items, we sort by node_id first
337337
#[derive(PartialEq, Eq, PartialOrd, Ord)]
338-
pub struct ItemSortKey(Option<HirId>, SymbolName);
338+
pub struct ItemSortKey<'tcx>(Option<HirId>, SymbolName<'tcx>);
339339

340-
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey {
340+
fn item_sort_key<'tcx>(tcx: TyCtxt<'tcx>, item: MonoItem<'tcx>) -> ItemSortKey<'tcx> {
341341
ItemSortKey(
342342
match item {
343343
MonoItem::Fn(ref instance) => {

src/librustc_middle/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ rustc_queries! {
691691
/// The `symbol_name` query provides the symbol name for calling a
692692
/// given instance from the local crate. In particular, it will also
693693
/// look up the correct symbol name of instances from upstream crates.
694-
query symbol_name(key: ty::Instance<'tcx>) -> ty::SymbolName {
694+
query symbol_name(key: ty::Instance<'tcx>) -> ty::SymbolName<'tcx> {
695695
desc { "computing the symbol for `{}`", key }
696696
cache_on_disk_if { true }
697697
}

src/librustc_middle/ty/codec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ where
262262
Ok(decoder.tcx().adt_def(def_id))
263263
}
264264

265+
#[inline]
266+
pub fn decode_symbol_name<D>(decoder: &mut D) -> Result<ty::SymbolName<'tcx>, D::Error>
267+
where
268+
D: TyDecoder<'tcx>,
269+
{
270+
Ok(ty::SymbolName::new(decoder.tcx(), &decoder.read_str()?))
271+
}
272+
265273
#[inline]
266274
pub fn decode_existential_predicate_slice<D>(
267275
decoder: &mut D,
@@ -504,6 +512,13 @@ macro_rules! implement_ty_decoder {
504512
}
505513
}
506514

515+
impl<'_x, $($typaram),*> SpecializedDecoder<ty::SymbolName<'_x>>
516+
for $DecoderName<$($typaram),*> {
517+
fn specialized_decode(&mut self) -> Result<ty::SymbolName<'_x>, Self::Error> {
518+
unsafe { transmute(decode_symbol_name(self)) }
519+
}
520+
}
521+
507522
impl<'_x, '_y, $($typaram),*> SpecializedDecoder<&'_x ty::List<ty::ExistentialPredicate<'_y>>>
508523
for $DecoderName<$($typaram),*>
509524
where &'_x ty::List<ty::ExistentialPredicate<'_y>>: UseSpecializedDecodable {

0 commit comments

Comments
 (0)