Skip to content

Commit cc74068

Browse files
committed
Remove Rc from the interner.
1 parent 36c8f6b commit cc74068

File tree

4 files changed

+36
-51
lines changed

4 files changed

+36
-51
lines changed

src/librustc_driver/driver.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1355,11 +1355,3 @@ pub fn build_output_filenames(input: &Input,
13551355
}
13561356
}
13571357
}
1358-
1359-
// For use by the `rusti` project (https://github.com/murarth/rusti).
1360-
pub fn reset_thread_local_state() {
1361-
// These may be left in an incoherent state after a previous compile.
1362-
syntax::ext::hygiene::reset_hygiene_data();
1363-
// `clear_interner` can be used to free memory, but it does not restore the initial state.
1364-
symbol::reset_interner();
1365-
}

src/librustc_trans/debuginfo/metadata.rs

+14-25
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use std::ffi::CString;
4444
use std::fmt::Write;
4545
use std::path::Path;
4646
use std::ptr;
47-
use std::rc::Rc;
4847
use syntax::ast;
4948
use syntax::symbol::{Interner, InternedString};
5049
use syntax_pos::{self, Span};
@@ -116,9 +115,8 @@ impl<'tcx> TypeMap<'tcx> {
116115
unique_type_id: UniqueTypeId,
117116
metadata: DIType) {
118117
if self.unique_id_to_metadata.insert(unique_type_id, metadata).is_some() {
119-
let unique_type_id_str = self.get_unique_type_id_as_string(unique_type_id);
120118
bug!("Type metadata for unique id '{}' is already in the TypeMap!",
121-
&unique_type_id_str[..]);
119+
self.get_unique_type_id_as_string(unique_type_id));
122120
}
123121
}
124122

@@ -132,7 +130,7 @@ impl<'tcx> TypeMap<'tcx> {
132130

133131
// Get the string representation of a UniqueTypeId. This method will fail if
134132
// the id is unknown.
135-
fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> Rc<str> {
133+
fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> &str {
136134
let UniqueTypeId(interner_key) = unique_type_id;
137135
self.unique_id_interner.get(interner_key)
138136
}
@@ -181,7 +179,7 @@ impl<'tcx> TypeMap<'tcx> {
181179
-> UniqueTypeId {
182180
let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type);
183181
let enum_variant_type_id = format!("{}::{}",
184-
&self.get_unique_type_id_as_string(enum_type_id),
182+
self.get_unique_type_id_as_string(enum_type_id),
185183
variant_name);
186184
let interner_key = self.unique_id_interner.intern(&enum_variant_type_id);
187185
UniqueTypeId(interner_key)
@@ -622,29 +620,25 @@ pub fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
622620
let metadata_for_uid = match type_map.find_metadata_for_unique_id(unique_type_id) {
623621
Some(metadata) => metadata,
624622
None => {
625-
let unique_type_id_str =
626-
type_map.get_unique_type_id_as_string(unique_type_id);
627623
span_bug!(usage_site_span,
628624
"Expected type metadata for unique \
629625
type id '{}' to already be in \
630626
the debuginfo::TypeMap but it \
631627
was not. (Ty = {})",
632-
&unique_type_id_str[..],
628+
type_map.get_unique_type_id_as_string(unique_type_id),
633629
t);
634630
}
635631
};
636632

637633
match type_map.find_metadata_for_type(t) {
638634
Some(metadata) => {
639635
if metadata != metadata_for_uid {
640-
let unique_type_id_str =
641-
type_map.get_unique_type_id_as_string(unique_type_id);
642636
span_bug!(usage_site_span,
643637
"Mismatch between Ty and \
644638
UniqueTypeId maps in \
645639
debuginfo::TypeMap. \
646640
UniqueTypeId={}, Ty={}",
647-
&unique_type_id_str[..],
641+
type_map.get_unique_type_id_as_string(unique_type_id),
648642
t);
649643
}
650644
}
@@ -1525,13 +1519,10 @@ fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
15251519
let enum_llvm_type = type_of::type_of(cx, enum_type);
15261520
let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type);
15271521

1528-
let unique_type_id_str = debug_context(cx)
1529-
.type_map
1530-
.borrow()
1531-
.get_unique_type_id_as_string(unique_type_id);
1532-
15331522
let enum_name = CString::new(enum_name).unwrap();
1534-
let unique_type_id_str = CString::new(unique_type_id_str.as_bytes()).unwrap();
1523+
let unique_type_id_str = CString::new(
1524+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1525+
).unwrap();
15351526
let enum_metadata = unsafe {
15361527
llvm::LLVMRustDIBuilderCreateUnionType(
15371528
DIB(cx),
@@ -1668,11 +1659,10 @@ fn create_struct_stub(cx: &CrateContext,
16681659
-> DICompositeType {
16691660
let (struct_size, struct_align) = size_and_align_of(cx, struct_llvm_type);
16701661

1671-
let unique_type_id_str = debug_context(cx).type_map
1672-
.borrow()
1673-
.get_unique_type_id_as_string(unique_type_id);
16741662
let name = CString::new(struct_type_name).unwrap();
1675-
let unique_type_id = CString::new(unique_type_id_str.as_bytes()).unwrap();
1663+
let unique_type_id = CString::new(
1664+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1665+
).unwrap();
16761666
let metadata_stub = unsafe {
16771667
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
16781668
// pointer will lead to hard to trace and debug LLVM assertions
@@ -1706,11 +1696,10 @@ fn create_union_stub(cx: &CrateContext,
17061696
-> DICompositeType {
17071697
let (union_size, union_align) = size_and_align_of(cx, union_llvm_type);
17081698

1709-
let unique_type_id_str = debug_context(cx).type_map
1710-
.borrow()
1711-
.get_unique_type_id_as_string(unique_type_id);
17121699
let name = CString::new(union_type_name).unwrap();
1713-
let unique_type_id = CString::new(unique_type_id_str.as_bytes()).unwrap();
1700+
let unique_type_id = CString::new(
1701+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1702+
).unwrap();
17141703
let metadata_stub = unsafe {
17151704
// LLVMRustDIBuilderCreateUnionType() wants an empty array. A null
17161705
// pointer will lead to hard to trace and debug LLVM assertions

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![feature(associated_consts)]
2828
#![feature(const_fn)]
2929
#![feature(libc)]
30+
#![feature(optin_builtin_traits)]
3031
#![feature(rustc_private)]
3132
#![feature(staged_api)]
3233
#![feature(str_escape)]

src/libsyntax/symbol.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ use serialize::{Decodable, Decoder, Encodable, Encoder};
1616
use std::cell::RefCell;
1717
use std::collections::HashMap;
1818
use std::fmt;
19-
use std::rc::Rc;
2019

2120
/// A symbol is an interned or gensymed string.
2221
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2322
pub struct Symbol(u32);
2423

24+
// The interner in thread-local, so `Symbol` shouldn't move between threads.
25+
impl !Send for Symbol { }
26+
2527
impl Symbol {
2628
/// Maps a string to its interned representation.
2729
pub fn intern(string: &str) -> Self {
@@ -34,7 +36,11 @@ impl Symbol {
3436
}
3537

3638
pub fn as_str(self) -> InternedString {
37-
with_interner(|interner| InternedString { string: interner.get(self) })
39+
with_interner(|interner| unsafe {
40+
InternedString {
41+
string: ::std::mem::transmute::<&str, &str>(interner.get(self))
42+
}
43+
})
3844
}
3945

4046
pub fn as_u32(self) -> u32 {
@@ -74,8 +80,8 @@ impl<'a> PartialEq<&'a str> for Symbol {
7480

7581
#[derive(Default)]
7682
pub struct Interner {
77-
names: HashMap<Rc<str>, Symbol>,
78-
strings: Vec<Rc<str>>,
83+
names: HashMap<Box<str>, Symbol>,
84+
strings: Vec<Box<str>>,
7985
}
8086

8187
impl Interner {
@@ -97,7 +103,7 @@ impl Interner {
97103
}
98104

99105
let name = Symbol(self.strings.len() as u32);
100-
let string = Rc::__from_str(string);
106+
let string = string.to_string().into_boxed_str();
101107
self.strings.push(string.clone());
102108
self.names.insert(string, name);
103109
name
@@ -106,12 +112,12 @@ impl Interner {
106112
fn gensym(&mut self, string: &str) -> Symbol {
107113
let gensym = Symbol(self.strings.len() as u32);
108114
// leave out of `names` to avoid colliding
109-
self.strings.push(Rc::__from_str(string));
115+
self.strings.push(string.to_string().into_boxed_str());
110116
gensym
111117
}
112118

113-
pub fn get(&self, name: Symbol) -> Rc<str> {
114-
self.strings[name.0 as usize].clone()
119+
pub fn get(&self, name: Symbol) -> &str {
120+
&self.strings[name.0 as usize]
115121
}
116122
}
117123

@@ -225,11 +231,6 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
225231
INTERNER.with(|interner| f(&mut *interner.borrow_mut()))
226232
}
227233

228-
/// Reset the ident interner to its initial state.
229-
pub fn reset_interner() {
230-
with_interner(|interner| *interner = Interner::fresh());
231-
}
232-
233234
/// Represents a string stored in the thread-local interner. Because the
234235
/// interner lives for the life of the thread, this can be safely treated as an
235236
/// immortal string, as long as it never crosses between threads.
@@ -241,23 +242,25 @@ pub fn reset_interner() {
241242
/// somehow.
242243
#[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
243244
pub struct InternedString {
244-
string: Rc<str>,
245+
string: &'static str,
245246
}
246247

248+
impl !Send for InternedString { }
249+
247250
impl ::std::ops::Deref for InternedString {
248251
type Target = str;
249-
fn deref(&self) -> &str { &self.string }
252+
fn deref(&self) -> &str { self.string }
250253
}
251254

252255
impl fmt::Debug for InternedString {
253256
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
254-
fmt::Debug::fmt(&self.string, f)
257+
fmt::Debug::fmt(self.string, f)
255258
}
256259
}
257260

258261
impl fmt::Display for InternedString {
259262
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260-
fmt::Display::fmt(&self.string, f)
263+
fmt::Display::fmt(self.string, f)
261264
}
262265
}
263266

@@ -269,7 +272,7 @@ impl Decodable for InternedString {
269272

270273
impl Encodable for InternedString {
271274
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
272-
s.emit_str(&self.string)
275+
s.emit_str(self.string)
273276
}
274277
}
275278

0 commit comments

Comments
 (0)