use crate::attr;
use crate::edition::Edition;
use crate::ext::hygiene::{Mark, SyntaxContext};
-use crate::symbol::{Symbol, keywords, sym};
+use crate::symbol::{Ident, Symbol, keywords, sym};
use crate::source_map::{ExpnInfo, MacroAttribute, dummy_spanned, hygiene, respan};
use crate::ptr::P;
use crate::tokenstream::TokenStream;
for orig_name_str in names.iter().rev() {
// HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
// so they don't accidentally interfere with the new import paths.
+ let orig_name_sym = Symbol::intern(orig_name_str);
+ let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
let (rename, orig_name) = if rust_2018 {
- (Symbol::gensym(orig_name_str), Some(Symbol::intern(orig_name_str)))
+ (orig_name_ident.gensym(), Some(orig_name_sym))
} else {
- (Symbol::intern(orig_name_str), None)
+ (orig_name_ident, None)
};
krate.module.items.insert(0, P(ast::Item {
attrs: vec![attr::mk_attr_outer(
)],
vis: dummy_spanned(ast::VisibilityKind::Inherited),
node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
- ident: ast::Ident::with_empty_ctxt(rename),
+ ident: rename,
id: ast::DUMMY_NODE_ID,
span: DUMMY_SP,
tokens: None,
impl Ident {
#[inline]
+ /// Constructs a new identifier from a symbol and a span.
pub const fn new(name: Symbol, span: Span) -> Ident {
Ident { name, span }
}
+ /// Constructs a new identifier with an empty syntax context.
#[inline]
pub const fn with_empty_ctxt(name: Symbol) -> Ident {
Ident::new(name, DUMMY_SP)
Ident::with_empty_ctxt(string.as_symbol())
}
- /// Maps a string to an identifier with an empty syntax context.
+ /// Maps a string to an identifier with an empty span.
pub fn from_str(string: &str) -> Ident {
Ident::with_empty_ctxt(Symbol::intern(string))
}
+ /// Maps a string and a span to an identifier.
+ pub fn from_str_and_span(string: &str, span: Span) -> Ident {
+ Ident::new(Symbol::intern(string), span)
+ }
+
/// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
pub fn with_span_pos(self, span: Span) -> Ident {
Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
Ident::new(self.name, self.span.modern_and_legacy())
}
+ /// Transforms an identifier into one with the same name, but gensymed.
pub fn gensym(self) -> Ident {
let name = with_interner(|interner| interner.gensymed(self.name));
Ident::new(name, self.span)
}
+ /// Transforms an underscore identifier into one with the same name, but
+ /// gensymed. Leaves non-underscore identifiers unchanged.
pub fn gensym_if_underscore(self) -> Ident {
if self.name == keywords::Underscore.name() { self.gensym() } else { self }
}
Ok(if !string.starts_with('#') {
Ident::from_str(&string)
} else { // FIXME(jseyfried): intercrate hygiene
- Ident::with_empty_ctxt(Symbol::gensym(&string[1..]))
+ Ident::from_str(&string[1..]).gensym()
})
}
}
/// A symbol is an interned or gensymed string. A gensym is a symbol that is
-/// never equal to any other symbol. E.g.:
-/// ```
-/// assert_eq!(Symbol::intern("x"), Symbol::intern("x"))
-/// assert_ne!(Symbol::gensym("x"), Symbol::intern("x"))
-/// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
-/// ```
+/// never equal to any other symbol.
+///
/// Conceptually, a gensym can be thought of as a normal symbol with an
/// invisible unique suffix. Gensyms are useful when creating new identifiers
/// that must not match any existing identifiers, e.g. during macro expansion
-/// and syntax desugaring.
+/// and syntax desugaring. Because gensyms should always be identifiers, all
+/// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
+/// future the gensym-ness may be moved from `Symbol` to hygiene data.)
///
-/// Internally, a Symbol is implemented as an index, and all operations
+/// Examples:
+/// ```
+/// assert_eq!(Ident::from_str("x"), Ident::from_str("x"))
+/// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x"))
+/// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x").gensym())
+/// ```
+/// Internally, a symbol is implemented as an index, and all operations
/// (including hashing, equality, and ordering) operate on that index. The use
/// of `newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
/// because `newtype_index!` reserves the last 256 values for tagging purposes.
///
-/// Note that `Symbol` cannot directly be a `newtype_index!` because it implements
-/// `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
+/// Note that `Symbol` cannot directly be a `newtype_index!` because it
+/// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Symbol(SymbolIndex);
with_interner(|interner| interner.intern(string))
}
- /// Gensyms a new `usize`, using the current interner.
- pub fn gensym(string: &str) -> Self {
- with_interner(|interner| interner.gensym(string))
- }
-
pub fn as_str(self) -> LocalInternedString {
with_interner(|interner| unsafe {
LocalInternedString {
}
}
- fn gensym(&mut self, string: &str) -> Symbol {
- let symbol = self.intern(string);
- self.gensymed(symbol)
- }
-
fn gensymed(&mut self, symbol: Symbol) -> Symbol {
self.gensyms.push(symbol);
Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
assert_eq!(i.intern("cat"), Symbol::new(1));
// dog is still at zero
assert_eq!(i.intern("dog"), Symbol::new(0));
- assert_eq!(i.gensym("zebra"), Symbol::new(SymbolIndex::MAX_AS_U32));
+ let z = i.intern("zebra");
+ assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32));
// gensym of same string gets new number:
- assert_eq!(i.gensym("zebra"), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
+ assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
// gensym of *existing* string gets new number:
- assert_eq!(i.gensym("dog"), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
+ let d = i.intern("dog");
+ assert_eq!(i.gensymed(d), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
}
#[test]