diff options
| author | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-22 19:25:39 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-27 13:58:38 +1000 |
| commit | 9c7d28d4fdd95bcd6062fb82a2dd2f280bda3e72 (patch) | |
| tree | e7008d43a01a6e8f055512953b51f91142e3fa97 /src/libsyntax_pos | |
| parent | 58c68d00fd1702b74e67dcb6f6f54483c066ef31 (diff) | |
| download | rust-9c7d28d4fdd95bcd6062fb82a2dd2f280bda3e72.tar.gz rust-9c7d28d4fdd95bcd6062fb82a2dd2f280bda3e72.zip | |
Pre-intern "0", "1", ..., "9", and use where appropriate.
Diffstat (limited to 'src/libsyntax_pos')
| -rw-r--r-- | src/libsyntax_pos/symbol.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index ce75094de59..6167a90b22a 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -9,10 +9,10 @@ use rustc_data_structures::newtype_index; use rustc_macros::symbols; use serialize::{Decodable, Decoder, Encodable, Encoder}; -use std::fmt; -use std::str; use std::cmp::{PartialEq, Ordering, PartialOrd, Ord}; +use std::fmt; use std::hash::{Hash, Hasher}; +use std::str; use crate::hygiene::SyntaxContext; use crate::{Span, DUMMY_SP, GLOBALS}; @@ -102,6 +102,9 @@ symbols! { // Symbols that can be referred to with syntax_pos::sym::*. The symbol is // the stringified identifier unless otherwise specified (e.g. // `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 { aarch64_target_feature, abi, @@ -966,8 +969,21 @@ pub mod kw { // This module has a very short name because it's used a lot. pub mod sym { + use std::convert::TryInto; use super::Symbol; + 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 { |
