Skip to content

Commit ebec554

Browse files
authored
Auto merge of #37824 - jseyfried:symbols, r=eddyb
Clean up `ast::Attribute`, `ast::CrateConfig`, and string interning This PR - removes `ast::Attribute_` (changing `Attribute` from `Spanned<Attribute_>` to a struct), - moves a `MetaItem`'s name from the `MetaItemKind` variants to a field of `MetaItem`, - avoids needlessly wrapping `ast::MetaItem` with `P`, - moves string interning into `syntax::symbol` (`ast::Name` is a reexport of `symbol::Symbol` for now), - replaces `InternedString` with `Symbol` in the AST, HIR, and various other places, and - refactors `ast::CrateConfig` from a `Vec` to a `HashSet`. r? @eddyb
2 parents 59b87b3 + a8e86f0 commit ebec554

File tree

164 files changed

+1519
-1829
lines changed

Some content is hidden

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

164 files changed

+1519
-1829
lines changed

src/libproc_macro_plugin/qquote.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ use syntax::codemap::Span;
3434
use syntax::ext::base::*;
3535
use syntax::ext::base;
3636
use syntax::ext::proc_macro_shim::build_block_emitter;
37-
use syntax::parse::token::{self, Token, gensym_ident, str_to_ident};
37+
use syntax::parse::token::{self, Token};
3838
use syntax::print::pprust;
39+
use syntax::symbol::Symbol;
3940
use syntax::tokenstream::{TokenTree, TokenStream};
4041

4142
// ____________________________________________________________________________________________
@@ -124,7 +125,7 @@ fn qquote_iter<'cx>(cx: &'cx mut ExtCtxt, depth: i64, ts: TokenStream) -> (Bindi
124125
} // produce an error or something first
125126
let exp = vec![exp.unwrap().to_owned()];
126127
debug!("RHS: {:?}", exp.clone());
127-
let new_id = gensym_ident("tmp");
128+
let new_id = Ident::with_empty_ctxt(Symbol::gensym("tmp"));
128129
debug!("RHS TS: {:?}", TokenStream::from_tts(exp.clone()));
129130
debug!("RHS TS TT: {:?}", TokenStream::from_tts(exp.clone()).to_vec());
130131
bindings.push((new_id, TokenStream::from_tts(exp)));
@@ -179,7 +180,7 @@ fn unravel_concats(tss: Vec<TokenStream>) -> TokenStream {
179180
};
180181

181182
while let Some(ts) = pushes.pop() {
182-
output = build_fn_call(str_to_ident("concat"),
183+
output = build_fn_call(Ident::from_str("concat"),
183184
concat(concat(ts,
184185
from_tokens(vec![Token::Comma])),
185186
output));
@@ -209,18 +210,19 @@ fn convert_complex_tts<'cx>(cx: &'cx mut ExtCtxt, tts: Vec<QTT>) -> (Bindings, T
209210
// FIXME handle sequence repetition tokens
210211
QTT::QDL(qdl) => {
211212
debug!(" QDL: {:?} ", qdl.tts);
212-
let new_id = gensym_ident("qdl_tmp");
213+
let new_id = Ident::with_empty_ctxt(Symbol::gensym("qdl_tmp"));
213214
let mut cct_rec = convert_complex_tts(cx, qdl.tts);
214215
bindings.append(&mut cct_rec.0);
215216
bindings.push((new_id, cct_rec.1));
216217

217218
let sep = build_delim_tok(qdl.delim);
218219

219-
pushes.push(build_mod_call(vec![str_to_ident("proc_macro_tokens"),
220-
str_to_ident("build"),
221-
str_to_ident("build_delimited")],
222-
concat(from_tokens(vec![Token::Ident(new_id)]),
223-
concat(lex(","), sep))));
220+
pushes.push(build_mod_call(
221+
vec![Ident::from_str("proc_macro_tokens"),
222+
Ident::from_str("build"),
223+
Ident::from_str("build_delimited")],
224+
concat(from_tokens(vec![Token::Ident(new_id)]), concat(lex(","), sep)),
225+
));
224226
}
225227
QTT::QIdent(t) => {
226228
pushes.push(TokenStream::from_tts(vec![t]));
@@ -250,13 +252,13 @@ fn unravel(binds: Bindings) -> TokenStream {
250252

251253
/// Checks if the Ident is `unquote`.
252254
fn is_unquote(id: Ident) -> bool {
253-
let qq = str_to_ident("unquote");
255+
let qq = Ident::from_str("unquote");
254256
id.name == qq.name // We disregard context; unquote is _reserved_
255257
}
256258

257259
/// Checks if the Ident is `quote`.
258260
fn is_qquote(id: Ident) -> bool {
259-
let qq = str_to_ident("qquote");
261+
let qq = Ident::from_str("qquote");
260262
id.name == qq.name // We disregard context; qquote is _reserved_
261263
}
262264

@@ -266,7 +268,8 @@ mod int_build {
266268

267269
use syntax::ast::{self, Ident};
268270
use syntax::codemap::{DUMMY_SP};
269-
use syntax::parse::token::{self, Token, keywords, str_to_ident};
271+
use syntax::parse::token::{self, Token, Lit};
272+
use syntax::symbol::keywords;
270273
use syntax::tokenstream::{TokenTree, TokenStream};
271274

272275
// ____________________________________________________________________________________________
@@ -277,19 +280,19 @@ mod int_build {
277280
build_paren_delimited(build_vec(build_token_tt(t))))
278281
}
279282

280-
pub fn emit_lit(l: token::Lit, n: Option<ast::Name>) -> TokenStream {
283+
pub fn emit_lit(l: Lit, n: Option<ast::Name>) -> TokenStream {
281284
let suf = match n {
282-
Some(n) => format!("Some(ast::Name({}))", n.0),
285+
Some(n) => format!("Some(ast::Name({}))", n.as_u32()),
283286
None => "None".to_string(),
284287
};
285288

286289
let lit = match l {
287-
token::Lit::Byte(n) => format!("Lit::Byte(token::intern(\"{}\"))", n.to_string()),
288-
token::Lit::Char(n) => format!("Lit::Char(token::intern(\"{}\"))", n.to_string()),
289-
token::Lit::Integer(n) => format!("Lit::Integer(token::intern(\"{}\"))", n.to_string()),
290-
token::Lit::Float(n) => format!("Lit::Float(token::intern(\"{}\"))", n.to_string()),
291-
token::Lit::Str_(n) => format!("Lit::Str_(token::intern(\"{}\"))", n.to_string()),
292-
token::Lit::ByteStr(n) => format!("Lit::ByteStr(token::intern(\"{}\"))", n.to_string()),
290+
Lit::Byte(n) => format!("Lit::Byte(Symbol::intern(\"{}\"))", n.to_string()),
291+
Lit::Char(n) => format!("Lit::Char(Symbol::intern(\"{}\"))", n.to_string()),
292+
Lit::Float(n) => format!("Lit::Float(Symbol::intern(\"{}\"))", n.to_string()),
293+
Lit::Str_(n) => format!("Lit::Str_(Symbol::intern(\"{}\"))", n.to_string()),
294+
Lit::Integer(n) => format!("Lit::Integer(Symbol::intern(\"{}\"))", n.to_string()),
295+
Lit::ByteStr(n) => format!("Lit::ByteStr(Symbol::intern(\"{}\"))", n.to_string()),
293296
_ => panic!("Unsupported literal"),
294297
};
295298

@@ -388,9 +391,10 @@ mod int_build {
388391
Token::Underscore => lex("_"),
389392
Token::Literal(lit, sfx) => emit_lit(lit, sfx),
390393
// fix ident expansion information... somehow
391-
Token::Ident(ident) => lex(&format!("Token::Ident(str_to_ident(\"{}\"))", ident.name)),
392-
Token::Lifetime(ident) => lex(&format!("Token::Ident(str_to_ident(\"{}\"))",
393-
ident.name)),
394+
Token::Ident(ident) =>
395+
lex(&format!("Token::Ident(Ident::from_str(\"{}\"))", ident.name)),
396+
Token::Lifetime(ident) =>
397+
lex(&format!("Token::Ident(Ident::from_str(\"{}\"))", ident.name)),
394398
_ => panic!("Unhandled case!"),
395399
}
396400
}
@@ -408,7 +412,7 @@ mod int_build {
408412

409413
/// Takes `input` and returns `vec![input]`.
410414
pub fn build_vec(ts: TokenStream) -> TokenStream {
411-
build_mac_call(str_to_ident("vec"), ts)
415+
build_mac_call(Ident::from_str("vec"), ts)
412416
// tts.clone().to_owned()
413417
}
414418

src/libproc_macro_tokens/build.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ extern crate syntax_pos;
1313

1414
use syntax::ast::Ident;
1515
use syntax::codemap::DUMMY_SP;
16-
use syntax::parse::token::{self, Token, keywords, str_to_ident};
16+
use syntax::parse::token::{self, Token};
17+
use syntax::symbol::keywords;
1718
use syntax::tokenstream::{self, TokenTree, TokenStream};
1819
use std::rc::Rc;
1920

@@ -43,13 +44,13 @@ pub fn ident_eq(tident: &TokenTree, id: Ident) -> bool {
4344

4445
/// Convert a `&str` into a Token.
4546
pub fn str_to_token_ident(s: &str) -> Token {
46-
Token::Ident(str_to_ident(s))
47+
Token::Ident(Ident::from_str(s))
4748
}
4849

4950
/// Converts a keyword (from `syntax::parse::token::keywords`) into a Token that
5051
/// corresponds to it.
5152
pub fn keyword_to_token_ident(kw: keywords::Keyword) -> Token {
52-
Token::Ident(str_to_ident(&kw.name().as_str()[..]))
53+
Token::Ident(Ident::from_str(&kw.name().as_str()[..]))
5354
}
5455

5556
// ____________________________________________________________________________________________

src/librustc/hir/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a> CheckAttrVisitor<'a> {
6464
None => continue,
6565
};
6666

67-
let (message, label) = match &*name {
67+
let (message, label) = match &*name.as_str() {
6868
"C" => {
6969
conflicting_reprs += 1;
7070
if target != Target::Struct &&
@@ -120,7 +120,7 @@ impl<'a> CheckAttrVisitor<'a> {
120120
}
121121

122122
fn check_attribute(&self, attr: &ast::Attribute, target: Target) {
123-
let name: &str = &attr.name();
123+
let name: &str = &attr.name().as_str();
124124
match name {
125125
"inline" => self.check_inline(attr, target),
126126
"repr" => self.check_repr(attr, target),

src/librustc/hir/lowering.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ use syntax::ast::*;
5353
use syntax::errors;
5454
use syntax::ptr::P;
5555
use syntax::codemap::{respan, Spanned};
56-
use syntax::parse::token;
5756
use syntax::std_inject;
57+
use syntax::symbol::{Symbol, keywords};
5858
use syntax::visit::{self, Visitor};
5959
use syntax_pos::Span;
6060

@@ -149,7 +149,7 @@ impl<'a> LoweringContext<'a> {
149149
}
150150

151151
fn str_to_ident(&self, s: &'static str) -> Name {
152-
token::gensym(s)
152+
Symbol::gensym(s)
153153
}
154154

155155
fn with_parent_def<T, F>(&mut self, parent_id: NodeId, f: F) -> T
@@ -400,8 +400,8 @@ impl<'a> LoweringContext<'a> {
400400
// Don't expose `Self` (recovered "keyword used as ident" parse error).
401401
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
402402
// Instead, use gensym("Self") to create a distinct name that looks the same.
403-
if name == token::keywords::SelfType.name() {
404-
name = token::gensym("Self");
403+
if name == keywords::SelfType.name() {
404+
name = Symbol::gensym("Self");
405405
}
406406

407407
hir::TyParam {
@@ -540,7 +540,7 @@ impl<'a> LoweringContext<'a> {
540540
hir::StructField {
541541
span: f.span,
542542
id: f.id,
543-
name: f.ident.map(|ident| ident.name).unwrap_or(token::intern(&index.to_string())),
543+
name: f.ident.map(|ident| ident.name).unwrap_or(Symbol::intern(&index.to_string())),
544544
vis: self.lower_visibility(&f.vis),
545545
ty: self.lower_ty(&f.ty),
546546
attrs: self.lower_attrs(&f.attrs),
@@ -1189,7 +1189,7 @@ impl<'a> LoweringContext<'a> {
11891189
e.span,
11901190
hir::PopUnstableBlock,
11911191
ThinVec::new());
1192-
this.field(token::intern(s), signal_block, ast_expr.span)
1192+
this.field(Symbol::intern(s), signal_block, ast_expr.span)
11931193
}).collect();
11941194
let attrs = ast_expr.attrs.clone();
11951195

@@ -1953,9 +1953,9 @@ impl<'a> LoweringContext<'a> {
19531953
fn std_path_components(&mut self, components: &[&str]) -> Vec<Name> {
19541954
let mut v = Vec::new();
19551955
if let Some(s) = self.crate_root {
1956-
v.push(token::intern(s));
1956+
v.push(Symbol::intern(s));
19571957
}
1958-
v.extend(components.iter().map(|s| token::intern(s)));
1958+
v.extend(components.iter().map(|s| Symbol::intern(s)));
19591959
return v;
19601960
}
19611961

src/librustc/hir/map/def_collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use middle::cstore::InlinedItem;
1919
use syntax::ast::*;
2020
use syntax::ext::hygiene::Mark;
2121
use syntax::visit;
22-
use syntax::parse::token::{self, keywords};
22+
use syntax::symbol::{Symbol, keywords};
2323

2424
/// Creates def ids for nodes in the HIR.
2525
pub struct DefCollector<'a> {
@@ -169,7 +169,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
169169
this.with_parent(variant_def_index, |this| {
170170
for (index, field) in v.node.data.fields().iter().enumerate() {
171171
let name = field.ident.map(|ident| ident.name)
172-
.unwrap_or_else(|| token::intern(&index.to_string()));
172+
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
173173
this.create_def(field.id, DefPathData::Field(name.as_str()));
174174
}
175175

@@ -188,7 +188,7 @@ impl<'a> visit::Visitor for DefCollector<'a> {
188188

189189
for (index, field) in struct_def.fields().iter().enumerate() {
190190
let name = field.ident.map(|ident| ident.name.as_str())
191-
.unwrap_or(token::intern(&index.to_string()).as_str());
191+
.unwrap_or(Symbol::intern(&index.to_string()).as_str());
192192
this.create_def(field.id, DefPathData::Field(name));
193193
}
194194
}

src/librustc/hir/map/definitions.rs

+19-38
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::fmt::Write;
1414
use std::hash::{Hash, Hasher};
1515
use std::collections::hash_map::DefaultHasher;
1616
use syntax::ast;
17-
use syntax::parse::token::{self, InternedString};
17+
use syntax::symbol::{Symbol, InternedString};
1818
use ty::TyCtxt;
1919
use util::nodemap::NodeMap;
2020

@@ -115,9 +115,9 @@ impl DefPath {
115115
pub fn to_string(&self, tcx: TyCtxt) -> String {
116116
let mut s = String::with_capacity(self.data.len() * 16);
117117

118-
s.push_str(&tcx.original_crate_name(self.krate));
118+
s.push_str(&tcx.original_crate_name(self.krate).as_str());
119119
s.push_str("/");
120-
s.push_str(&tcx.crate_disambiguator(self.krate));
120+
s.push_str(&tcx.crate_disambiguator(self.krate).as_str());
121121

122122
for component in &self.data {
123123
write!(s,
@@ -137,8 +137,8 @@ impl DefPath {
137137
}
138138

139139
pub fn deterministic_hash_to<H: Hasher>(&self, tcx: TyCtxt, state: &mut H) {
140-
tcx.original_crate_name(self.krate).hash(state);
141-
tcx.crate_disambiguator(self.krate).hash(state);
140+
tcx.original_crate_name(self.krate).as_str().hash(state);
141+
tcx.crate_disambiguator(self.krate).as_str().hash(state);
142142
self.data.hash(state);
143143
}
144144
}
@@ -328,7 +328,7 @@ impl DefPathData {
328328
LifetimeDef(ref name) |
329329
EnumVariant(ref name) |
330330
Binding(ref name) |
331-
Field(ref name) => Some(token::intern(name)),
331+
Field(ref name) => Some(Symbol::intern(name)),
332332

333333
Impl |
334334
CrateRoot |
@@ -343,7 +343,7 @@ impl DefPathData {
343343

344344
pub fn as_interned_str(&self) -> InternedString {
345345
use self::DefPathData::*;
346-
match *self {
346+
let s = match *self {
347347
TypeNs(ref name) |
348348
ValueNs(ref name) |
349349
Module(ref name) |
@@ -353,43 +353,24 @@ impl DefPathData {
353353
EnumVariant(ref name) |
354354
Binding(ref name) |
355355
Field(ref name) => {
356-
name.clone()
357-
}
358-
359-
Impl => {
360-
InternedString::new("{{impl}}")
356+
return name.clone();
361357
}
362358

363359
// note that this does not show up in user printouts
364-
CrateRoot => {
365-
InternedString::new("{{root}}")
366-
}
360+
CrateRoot => "{{root}}",
367361

368362
// note that this does not show up in user printouts
369-
InlinedRoot(_) => {
370-
InternedString::new("{{inlined-root}}")
371-
}
372-
373-
Misc => {
374-
InternedString::new("{{?}}")
375-
}
376-
377-
ClosureExpr => {
378-
InternedString::new("{{closure}}")
379-
}
380-
381-
StructCtor => {
382-
InternedString::new("{{constructor}}")
383-
}
384-
385-
Initializer => {
386-
InternedString::new("{{initializer}}")
387-
}
363+
InlinedRoot(_) => "{{inlined-root}}",
364+
365+
Impl => "{{impl}}",
366+
Misc => "{{?}}",
367+
ClosureExpr => "{{closure}}",
368+
StructCtor => "{{constructor}}",
369+
Initializer => "{{initializer}}",
370+
ImplTrait => "{{impl-Trait}}",
371+
};
388372

389-
ImplTrait => {
390-
InternedString::new("{{impl-Trait}}")
391-
}
392-
}
373+
Symbol::intern(s).as_str()
393374
}
394375

395376
pub fn to_string(&self) -> String {

src/librustc/hir/map/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ impl<'a, 'ast> NodesMatchingSuffix<'a, 'ast> {
765765
None => return false,
766766
Some((node_id, name)) => (node_id, name),
767767
};
768-
if &part[..] != mod_name.as_str() {
768+
if mod_name != &**part {
769769
return false;
770770
}
771771
cursor = self.map.get_parent(mod_id);
@@ -803,8 +803,7 @@ impl<'a, 'ast> NodesMatchingSuffix<'a, 'ast> {
803803
// We are looking at some node `n` with a given name and parent
804804
// id; do their names match what I am seeking?
805805
fn matches_names(&self, parent_of_n: NodeId, name: Name) -> bool {
806-
name.as_str() == &self.item_name[..] &&
807-
self.suffix_matches(parent_of_n)
806+
name == &**self.item_name && self.suffix_matches(parent_of_n)
808807
}
809808
}
810809

0 commit comments

Comments
 (0)