Skip to content

Commit 4dfbaba

Browse files
authored
Rollup merge of #64141 - nnethercote:minimize-LocalInternedString, r=petrochenkov
Minimize uses of `LocalInternedString` `LocalInternedString` is described as "An alternative to `Symbol` and `InternedString`, useful when the chars within the symbol need to be accessed. It is best used for temporary values." This PR makes the code match that comment, by removing all non-local uses of `LocalInternedString`. This allows the removal of a number of operations on `LocalInternedString` and a couple of uses of `unsafe`.
2 parents 57ffc83 + cc17b1b commit 4dfbaba

File tree

22 files changed

+86
-133
lines changed

22 files changed

+86
-133
lines changed

src/librustc/hir/map/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
186186
});
187187

188188
let mut upstream_crates: Vec<_> = cstore.crates_untracked().iter().map(|&cnum| {
189-
let name = cstore.crate_name_untracked(cnum).as_str();
189+
let name = cstore.crate_name_untracked(cnum).as_interned_str();
190190
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
191191
let hash = cstore.crate_hash_untracked(cnum);
192192
(name, disambiguator, hash)

src/librustc/ich/impls_syntax.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::mem;
99
use syntax::ast;
1010
use syntax::feature_gate;
1111
use syntax::parse::token;
12-
use syntax::symbol::{InternedString, LocalInternedString};
12+
use syntax::symbol::InternedString;
1313
use syntax::tokenstream;
1414
use syntax_pos::SourceFile;
1515

@@ -39,27 +39,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
3939
}
4040
}
4141

42-
impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
43-
#[inline]
44-
fn hash_stable<W: StableHasherResult>(&self,
45-
hcx: &mut StableHashingContext<'a>,
46-
hasher: &mut StableHasher<W>) {
47-
let s: &str = &**self;
48-
s.hash_stable(hcx, hasher);
49-
}
50-
}
51-
52-
impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
53-
type KeyType = LocalInternedString;
54-
55-
#[inline]
56-
fn to_stable_hash_key(&self,
57-
_: &StableHashingContext<'a>)
58-
-> LocalInternedString {
59-
self.clone()
60-
}
61-
}
62-
6342
impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
6443
#[inline]
6544
fn hash_stable<W: StableHasherResult>(&self,

src/librustc/lint/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::util::common::time;
3333
use std::default::Default as StdDefault;
3434
use syntax::ast;
3535
use syntax::edition;
36-
use syntax_pos::{MultiSpan, Span, symbol::{LocalInternedString, Symbol}};
36+
use syntax_pos::{MultiSpan, Span, symbol::Symbol};
3737
use errors::DiagnosticBuilder;
3838
use crate::hir;
3939
use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -405,7 +405,7 @@ impl LintStore {
405405
pub fn check_lint_name(
406406
&self,
407407
lint_name: &str,
408-
tool_name: Option<LocalInternedString>,
408+
tool_name: Option<Symbol>,
409409
) -> CheckLintNameResult<'_> {
410410
let complete_name = if let Some(tool_name) = tool_name {
411411
format!("{}::{}", tool_name, lint_name)

src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ impl<'a> LintLevelsBuilder<'a> {
291291
continue;
292292
}
293293

294-
Some(tool_ident.as_str())
294+
Some(tool_ident.name)
295295
} else {
296296
None
297297
};

src/librustc/traits/on_unimplemented.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use syntax::ast::{MetaItem, NestedMetaItem};
99
use syntax::attr;
1010
use syntax::symbol::{Symbol, kw, sym};
1111
use syntax_pos::Span;
12-
use syntax_pos::symbol::LocalInternedString;
1312

1413
#[derive(Clone, Debug)]
15-
pub struct OnUnimplementedFormatString(LocalInternedString);
14+
pub struct OnUnimplementedFormatString(Symbol);
1615

1716
#[derive(Debug)]
1817
pub struct OnUnimplementedDirective {
@@ -89,19 +88,19 @@ impl<'tcx> OnUnimplementedDirective {
8988
if item.check_name(sym::message) && message.is_none() {
9089
if let Some(message_) = item.value_str() {
9190
message = Some(OnUnimplementedFormatString::try_parse(
92-
tcx, trait_def_id, message_.as_str(), span)?);
91+
tcx, trait_def_id, message_, span)?);
9392
continue;
9493
}
9594
} else if item.check_name(sym::label) && label.is_none() {
9695
if let Some(label_) = item.value_str() {
9796
label = Some(OnUnimplementedFormatString::try_parse(
98-
tcx, trait_def_id, label_.as_str(), span)?);
97+
tcx, trait_def_id, label_, span)?);
9998
continue;
10099
}
101100
} else if item.check_name(sym::note) && note.is_none() {
102101
if let Some(note_) = item.value_str() {
103102
note = Some(OnUnimplementedFormatString::try_parse(
104-
tcx, trait_def_id, note_.as_str(), span)?);
103+
tcx, trait_def_id, note_, span)?);
105104
continue;
106105
}
107106
} else if item.check_name(sym::on) && is_root &&
@@ -154,7 +153,7 @@ impl<'tcx> OnUnimplementedDirective {
154153
message: None,
155154
subcommands: vec![],
156155
label: Some(OnUnimplementedFormatString::try_parse(
157-
tcx, trait_def_id, value.as_str(), attr.span)?),
156+
tcx, trait_def_id, value, attr.span)?),
158157
note: None,
159158
}))
160159
} else {
@@ -218,7 +217,7 @@ impl<'tcx> OnUnimplementedFormatString {
218217
fn try_parse(
219218
tcx: TyCtxt<'tcx>,
220219
trait_def_id: DefId,
221-
from: LocalInternedString,
220+
from: Symbol,
222221
err_sp: Span,
223222
) -> Result<Self, ErrorReported> {
224223
let result = OnUnimplementedFormatString(from);
@@ -234,7 +233,8 @@ impl<'tcx> OnUnimplementedFormatString {
234233
) -> Result<(), ErrorReported> {
235234
let name = tcx.item_name(trait_def_id);
236235
let generics = tcx.generics_of(trait_def_id);
237-
let parser = Parser::new(&self.0, None, vec![], false);
236+
let s = self.0.as_str();
237+
let parser = Parser::new(&s, None, vec![], false);
238238
let mut result = Ok(());
239239
for token in parser {
240240
match token {
@@ -294,7 +294,8 @@ impl<'tcx> OnUnimplementedFormatString {
294294
}).collect::<FxHashMap<Symbol, String>>();
295295
let empty_string = String::new();
296296

297-
let parser = Parser::new(&self.0, None, vec![], false);
297+
let s = self.0.as_str();
298+
let parser = Parser::new(&s, None, vec![], false);
298299
parser.map(|p|
299300
match p {
300301
Piece::String(s) => s,

src/librustc/ty/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::ops::Range;
4646
use syntax::ast::{self, Name, Ident, NodeId};
4747
use syntax::attr;
4848
use syntax::ext::hygiene::ExpnId;
49-
use syntax::symbol::{kw, sym, Symbol, LocalInternedString, InternedString};
49+
use syntax::symbol::{kw, sym, Symbol, InternedString};
5050
use syntax_pos::Span;
5151

5252
use smallvec;
@@ -3386,10 +3386,6 @@ impl SymbolName {
33863386
name: InternedString::intern(name)
33873387
}
33883388
}
3389-
3390-
pub fn as_str(&self) -> LocalInternedString {
3391-
self.name.as_str()
3392-
}
33933389
}
33943390

33953391
impl fmt::Display for SymbolName {

src/librustc_codegen_llvm/builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::context::CodegenCx;
55
use crate::type_::Type;
66
use crate::type_of::LayoutLlvmExt;
77
use crate::value::Value;
8-
use syntax::symbol::LocalInternedString;
98
use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
109
use rustc_codegen_ssa::MemFlags;
1110
use libc::{c_uint, c_char};
@@ -24,6 +23,7 @@ use std::ffi::CStr;
2423
use std::ops::{Deref, Range};
2524
use std::ptr;
2625
use std::iter::TrustedLen;
26+
use syntax::symbol::Symbol;
2727

2828
// All Builders must have an llfn associated with them
2929
#[must_use]
@@ -1082,8 +1082,8 @@ impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {
10821082

10831083
fn static_panic_msg(
10841084
&mut self,
1085-
msg: Option<LocalInternedString>,
1086-
filename: LocalInternedString,
1085+
msg: Option<Symbol>,
1086+
filename: Symbol,
10871087
line: Self::Value,
10881088
col: Self::Value,
10891089
kind: &str,

src/librustc_codegen_llvm/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn get_fn(
3737
return llfn;
3838
}
3939

40-
let sym = tcx.symbol_name(instance).as_str();
40+
let sym = tcx.symbol_name(instance).name.as_str();
4141
debug!("get_fn({:?}: {:?}) => {}", instance, sig, sym);
4242

4343
// Create a fn pointer with the substituted signature.

src/librustc_codegen_llvm/common.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
1717

1818
use libc::{c_uint, c_char};
1919

20-
use syntax::symbol::LocalInternedString;
20+
use syntax::symbol::Symbol;
2121
use syntax::ast::Mutability;
2222

2323
pub use crate::context::CodegenCx;
@@ -122,17 +122,18 @@ impl CodegenCx<'ll, 'tcx> {
122122

123123
fn const_cstr(
124124
&self,
125-
s: LocalInternedString,
125+
s: Symbol,
126126
null_terminated: bool,
127127
) -> &'ll Value {
128128
unsafe {
129129
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
130130
return llval;
131131
}
132132

133+
let s_str = s.as_str();
133134
let sc = llvm::LLVMConstStringInContext(self.llcx,
134-
s.as_ptr() as *const c_char,
135-
s.len() as c_uint,
135+
s_str.as_ptr() as *const c_char,
136+
s_str.len() as c_uint,
136137
!null_terminated as Bool);
137138
let sym = self.generate_local_symbol_name("str");
138139
let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
@@ -147,8 +148,8 @@ impl CodegenCx<'ll, 'tcx> {
147148
}
148149
}
149150

150-
pub fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
151-
let len = s.len();
151+
pub fn const_str_slice(&self, s: Symbol) -> &'ll Value {
152+
let len = s.as_str().len();
152153
let cs = consts::ptrcast(self.const_cstr(s, false),
153154
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
154155
self.const_fat_ptr(cs, self.const_usize(len as u64))

src/librustc_codegen_llvm/consts.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint,
1111
Pointer, ErrorHandled, GlobalId};
1212
use rustc::mir::mono::MonoItem;
1313
use rustc::hir::Node;
14-
use syntax_pos::Span;
1514
use rustc_target::abi::HasDataLayout;
16-
use syntax::symbol::sym;
17-
use syntax_pos::symbol::LocalInternedString;
1815
use rustc::ty::{self, Ty, Instance};
1916
use rustc_codegen_ssa::traits::*;
17+
use syntax::symbol::{Symbol, sym};
18+
use syntax_pos::Span;
2019

2120
use rustc::ty::layout::{self, Size, Align, LayoutOf};
2221

@@ -122,10 +121,11 @@ fn check_and_apply_linkage(
122121
cx: &CodegenCx<'ll, 'tcx>,
123122
attrs: &CodegenFnAttrs,
124123
ty: Ty<'tcx>,
125-
sym: LocalInternedString,
124+
sym: Symbol,
126125
span: Span
127126
) -> &'ll Value {
128127
let llty = cx.layout_of(ty).llvm_type(cx);
128+
let sym = sym.as_str();
129129
if let Some(linkage) = attrs.linkage {
130130
debug!("get_static: sym={} linkage={:?}", sym, linkage);
131131

@@ -221,7 +221,7 @@ impl CodegenCx<'ll, 'tcx> {
221221
def_id);
222222

223223
let ty = instance.ty(self.tcx);
224-
let sym = self.tcx.symbol_name(instance).as_str();
224+
let sym = self.tcx.symbol_name(instance).name.as_symbol();
225225

226226
debug!("get_static: sym={} instance={:?}", sym, instance);
227227

@@ -232,11 +232,12 @@ impl CodegenCx<'ll, 'tcx> {
232232
Node::Item(&hir::Item {
233233
ref attrs, span, node: hir::ItemKind::Static(..), ..
234234
}) => {
235-
if self.get_declared_value(&sym[..]).is_some() {
235+
let sym_str = sym.as_str();
236+
if self.get_declared_value(&sym_str).is_some() {
236237
span_bug!(span, "Conflicting symbol names for static?");
237238
}
238239

239-
let g = self.define_global(&sym[..], llty).unwrap();
240+
let g = self.define_global(&sym_str, llty).unwrap();
240241

241242
if !self.tcx.is_reachable_non_generic(def_id) {
242243
unsafe {

src/librustc_codegen_llvm/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::cell::{Cell, RefCell};
2929
use std::iter;
3030
use std::str;
3131
use std::sync::Arc;
32-
use syntax::symbol::LocalInternedString;
32+
use syntax::symbol::Symbol;
3333
use syntax::source_map::{DUMMY_SP, Span};
3434
use crate::abi::Abi;
3535

@@ -52,7 +52,7 @@ pub struct CodegenCx<'ll, 'tcx> {
5252
pub vtables:
5353
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
5454
/// Cache of constant strings,
55-
pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'ll Value>>,
55+
pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
5656

5757
/// Reverse-direction for const ptrs cast from globals.
5858
/// Key is a Value holding a *T,

src/librustc_codegen_llvm/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ pub fn create_global_var_metadata(
22512251
None
22522252
} else {
22532253
let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id));
2254-
Some(SmallCStr::new(&linkage_name.as_str()))
2254+
Some(SmallCStr::new(&linkage_name.name.as_str()))
22552255
};
22562256

22572257
let global_align = cx.align_of(variable_type);

src/librustc_codegen_llvm/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
290290
let scope_line = span_start(self, span).line;
291291

292292
let function_name = CString::new(name).unwrap();
293-
let linkage_name = SmallCStr::new(&linkage_name.as_str());
293+
let linkage_name = SmallCStr::new(&linkage_name.name.as_str());
294294

295295
let mut flags = DIFlags::FlagPrototyped;
296296

src/librustc_codegen_ssa/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn reachable_non_generics_provider(
121121
})
122122
.map(|def_id| {
123123
let export_level = if special_runtime_crate {
124-
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).as_str();
124+
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.as_str();
125125
// We can probably do better here by just ensuring that
126126
// it has hidden visibility rather than public
127127
// visibility, as this is primarily here to ensure it's

src/librustc_codegen_ssa/mir/block.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::traits::*;
1414

1515
use std::borrow::Cow;
1616

17-
use syntax::symbol::LocalInternedString;
17+
use syntax::symbol::Symbol;
1818
use syntax_pos::Pos;
1919

2020
use super::{FunctionCx, LocalRef};
@@ -397,7 +397,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
397397

398398
// Get the location information.
399399
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
400-
let filename = LocalInternedString::intern(&loc.file.name.to_string());
400+
let filename = Symbol::intern(&loc.file.name.to_string());
401401
let line = bx.const_u32(loc.line as u32);
402402
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
403403

@@ -418,8 +418,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
418418
vec![file_line_col, index, len])
419419
}
420420
_ => {
421-
let str = msg.description();
422-
let msg_str = LocalInternedString::intern(str);
421+
let msg_str = Symbol::intern(msg.description());
423422
let msg_file_line_col = bx.static_panic_msg(
424423
Some(msg_str),
425424
filename,
@@ -531,15 +530,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
531530
let layout = bx.layout_of(ty);
532531
if layout.abi.is_uninhabited() {
533532
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
534-
let filename = LocalInternedString::intern(&loc.file.name.to_string());
533+
let filename = Symbol::intern(&loc.file.name.to_string());
535534
let line = bx.const_u32(loc.line as u32);
536535
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
537536

538537
let str = format!(
539538
"Attempted to instantiate uninhabited type {}",
540539
ty
541540
);
542-
let msg_str = LocalInternedString::intern(&str);
541+
let msg_str = Symbol::intern(&str);
543542
let msg_file_line_col = bx.static_panic_msg(
544543
Some(msg_str),
545544
filename,

src/librustc_codegen_ssa/mono_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
5858
self.to_raw_string(),
5959
cx.codegen_unit().name());
6060

61-
let symbol_name = self.symbol_name(cx.tcx()).as_str();
61+
let symbol_name = self.symbol_name(cx.tcx()).name.as_str();
6262

6363
debug!("symbol {}", &symbol_name);
6464

0 commit comments

Comments
 (0)