Introduce LocalInternedString::intern.

`LocalInternedString::intern(x)` is preferable to
`Symbol::intern(x).as_str()`, because the former involves one call to
`with_interner` while the latter involves two.
This commit is contained in:
Nicholas Nethercote 2019-05-14 15:13:42 +10:00
parent 257eaf523f
commit c06cdbeac5
4 changed files with 24 additions and 12 deletions

View file

@ -828,8 +828,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
// This shouldn't ever be needed, but just in case:
Ok(vec![match trait_ref {
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
Some(trait_ref) => LocalInternedString::intern(&format!("{:?}", trait_ref)),
None => LocalInternedString::intern(&format!("<{}>", self_ty)),
}])
}
@ -845,9 +845,10 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
// This shouldn't ever be needed, but just in case:
path.push(match trait_ref {
Some(trait_ref) => {
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
LocalInternedString::intern(&format!("<impl {} for {}>", trait_ref,
self_ty))
},
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
None => LocalInternedString::intern(&format!("<impl {}>", self_ty)),
});
Ok(path)

View file

@ -20,7 +20,7 @@ use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc::hir;
use syntax::ast::{self, FloatTy};
use syntax::symbol::Symbol;
use syntax::symbol::LocalInternedString;
use rustc_codegen_ssa::traits::*;
@ -213,7 +213,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
"type_name" => {
let tp_ty = substs.type_at(0);
let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
let ty_name = LocalInternedString::intern(&tp_ty.to_string());
self.const_str_slice(ty_name)
}
"type_id" => {

View file

@ -15,7 +15,7 @@ use crate::traits::*;
use std::borrow::Cow;
use syntax::symbol::Symbol;
use syntax::symbol::LocalInternedString;
use syntax_pos::Pos;
use super::{FunctionCx, LocalRef};
@ -401,7 +401,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
// Get the location information.
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
let filename = LocalInternedString::intern(&loc.file.name.to_string());
let line = bx.const_u32(loc.line as u32);
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
@ -423,7 +423,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
_ => {
let str = msg.description();
let msg_str = Symbol::intern(str).as_str();
let msg_str = LocalInternedString::intern(str);
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,
@ -535,7 +535,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let layout = bx.layout_of(ty);
if layout.abi.is_uninhabited() {
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
let filename = LocalInternedString::intern(&loc.file.name.to_string());
let line = bx.const_u32(loc.line as u32);
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
@ -543,7 +543,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
"Attempted to instantiate uninhabited type {}",
ty
);
let msg_str = Symbol::intern(&str).as_str();
let msg_str = LocalInternedString::intern(&str);
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,

View file

@ -1029,6 +1029,17 @@ pub struct LocalInternedString {
}
impl LocalInternedString {
/// Maps a string to its interned representation.
pub fn intern(string: &str) -> Self {
let string = with_interner(|interner| {
let symbol = interner.intern(string);
interner.strings[symbol.0.as_usize()]
});
LocalInternedString {
string: unsafe { std::mem::transmute::<&str, &str>(string) }
}
}
pub fn as_interned_str(self) -> InternedString {
InternedString {
symbol: Symbol::intern(self.string)
@ -1105,7 +1116,7 @@ impl fmt::Display for LocalInternedString {
impl Decodable for LocalInternedString {
fn decode<D: Decoder>(d: &mut D) -> Result<LocalInternedString, D::Error> {
Ok(Symbol::intern(&d.read_str()?).as_str())
Ok(LocalInternedString::intern(&d.read_str()?))
}
}