Pre-intern "0", "1", ..., "9", and use where appropriate.

This commit is contained in:
Nicholas Nethercote 2019-05-22 19:25:39 +10:00
parent 58c68d00fd
commit 9c7d28d4fd
8 changed files with 48 additions and 13 deletions

View file

@ -2956,7 +2956,7 @@ impl<'a> LoweringContext<'a> {
ident: match f.ident { ident: match f.ident {
Some(ident) => ident, Some(ident) => ident,
// FIXME(jseyfried): positional field hygiene // FIXME(jseyfried): positional field hygiene
None => Ident::new(Symbol::intern(&index.to_string()), f.span), None => Ident::new(sym::integer(index), f.span),
}, },
vis: self.lower_visibility(&f.vis, None), vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()), ty: self.lower_ty(&f.ty, ImplTraitContext::disallowed()),

View file

@ -5,8 +5,7 @@ use crate::session::CrateDisambiguator;
use syntax::ast::*; use syntax::ast::*;
use syntax::ext::hygiene::Mark; use syntax::ext::hygiene::Mark;
use syntax::visit; use syntax::visit;
use syntax::symbol::kw; use syntax::symbol::{kw, sym};
use syntax::symbol::Symbol;
use syntax::parse::token::{self, Token}; use syntax::parse::token::{self, Token};
use syntax_pos::Span; use syntax_pos::Span;
@ -221,7 +220,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
_: &'a Generics, _: NodeId, _: Span) { _: &'a Generics, _: NodeId, _: Span) {
for (index, field) in data.fields().iter().enumerate() { for (index, field) in data.fields().iter().enumerate() {
let name = field.ident.map(|ident| ident.name) let name = field.ident.map(|ident| ident.name)
.unwrap_or_else(|| Symbol::intern(&index.to_string())); .unwrap_or_else(|| sym::integer(index));
let def = self.create_def(field.id, let def = self.create_def(field.id,
DefPathData::ValueNs(name.as_interned_str()), DefPathData::ValueNs(name.as_interned_str()),
field.span); field.span);

View file

@ -1316,7 +1316,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) { for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2) let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string()))); let interior = InteriorField(FieldIndex(i, sym::integer(i)));
let subcmt = Rc::new( let subcmt = Rc::new(
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior)); self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
self.cat_pattern_(subcmt, &subpat, op)?; self.cat_pattern_(subcmt, &subpat, op)?;
@ -1363,7 +1363,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
}; };
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) { for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2) let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string()))); let interior = InteriorField(FieldIndex(i, sym::integer(i)));
let subcmt = Rc::new( let subcmt = Rc::new(
self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior)); self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
self.cat_pattern_(subcmt, &subpat, op)?; self.cat_pattern_(subcmt, &subpat, op)?;

View file

@ -96,6 +96,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
let mut keyword_stream = quote! {}; let mut keyword_stream = quote! {};
let mut symbols_stream = quote! {}; let mut symbols_stream = quote! {};
let mut digits_stream = quote! {};
let mut prefill_stream = quote! {}; let mut prefill_stream = quote! {};
let mut counter = 0u32; let mut counter = 0u32;
let mut keys = HashSet::<String>::new(); let mut keys = HashSet::<String>::new();
@ -106,6 +107,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
} }
}; };
// Generate the listed keywords.
for keyword in &input.keywords.0 { for keyword in &input.keywords.0 {
let name = &keyword.name; let name = &keyword.name;
let value = &keyword.value; let value = &keyword.value;
@ -119,6 +121,7 @@ pub fn symbols(input: TokenStream) -> TokenStream {
counter += 1; counter += 1;
} }
// Generate the listed symbols.
for symbol in &input.symbols.0 { for symbol in &input.symbols.0 {
let name = &symbol.name; let name = &symbol.name;
let value = match &symbol.value { let value = match &symbol.value {
@ -135,6 +138,19 @@ pub fn symbols(input: TokenStream) -> TokenStream {
counter += 1; counter += 1;
} }
// Generate symbols for the strings "0", "1", ..., "9".
for n in 0..10 {
let n = n.to_string();
check_dup(&n);
prefill_stream.extend(quote! {
#n,
});
digits_stream.extend(quote! {
Symbol::new(#counter),
});
counter += 1;
}
let tt = TokenStream::from(quote! { let tt = TokenStream::from(quote! {
macro_rules! keywords { macro_rules! keywords {
() => { () => {
@ -145,6 +161,10 @@ pub fn symbols(input: TokenStream) -> TokenStream {
macro_rules! symbols { macro_rules! symbols {
() => { () => {
#symbols_stream #symbols_stream
pub const digits_array: &[Symbol; 10] = &[
#digits_stream
];
} }
} }

View file

@ -2,7 +2,7 @@ use std::fmt::Write;
use std::hash::Hash; use std::hash::Hash;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use syntax_pos::symbol::Symbol; use syntax_pos::symbol::{sym, Symbol};
use rustc::hir; use rustc::hir;
use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf, VariantIdx}; use rustc::ty::layout::{self, Size, Align, TyLayout, LayoutOf, VariantIdx};
use rustc::ty; use rustc::ty;
@ -188,7 +188,7 @@ impl<'rt, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> ValidityVisitor<'rt, 'a, '
PathElem::ClosureVar(name.unwrap_or_else(|| { PathElem::ClosureVar(name.unwrap_or_else(|| {
// Fall back to showing the field index. // Fall back to showing the field index.
Symbol::intern(&field.to_string()) sym::integer(field)
})) }))
} }

View file

@ -1,7 +1,7 @@
use crate::ast::{self, Ident}; use crate::ast::{self, Ident};
use crate::parse::ParseSess; use crate::parse::ParseSess;
use crate::parse::token::{self, Token}; use crate::parse::token::{self, Token};
use crate::symbol::Symbol; use crate::symbol::{sym, Symbol};
use crate::parse::unescape; use crate::parse::unescape;
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char}; use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};
@ -754,7 +754,7 @@ impl<'a> StringReader<'a> {
} }
_ => { _ => {
// just a 0 // just a 0
return (token::Integer, self.name_from(start_bpos)); return (token::Integer, sym::integer(0));
} }
} }
} else if c.is_digit(10) { } else if c.is_digit(10) {

View file

@ -197,7 +197,7 @@ impl LitKind {
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())), ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
ast::LitIntType::Unsuffixed => None, ast::LitIntType::Unsuffixed => None,
}; };
(token::Integer, Symbol::intern(&n.to_string()), suffix) (token::Integer, sym::integer(n), suffix)
} }
LitKind::Float(symbol, ty) => { LitKind::Float(symbol, ty) => {
(token::Float, symbol, Some(Symbol::intern(ty.ty_to_string()))) (token::Float, symbol, Some(Symbol::intern(ty.ty_to_string())))

View file

@ -9,10 +9,10 @@ use rustc_data_structures::newtype_index;
use rustc_macros::symbols; use rustc_macros::symbols;
use serialize::{Decodable, Decoder, Encodable, Encoder}; use serialize::{Decodable, Decoder, Encodable, Encoder};
use std::fmt;
use std::str;
use std::cmp::{PartialEq, Ordering, PartialOrd, Ord}; use std::cmp::{PartialEq, Ordering, PartialOrd, Ord};
use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::str;
use crate::hygiene::SyntaxContext; use crate::hygiene::SyntaxContext;
use crate::{Span, DUMMY_SP, GLOBALS}; use crate::{Span, DUMMY_SP, GLOBALS};
@ -102,6 +102,9 @@ symbols! {
// Symbols that can be referred to with syntax_pos::sym::*. The symbol is // Symbols that can be referred to with syntax_pos::sym::*. The symbol is
// the stringified identifier unless otherwise specified (e.g. // the stringified identifier unless otherwise specified (e.g.
// `proc_dash_macro` represents "proc-macro"). // `proc_dash_macro` represents "proc-macro").
//
// As well as the symbols listed, there are symbols for the the strings
// "0", "1", ..., "9", which are accessible via `sym::integer`.
Symbols { Symbols {
aarch64_target_feature, aarch64_target_feature,
abi, abi,
@ -966,8 +969,21 @@ pub mod kw {
// This module has a very short name because it's used a lot. // This module has a very short name because it's used a lot.
pub mod sym { pub mod sym {
use std::convert::TryInto;
use super::Symbol; use super::Symbol;
symbols!(); symbols!();
// Get the symbol for an integer. The first few non-negative integers each
// have a static symbol and therefore are fast.
pub fn integer<N: TryInto<usize> + Copy + ToString>(n: N) -> Symbol {
if let Result::Ok(idx) = n.try_into() {
if let Option::Some(&sym) = digits_array.get(idx) {
return sym;
}
}
Symbol::intern(&n.to_string())
}
} }
impl Symbol { impl Symbol {