Skip to content

Commit d9e8d62

Browse files
committed
Auto merge of rust-lang#74214 - nnethercote:change-SymbolName-name, r=eddyb
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. r? @eddyb
2 parents c714eae + a1b8540 commit d9e8d62

File tree

19 files changed

+76
-67
lines changed

19 files changed

+76
-67
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/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/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(Symbol::intern(mangled_fn.name));
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/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_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 {

src/librustc_middle/ty/mod.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use std::hash::{Hash, Hasher};
5050
use std::marker::PhantomData;
5151
use std::ops::Range;
5252
use std::ptr;
53+
use std::str;
5354

5455
pub use self::sty::BoundRegion::*;
5556
pub use self::sty::InferTy::*;
@@ -2988,40 +2989,37 @@ pub struct CrateInherentImpls {
29882989
pub inherent_impls: DefIdMap<Vec<DefId>>,
29892990
}
29902991

2991-
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, HashStable)]
2992-
pub struct SymbolName {
2993-
// FIXME: we don't rely on interning or equality here - better have
2994-
// this be a `&'tcx str`.
2995-
pub name: Symbol,
2996-
}
2997-
2998-
impl SymbolName {
2999-
pub fn new(name: &str) -> SymbolName {
3000-
SymbolName { name: Symbol::intern(name) }
3001-
}
2992+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
2993+
pub struct SymbolName<'tcx> {
2994+
/// `&str` gives a consistent ordering, which ensures reproducible builds.
2995+
pub name: &'tcx str,
30022996
}
30032997

3004-
impl PartialOrd for SymbolName {
3005-
fn partial_cmp(&self, other: &SymbolName) -> Option<Ordering> {
3006-
self.name.as_str().partial_cmp(&other.name.as_str())
2998+
impl<'tcx> SymbolName<'tcx> {
2999+
pub fn new(tcx: TyCtxt<'tcx>, name: &str) -> SymbolName<'tcx> {
3000+
SymbolName {
3001+
name: unsafe { str::from_utf8_unchecked(tcx.arena.alloc_slice(name.as_bytes())) },
3002+
}
30073003
}
30083004
}
30093005

3010-
/// Ordering must use the chars to ensure reproducible builds.
3011-
impl Ord for SymbolName {
3012-
fn cmp(&self, other: &SymbolName) -> Ordering {
3013-
self.name.as_str().cmp(&other.name.as_str())
3006+
impl<'tcx> fmt::Display for SymbolName<'tcx> {
3007+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
3008+
fmt::Display::fmt(&self.name, fmt)
30143009
}
30153010
}
30163011

3017-
impl fmt::Display for SymbolName {
3012+
impl<'tcx> fmt::Debug for SymbolName<'tcx> {
30183013
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
30193014
fmt::Display::fmt(&self.name, fmt)
30203015
}
30213016
}
30223017

3023-
impl fmt::Debug for SymbolName {
3024-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
3025-
fmt::Display::fmt(&self.name, fmt)
3018+
impl<'tcx> rustc_serialize::UseSpecializedEncodable for SymbolName<'tcx> {
3019+
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
3020+
s.emit_str(self.name)
30263021
}
30273022
}
3023+
3024+
// The decoding takes place in `decode_symbol_name()`.
3025+
impl<'tcx> rustc_serialize::UseSpecializedDecodable for SymbolName<'tcx> {}

src/librustc_middle/ty/query/values.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::ty::{self, AdtSizedConstraint, Ty, TyCtxt, TyS};
22

3-
use rustc_span::symbol::Symbol;
4-
53
pub(super) trait Value<'tcx>: Sized {
64
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self;
75
}
@@ -21,9 +19,15 @@ impl<'tcx> Value<'tcx> for &'_ TyS<'_> {
2119
}
2220
}
2321

24-
impl<'tcx> Value<'tcx> for ty::SymbolName {
25-
fn from_cycle_error(_: TyCtxt<'tcx>) -> Self {
26-
ty::SymbolName { name: Symbol::intern("<error>") }
22+
impl<'tcx> Value<'tcx> for ty::SymbolName<'_> {
23+
fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
24+
// SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
25+
// FIXME: Represent the above fact in the trait system somehow.
26+
unsafe {
27+
std::mem::transmute::<ty::SymbolName<'tcx>, ty::SymbolName<'_>>(ty::SymbolName::new(
28+
tcx, "<error>",
29+
))
30+
}
2731
}
2832
}
2933

src/librustc_mir/monomorphize/partitioning.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ where
817817
debug!("CodegenUnit {} estimated size {} :", cgu.name(), cgu.size_estimate());
818818

819819
for (mono_item, linkage) in cgu.items() {
820-
let symbol_name = mono_item.symbol_name(tcx).name.as_str();
820+
let symbol_name = mono_item.symbol_name(tcx).name;
821821
let symbol_hash_start = symbol_name.rfind('h');
822822
let symbol_hash =
823823
symbol_hash_start.map(|i| &symbol_name[i..]).unwrap_or("<no hash>");

src/librustc_span/symbol.rs

-2
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,6 @@ symbols! {
856856
rustc_unsafe_specialization_marker,
857857
rustc_variance,
858858
rust_eh_personality,
859-
rust_eh_register_frames,
860-
rust_eh_unregister_frames,
861859
rustfmt,
862860
rust_oom,
863861
rvalue_static_promotion,

0 commit comments

Comments
 (0)