Skip to content

Commit 44b5359

Browse files
committed
Rollup merge of rust-lang#49894 - Zoxc:sync-internedstring, r=michaelwoerister
Rename InternedString to LocalInternedString and introduce a new thread-safe InternedString This is an allocation-free alternative to rust-lang#46972.
2 parents f1a4c10 + 4d52751 commit 44b5359

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+307
-149
lines changed

src/librustc/hir/lowering.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ impl<'a> LoweringContext<'a> {
655655
self.resolver.definitions().create_def_with_parent(
656656
parent_id.index,
657657
def_node_id,
658-
DefPathData::LifetimeDef(str_name),
658+
DefPathData::LifetimeDef(str_name.as_interned_str()),
659659
DefIndexAddressSpace::High,
660660
Mark::root(),
661661
span,
@@ -1302,7 +1302,7 @@ impl<'a> LoweringContext<'a> {
13021302
self.context.resolver.definitions().create_def_with_parent(
13031303
self.parent,
13041304
def_node_id,
1305-
DefPathData::LifetimeDef(name.name().as_str()),
1305+
DefPathData::LifetimeDef(name.name().as_interned_str()),
13061306
DefIndexAddressSpace::High,
13071307
Mark::root(),
13081308
lifetime.span,

src/librustc/hir/map/def_collector.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
107107
// information we encapsulate into
108108
let def_data = match i.node {
109109
ItemKind::Impl(..) => DefPathData::Impl,
110-
ItemKind::Trait(..) => DefPathData::Trait(i.ident.name.as_str()),
110+
ItemKind::Trait(..) => DefPathData::Trait(i.ident.name.as_interned_str()),
111111
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
112112
ItemKind::TraitAlias(..) |
113113
ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) | ItemKind::Ty(..) =>
114-
DefPathData::TypeNs(i.ident.name.as_str()),
114+
DefPathData::TypeNs(i.ident.name.as_interned_str()),
115115
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
116116
return visit::walk_item(self, i);
117117
}
118-
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_str()),
118+
ItemKind::Mod(..) => DefPathData::Module(i.ident.name.as_interned_str()),
119119
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
120-
DefPathData::ValueNs(i.ident.name.as_str()),
121-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
120+
DefPathData::ValueNs(i.ident.name.as_interned_str()),
121+
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_interned_str()),
122122
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
123123
ItemKind::GlobalAsm(..) => DefPathData::Misc,
124124
ItemKind::Use(..) => {
@@ -133,15 +133,16 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
133133
for v in &enum_definition.variants {
134134
let variant_def_index =
135135
this.create_def(v.node.data.id(),
136-
DefPathData::EnumVariant(v.node.ident.name.as_str()),
136+
DefPathData::EnumVariant(v.node.ident
137+
.name.as_interned_str()),
137138
REGULAR_SPACE,
138139
v.span);
139140
this.with_parent(variant_def_index, |this| {
140141
for (index, field) in v.node.data.fields().iter().enumerate() {
141142
let name = field.ident.map(|ident| ident.name)
142143
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
143144
this.create_def(field.id,
144-
DefPathData::Field(name.as_str()),
145+
DefPathData::Field(name.as_interned_str()),
145146
REGULAR_SPACE,
146147
field.span);
147148
}
@@ -165,7 +166,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
165166
let name = field.ident.map(|ident| ident.name)
166167
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
167168
this.create_def(field.id,
168-
DefPathData::Field(name.as_str()),
169+
DefPathData::Field(name.as_interned_str()),
169170
REGULAR_SPACE,
170171
field.span);
171172
}
@@ -187,7 +188,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
187188
}
188189

189190
let def = self.create_def(foreign_item.id,
190-
DefPathData::ValueNs(foreign_item.ident.name.as_str()),
191+
DefPathData::ValueNs(foreign_item.ident.name.as_interned_str()),
191192
REGULAR_SPACE,
192193
foreign_item.span);
193194

@@ -201,15 +202,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
201202
GenericParam::Lifetime(ref lifetime_def) => {
202203
self.create_def(
203204
lifetime_def.lifetime.id,
204-
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_str()),
205+
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_interned_str()),
205206
REGULAR_SPACE,
206207
lifetime_def.lifetime.ident.span
207208
);
208209
}
209210
GenericParam::Type(ref ty_param) => {
210211
self.create_def(
211212
ty_param.id,
212-
DefPathData::TypeParam(ty_param.ident.name.as_str()),
213+
DefPathData::TypeParam(ty_param.ident.name.as_interned_str()),
213214
REGULAR_SPACE,
214215
ty_param.ident.span
215216
);
@@ -222,8 +223,10 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
222223
fn visit_trait_item(&mut self, ti: &'a TraitItem) {
223224
let def_data = match ti.node {
224225
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
225-
DefPathData::ValueNs(ti.ident.name.as_str()),
226-
TraitItemKind::Type(..) => DefPathData::AssocTypeInTrait(ti.ident.name.as_str()),
226+
DefPathData::ValueNs(ti.ident.name.as_interned_str()),
227+
TraitItemKind::Type(..) => {
228+
DefPathData::AssocTypeInTrait(ti.ident.name.as_interned_str())
229+
},
227230
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false),
228231
};
229232

@@ -240,8 +243,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
240243
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
241244
let def_data = match ii.node {
242245
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
243-
DefPathData::ValueNs(ii.ident.name.as_str()),
244-
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.name.as_str()),
246+
DefPathData::ValueNs(ii.ident.name.as_interned_str()),
247+
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.name.as_interned_str()),
245248
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false),
246249
};
247250

src/librustc/hir/map/definitions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl DefPathData {
701701
Typeof => "{{typeof}}",
702702
};
703703

704-
Symbol::intern(s).as_str()
704+
Symbol::intern(s).as_interned_str()
705705
}
706706

707707
pub fn to_string(&self) -> String {
@@ -731,7 +731,7 @@ macro_rules! define_global_metadata_kind {
731731
definitions.create_def_with_parent(
732732
CRATE_DEF_INDEX,
733733
ast::DUMMY_NODE_ID,
734-
DefPathData::GlobalMetaData(instance.name().as_str()),
734+
DefPathData::GlobalMetaData(instance.name().as_interned_str()),
735735
GLOBAL_MD_ADDRESS_SPACE,
736736
Mark::root(),
737737
DUMMY_SP
@@ -746,7 +746,7 @@ macro_rules! define_global_metadata_kind {
746746
let def_key = DefKey {
747747
parent: Some(CRATE_DEF_INDEX),
748748
disambiguated_data: DisambiguatedDefPathData {
749-
data: DefPathData::GlobalMetaData(self.name().as_str()),
749+
data: DefPathData::GlobalMetaData(self.name().as_interned_str()),
750750
disambiguator: 0,
751751
}
752752
};

src/librustc/ich/impls_syntax.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::mem;
1919
use syntax::ast;
2020
use syntax::feature_gate;
2121
use syntax::parse::token;
22-
use syntax::symbol::InternedString;
22+
use syntax::symbol::{InternedString, LocalInternedString};
2323
use syntax::tokenstream;
2424
use syntax_pos::FileMap;
2525

@@ -34,8 +34,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
3434
fn hash_stable<W: StableHasherResult>(&self,
3535
hcx: &mut StableHashingContext<'a>,
3636
hasher: &mut StableHasher<W>) {
37-
let s: &str = &**self;
38-
s.hash_stable(hcx, hasher);
37+
self.with(|s| s.hash_stable(hcx, hasher))
3938
}
4039
}
4140

@@ -50,6 +49,27 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
5049
}
5150
}
5251

52+
impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
53+
#[inline]
54+
fn hash_stable<W: StableHasherResult>(&self,
55+
hcx: &mut StableHashingContext<'a>,
56+
hasher: &mut StableHasher<W>) {
57+
let s: &str = &**self;
58+
s.hash_stable(hcx, hasher);
59+
}
60+
}
61+
62+
impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
63+
type KeyType = LocalInternedString;
64+
65+
#[inline]
66+
fn to_stable_hash_key(&self,
67+
_: &StableHashingContext<'a>)
68+
-> LocalInternedString {
69+
self.clone()
70+
}
71+
}
72+
5373
impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
5474
#[inline]
5575
fn hash_stable<W: StableHasherResult>(&self,
@@ -66,7 +86,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
6686
fn to_stable_hash_key(&self,
6787
_: &StableHashingContext<'a>)
6888
-> InternedString {
69-
self.as_str()
89+
self.as_interned_str()
7090
}
7191
}
7292

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#![feature(nonzero)]
5959
#![feature(proc_macro_internals)]
6060
#![feature(quote)]
61+
#![feature(optin_builtin_traits)]
6162
#![feature(refcell_replace_swap)]
6263
#![feature(rustc_diagnostic_macros)]
6364
#![feature(slice_patterns)]

src/librustc/traits/on_unimplemented.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ use util::nodemap::FxHashMap;
1818
use syntax::ast::{MetaItem, NestedMetaItem};
1919
use syntax::attr;
2020
use syntax_pos::Span;
21-
use syntax_pos::symbol::InternedString;
21+
use syntax_pos::symbol::LocalInternedString;
2222

2323
#[derive(Clone, Debug)]
24-
pub struct OnUnimplementedFormatString(InternedString);
24+
pub struct OnUnimplementedFormatString(LocalInternedString);
2525

2626
#[derive(Debug)]
2727
pub struct OnUnimplementedDirective {
@@ -225,7 +225,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
225225
impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
226226
pub fn try_parse(tcx: TyCtxt<'a, 'gcx, 'tcx>,
227227
trait_def_id: DefId,
228-
from: InternedString,
228+
from: LocalInternedString,
229229
err_sp: Span)
230230
-> Result<Self, ErrorReported>
231231
{

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24712471
}
24722472

24732473
pub fn mk_self_type(self) -> Ty<'tcx> {
2474-
self.mk_param(0, keywords::SelfType.name().as_str())
2474+
self.mk_param(0, keywords::SelfType.name().as_interned_str())
24752475
}
24762476

24772477
pub fn mk_param_from_def(self, def: &ty::TypeParameterDef) -> Ty<'tcx> {

src/librustc/ty/item_path.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ty::{self, Ty, TyCtxt};
1414
use middle::cstore::{ExternCrate, ExternCrateSource};
1515
use syntax::ast;
1616
use syntax::symbol::Symbol;
17-
use syntax::symbol::InternedString;
17+
use syntax::symbol::LocalInternedString;
1818

1919
use std::cell::Cell;
2020

@@ -131,7 +131,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
131131
{
132132
let visible_parent_map = self.visible_parent_map(LOCAL_CRATE);
133133

134-
let (mut cur_def, mut cur_path) = (external_def_id, Vec::<InternedString>::new());
134+
let (mut cur_def, mut cur_path) = (external_def_id, Vec::<LocalInternedString>::new());
135135
loop {
136136
// If `cur_def` is a direct or injected extern crate, push the path to the crate
137137
// followed by the path to the item within the crate and return.
@@ -168,8 +168,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
168168
}
169169

170170
let data = cur_def_key.disambiguated_data.data;
171-
let symbol =
172-
data.get_opt_name().unwrap_or_else(|| Symbol::intern("<unnamed>").as_str());
171+
let symbol = data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
172+
Symbol::intern("<unnamed>").as_str()
173+
});
173174
cur_path.push(symbol);
174175

175176
match visible_parent_map.get(&cur_def) {
@@ -221,7 +222,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
221222
data @ DefPathData::GlobalMetaData(..) => {
222223
let parent_def_id = self.parent_def_id(def_id).unwrap();
223224
self.push_item_path(buffer, parent_def_id);
224-
buffer.push(&data.as_interned_str());
225+
buffer.push(&data.as_interned_str().as_symbol().as_str());
225226
}
226227
DefPathData::StructCtor => { // present `X` instead of `X::{{constructor}}`
227228
let parent_def_id = self.parent_def_id(def_id).unwrap();

src/librustc/ty/maps/values.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'tcx> Value<'tcx> for Ty<'tcx> {
3737

3838
impl<'tcx> Value<'tcx> for ty::SymbolName {
3939
fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
40-
ty::SymbolName { name: Symbol::intern("<error>").as_str() }
40+
ty::SymbolName { name: Symbol::intern("<error>").as_interned_str() }
4141
}
4242
}
4343

src/librustc/ty/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use std::mem;
5151
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
5252
use syntax::attr;
5353
use syntax::ext::hygiene::Mark;
54-
use syntax::symbol::{Symbol, InternedString};
54+
use syntax::symbol::{Symbol, LocalInternedString, InternedString};
5555
use syntax_pos::{DUMMY_SP, Span};
5656

5757
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
@@ -2463,7 +2463,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24632463

24642464
pub fn item_name(self, id: DefId) -> InternedString {
24652465
if id.index == CRATE_DEF_INDEX {
2466-
self.original_crate_name(id.krate).as_str()
2466+
self.original_crate_name(id.krate).as_interned_str()
24672467
} else {
24682468
let def_key = self.def_key(id);
24692469
// The name of a StructCtor is that of its struct parent.
@@ -2820,15 +2820,13 @@ impl_stable_hash_for!(struct self::SymbolName {
28202820
impl SymbolName {
28212821
pub fn new(name: &str) -> SymbolName {
28222822
SymbolName {
2823-
name: Symbol::intern(name).as_str()
2823+
name: Symbol::intern(name).as_interned_str()
28242824
}
28252825
}
2826-
}
2827-
2828-
impl Deref for SymbolName {
2829-
type Target = str;
28302826

2831-
fn deref(&self) -> &str { &self.name }
2827+
pub fn as_str(&self) -> LocalInternedString {
2828+
self.name.as_str()
2829+
}
28322830
}
28332831

28342832
impl fmt::Display for SymbolName {

src/librustc/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
864864
}
865865

866866
pub fn for_self() -> ParamTy {
867-
ParamTy::new(0, keywords::SelfType.name().as_str())
867+
ParamTy::new(0, keywords::SelfType.name().as_interned_str())
868868
}
869869

870870
pub fn for_def(def: &ty::TypeParameterDef) -> ParamTy {

src/librustc/util/ppaux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl PrintContext {
462462
0 => Symbol::intern("'r"),
463463
1 => Symbol::intern("'s"),
464464
i => Symbol::intern(&format!("'t{}", i-2)),
465-
}.as_str()
465+
}.as_interned_str()
466466
}
467467

468468
// Replace any anonymous late-bound regions with named

src/librustc_driver/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
303303

304304
pub fn t_param(&self, index: u32) -> Ty<'tcx> {
305305
let name = format!("T{}", index);
306-
self.infcx.tcx.mk_param(index, Symbol::intern(&name).as_str())
306+
self.infcx.tcx.mk_param(index, Symbol::intern(&name).as_interned_str())
307307
}
308308

309309
pub fn re_early_bound(&self, index: u32, name: &'static str) -> ty::Region<'tcx> {
310-
let name = Symbol::intern(name).as_str();
310+
let name = Symbol::intern(name).as_interned_str();
311311
self.infcx.tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
312312
def_id: self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID),
313313
index,

src/librustc_incremental/assert_module_sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
7474

7575
let mname = self.field(attr, MODULE);
7676
let mangled_cgu_name = CodegenUnit::mangle_name(&mname.as_str());
77-
let mangled_cgu_name = Symbol::intern(&mangled_cgu_name).as_str();
77+
let mangled_cgu_name = Symbol::intern(&mangled_cgu_name).as_interned_str();
7878

7979
let dep_node = DepNode::new(self.tcx,
8080
DepConstructor::CompileCodegenUnit(mangled_cgu_name));

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl CrateStore for cstore::CStore {
535535
.insert(local_span, (name.to_string(), data.get_span(id.index, sess)));
536536

537537
LoadedMacro::MacroDef(ast::Item {
538-
ident: ast::Ident::from_str(&name),
538+
ident: ast::Ident::from_str(&name.as_str()),
539539
id: ast::DUMMY_NODE_ID,
540540
span: local_span,
541541
attrs: attrs.iter().cloned().collect(),

0 commit comments

Comments
 (0)