Skip to content

Commit

Permalink
Introduce LocalInternedString::intern.
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
nnethercote committed May 14, 2019
1 parent 47656a3 commit 67b969e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}])
}

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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" => {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -535,15 +535,15 @@ 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);

let str = format!(
"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,
Expand Down
13 changes: 12 additions & 1 deletion src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,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)
Expand Down Expand Up @@ -1095,7 +1106,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()?))
}
}

Expand Down

0 comments on commit 67b969e

Please sign in to comment.