diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index e713cf8d80553..7e1e751e85647 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -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!("", trait_ref, self_ty)).as_str() + LocalInternedString::intern(&format!("", trait_ref, + self_ty)) }, - None => Symbol::intern(&format!("", self_ty)).as_str(), + None => LocalInternedString::intern(&format!("", self_ty)), }); Ok(path) diff --git a/src/librustc_codegen_llvm/intrinsic.rs b/src/librustc_codegen_llvm/intrinsic.rs index ceb08f943678b..9ae0e26196d94 100644 --- a/src/librustc_codegen_llvm/intrinsic.rs +++ b/src/librustc_codegen_llvm/intrinsic.rs @@ -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" => { diff --git a/src/librustc_codegen_ssa/mir/block.rs b/src/librustc_codegen_ssa/mir/block.rs index e64c847db651b..26a86626b8975 100644 --- a/src/librustc_codegen_ssa/mir/block.rs +++ b/src/librustc_codegen_ssa/mir/block.rs @@ -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, diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 5761082b9309b..4debf9cfee70d 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -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) @@ -1095,7 +1106,7 @@ impl fmt::Display for LocalInternedString { impl Decodable for LocalInternedString { fn decode(d: &mut D) -> Result { - Ok(Symbol::intern(&d.read_str()?).as_str()) + Ok(LocalInternedString::intern(&d.read_str()?)) } }