Skip to content

Commit c06cdbe

Browse files
committed
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.
1 parent 257eaf5 commit c06cdbe

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/librustc/lint/context.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
828828

829829
// This shouldn't ever be needed, but just in case:
830830
Ok(vec![match trait_ref {
831-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
832-
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
831+
Some(trait_ref) => LocalInternedString::intern(&format!("{:?}", trait_ref)),
832+
None => LocalInternedString::intern(&format!("<{}>", self_ty)),
833833
}])
834834
}
835835

@@ -845,9 +845,10 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
845845
// This shouldn't ever be needed, but just in case:
846846
path.push(match trait_ref {
847847
Some(trait_ref) => {
848-
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
848+
LocalInternedString::intern(&format!("<impl {} for {}>", trait_ref,
849+
self_ty))
849850
},
850-
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
851+
None => LocalInternedString::intern(&format!("<impl {}>", self_ty)),
851852
});
852853

853854
Ok(path)

src/librustc_codegen_llvm/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
2020
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2121
use rustc::hir;
2222
use syntax::ast::{self, FloatTy};
23-
use syntax::symbol::Symbol;
23+
use syntax::symbol::LocalInternedString;
2424

2525
use rustc_codegen_ssa::traits::*;
2626

@@ -213,7 +213,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
213213
}
214214
"type_name" => {
215215
let tp_ty = substs.type_at(0);
216-
let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
216+
let ty_name = LocalInternedString::intern(&tp_ty.to_string());
217217
self.const_str_slice(ty_name)
218218
}
219219
"type_id" => {

src/librustc_codegen_ssa/mir/block.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::traits::*;
1515

1616
use std::borrow::Cow;
1717

18-
use syntax::symbol::Symbol;
18+
use syntax::symbol::LocalInternedString;
1919
use syntax_pos::Pos;
2020

2121
use super::{FunctionCx, LocalRef};
@@ -401,7 +401,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
401401

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

@@ -423,7 +423,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
423423
}
424424
_ => {
425425
let str = msg.description();
426-
let msg_str = Symbol::intern(str).as_str();
426+
let msg_str = LocalInternedString::intern(str);
427427
let msg_file_line_col = bx.static_panic_msg(
428428
Some(msg_str),
429429
filename,
@@ -535,15 +535,15 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
535535
let layout = bx.layout_of(ty);
536536
if layout.abi.is_uninhabited() {
537537
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
538-
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
538+
let filename = LocalInternedString::intern(&loc.file.name.to_string());
539539
let line = bx.const_u32(loc.line as u32);
540540
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
541541

542542
let str = format!(
543543
"Attempted to instantiate uninhabited type {}",
544544
ty
545545
);
546-
let msg_str = Symbol::intern(&str).as_str();
546+
let msg_str = LocalInternedString::intern(&str);
547547
let msg_file_line_col = bx.static_panic_msg(
548548
Some(msg_str),
549549
filename,

src/libsyntax_pos/symbol.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,17 @@ pub struct LocalInternedString {
10291029
}
10301030

10311031
impl LocalInternedString {
1032+
/// Maps a string to its interned representation.
1033+
pub fn intern(string: &str) -> Self {
1034+
let string = with_interner(|interner| {
1035+
let symbol = interner.intern(string);
1036+
interner.strings[symbol.0.as_usize()]
1037+
});
1038+
LocalInternedString {
1039+
string: unsafe { std::mem::transmute::<&str, &str>(string) }
1040+
}
1041+
}
1042+
10321043
pub fn as_interned_str(self) -> InternedString {
10331044
InternedString {
10341045
symbol: Symbol::intern(self.string)
@@ -1105,7 +1116,7 @@ impl fmt::Display for LocalInternedString {
11051116

11061117
impl Decodable for LocalInternedString {
11071118
fn decode<D: Decoder>(d: &mut D) -> Result<LocalInternedString, D::Error> {
1108-
Ok(Symbol::intern(&d.read_str()?).as_str())
1119+
Ok(LocalInternedString::intern(&d.read_str()?))
11091120
}
11101121
}
11111122

0 commit comments

Comments
 (0)