Skip to content

Commit 88d2999

Browse files
committed
Remove Symbol::gensym().
1 parent e57c0db commit 88d2999

File tree

8 files changed

+52
-43
lines changed

8 files changed

+52
-43
lines changed

Diff for: src/librustc_allocator/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
140140

141141
// Generate the submodule itself
142142
let name = f.kind.fn_name("allocator_abi");
143-
let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
143+
let allocator_abi = Ident::from_str(&name).gensym();
144144
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
145145
let module = f.cx.monotonic_expander().flat_map_item(module).pop().unwrap();
146146

Diff for: src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'a> Resolver<'a> {
314314
Ident::new(keywords::SelfLower.name(), new_span)
315315
),
316316
kind: ast::UseTreeKind::Simple(
317-
Some(Ident::new(Name::gensym("__dummy"), new_span)),
317+
Some(Ident::from_str_and_span("__dummy", new_span).gensym()),
318318
ast::DUMMY_NODE_ID,
319319
ast::DUMMY_NODE_ID,
320320
),

Diff for: src/libsyntax/diagnostics/plugin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ext::base::{ExtCtxt, MacEager, MacResult};
77
use crate::ext::build::AstBuilder;
88
use crate::parse::token;
99
use crate::ptr::P;
10-
use crate::symbol::{keywords, Symbol};
10+
use crate::symbol::keywords;
1111
use crate::tokenstream::{TokenTree};
1212

1313
use smallvec::smallvec;
@@ -121,13 +121,13 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>,
121121

122122
let span = span.apply_mark(ecx.current_expansion.mark);
123123

124-
let sym = Ident::new(Symbol::gensym(&format!("__register_diagnostic_{}", code)), span);
124+
let name = Ident::from_str_and_span(&format!("__register_diagnostic_{}", code), span).gensym();
125125

126126
MacEager::items(smallvec![
127127
ecx.item_mod(
128128
span,
129129
span,
130-
sym,
130+
name,
131131
vec![],
132132
vec![],
133133
)

Diff for: src/libsyntax/ext/tt/macro_rules.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ pub fn compile(
252252
def: &ast::Item,
253253
edition: Edition
254254
) -> SyntaxExtension {
255-
let lhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("lhs"));
256-
let rhs_nm = ast::Ident::with_empty_ctxt(Symbol::gensym("rhs"));
255+
let lhs_nm = ast::Ident::from_str("lhs").gensym();
256+
let rhs_nm = ast::Ident::from_str("rhs").gensym();
257257

258258
// Parse the macro_rules! invocation
259259
let body = match def.node {

Diff for: src/libsyntax/std_inject.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ast;
22
use crate::attr;
33
use crate::edition::Edition;
44
use crate::ext::hygiene::{Mark, SyntaxContext};
5-
use crate::symbol::{Symbol, keywords, sym};
5+
use crate::symbol::{Ident, Symbol, keywords, sym};
66
use crate::source_map::{ExpnInfo, MacroAttribute, dummy_spanned, hygiene, respan};
77
use crate::ptr::P;
88
use crate::tokenstream::TokenStream;
@@ -66,10 +66,12 @@ pub fn maybe_inject_crates_ref(
6666
for orig_name_str in names.iter().rev() {
6767
// HACK(eddyb) gensym the injected crates on the Rust 2018 edition,
6868
// so they don't accidentally interfere with the new import paths.
69+
let orig_name_sym = Symbol::intern(orig_name_str);
70+
let orig_name_ident = Ident::with_empty_ctxt(orig_name_sym);
6971
let (rename, orig_name) = if rust_2018 {
70-
(Symbol::gensym(orig_name_str), Some(Symbol::intern(orig_name_str)))
72+
(orig_name_ident.gensym(), Some(orig_name_sym))
7173
} else {
72-
(Symbol::intern(orig_name_str), None)
74+
(orig_name_ident, None)
7375
};
7476
krate.module.items.insert(0, P(ast::Item {
7577
attrs: vec![attr::mk_attr_outer(
@@ -79,7 +81,7 @@ pub fn maybe_inject_crates_ref(
7981
)],
8082
vis: dummy_spanned(ast::VisibilityKind::Inherited),
8183
node: ast::ItemKind::ExternCrate(alt_std_name.or(orig_name)),
82-
ident: ast::Ident::with_empty_ctxt(rename),
84+
ident: rename,
8385
id: ast::DUMMY_NODE_ID,
8486
span: DUMMY_SP,
8587
tokens: None,

Diff for: src/libsyntax/test.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ fn mk_reexport_mod(cx: &mut TestCtxt<'_>,
232232
items,
233233
};
234234

235-
let sym = Ident::with_empty_ctxt(Symbol::gensym("__test_reexports"));
235+
let name = Ident::from_str("__test_reexports").gensym();
236236
let parent = if parent == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { parent };
237237
cx.ext_cx.current_expansion.mark = cx.ext_cx.resolver.get_module_scope(parent);
238238
let it = cx.ext_cx.monotonic_expander().flat_map_item(P(ast::Item {
239-
ident: sym,
239+
ident: name,
240240
attrs: Vec::new(),
241241
id: ast::DUMMY_NODE_ID,
242242
node: ast::ItemKind::Mod(reexport_mod),
@@ -245,7 +245,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt<'_>,
245245
tokens: None,
246246
})).pop().unwrap();
247247

248-
(it, sym)
248+
(it, name)
249249
}
250250

251251
/// Crawl over the crate, inserting test reexports and the test main function
@@ -373,9 +373,10 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
373373
main_body);
374374

375375
// Honor the reexport_test_harness_main attribute
376-
let main_id = Ident::new(
377-
cx.reexport_test_harness_main.unwrap_or(Symbol::gensym("main")),
378-
sp);
376+
let main_id = match cx.reexport_test_harness_main {
377+
Some(sym) => Ident::new(sym, sp),
378+
None => Ident::from_str_and_span("main", sp).gensym(),
379+
};
379380

380381
P(ast::Item {
381382
ident: main_id,

Diff for: src/libsyntax_ext/proc_macro_decls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ fn mk_decls(
429429
let module = cx.item_mod(
430430
span,
431431
span,
432-
ast::Ident::with_empty_ctxt(Symbol::gensym("decls")),
432+
ast::Ident::from_str("decls").gensym(),
433433
vec![doc_hidden],
434434
vec![krate, decls_static],
435435
).map(|mut i| {

Diff for: src/libsyntax_pos/symbol.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,12 @@ pub struct Ident {
630630

631631
impl Ident {
632632
#[inline]
633+
/// Constructs a new identifier from a symbol and a span.
633634
pub const fn new(name: Symbol, span: Span) -> Ident {
634635
Ident { name, span }
635636
}
636637

638+
/// Constructs a new identifier with an empty syntax context.
637639
#[inline]
638640
pub const fn with_empty_ctxt(name: Symbol) -> Ident {
639641
Ident::new(name, DUMMY_SP)
@@ -644,11 +646,16 @@ impl Ident {
644646
Ident::with_empty_ctxt(string.as_symbol())
645647
}
646648

647-
/// Maps a string to an identifier with an empty syntax context.
649+
/// Maps a string to an identifier with an empty span.
648650
pub fn from_str(string: &str) -> Ident {
649651
Ident::with_empty_ctxt(Symbol::intern(string))
650652
}
651653

654+
/// Maps a string and a span to an identifier.
655+
pub fn from_str_and_span(string: &str, span: Span) -> Ident {
656+
Ident::new(Symbol::intern(string), span)
657+
}
658+
652659
/// Replaces `lo` and `hi` with those from `span`, but keep hygiene context.
653660
pub fn with_span_pos(self, span: Span) -> Ident {
654661
Ident::new(self.name, span.with_ctxt(self.span.ctxt()))
@@ -676,11 +683,14 @@ impl Ident {
676683
Ident::new(self.name, self.span.modern_and_legacy())
677684
}
678685

686+
/// Transforms an identifier into one with the same name, but gensymed.
679687
pub fn gensym(self) -> Ident {
680688
let name = with_interner(|interner| interner.gensymed(self.name));
681689
Ident::new(name, self.span)
682690
}
683691

692+
/// Transforms an underscore identifier into one with the same name, but
693+
/// gensymed. Leaves non-underscore identifiers unchanged.
684694
pub fn gensym_if_underscore(self) -> Ident {
685695
if self.name == keywords::Underscore.name() { self.gensym() } else { self }
686696
}
@@ -742,30 +752,34 @@ impl Decodable for Ident {
742752
Ok(if !string.starts_with('#') {
743753
Ident::from_str(&string)
744754
} else { // FIXME(jseyfried): intercrate hygiene
745-
Ident::with_empty_ctxt(Symbol::gensym(&string[1..]))
755+
Ident::from_str(&string[1..]).gensym()
746756
})
747757
}
748758
}
749759

750760
/// A symbol is an interned or gensymed string. A gensym is a symbol that is
751-
/// never equal to any other symbol. E.g.:
752-
/// ```
753-
/// assert_eq!(Symbol::intern("x"), Symbol::intern("x"))
754-
/// assert_ne!(Symbol::gensym("x"), Symbol::intern("x"))
755-
/// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x"))
756-
/// ```
761+
/// never equal to any other symbol.
762+
///
757763
/// Conceptually, a gensym can be thought of as a normal symbol with an
758764
/// invisible unique suffix. Gensyms are useful when creating new identifiers
759765
/// that must not match any existing identifiers, e.g. during macro expansion
760-
/// and syntax desugaring.
766+
/// and syntax desugaring. Because gensyms should always be identifiers, all
767+
/// gensym operations are on `Ident` rather than `Symbol`. (Indeed, in the
768+
/// future the gensym-ness may be moved from `Symbol` to hygiene data.)
761769
///
762-
/// Internally, a Symbol is implemented as an index, and all operations
770+
/// Examples:
771+
/// ```
772+
/// assert_eq!(Ident::from_str("x"), Ident::from_str("x"))
773+
/// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x"))
774+
/// assert_ne!(Ident::from_str("x").gensym(), Ident::from_str("x").gensym())
775+
/// ```
776+
/// Internally, a symbol is implemented as an index, and all operations
763777
/// (including hashing, equality, and ordering) operate on that index. The use
764778
/// of `newtype_index!` means that `Option<Symbol>` only takes up 4 bytes,
765779
/// because `newtype_index!` reserves the last 256 values for tagging purposes.
766780
///
767-
/// Note that `Symbol` cannot directly be a `newtype_index!` because it implements
768-
/// `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
781+
/// Note that `Symbol` cannot directly be a `newtype_index!` because it
782+
/// implements `fmt::Debug`, `Encodable`, and `Decodable` in special ways.
769783
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
770784
pub struct Symbol(SymbolIndex);
771785

@@ -783,11 +797,6 @@ impl Symbol {
783797
with_interner(|interner| interner.intern(string))
784798
}
785799

786-
/// Gensyms a new `usize`, using the current interner.
787-
pub fn gensym(string: &str) -> Self {
788-
with_interner(|interner| interner.gensym(string))
789-
}
790-
791800
pub fn as_str(self) -> LocalInternedString {
792801
with_interner(|interner| unsafe {
793802
LocalInternedString {
@@ -895,11 +904,6 @@ impl Interner {
895904
}
896905
}
897906

898-
fn gensym(&mut self, string: &str) -> Symbol {
899-
let symbol = self.intern(string);
900-
self.gensymed(symbol)
901-
}
902-
903907
fn gensymed(&mut self, symbol: Symbol) -> Symbol {
904908
self.gensyms.push(symbol);
905909
Symbol::new(SymbolIndex::MAX_AS_U32 - self.gensyms.len() as u32 + 1)
@@ -1267,11 +1271,13 @@ mod tests {
12671271
assert_eq!(i.intern("cat"), Symbol::new(1));
12681272
// dog is still at zero
12691273
assert_eq!(i.intern("dog"), Symbol::new(0));
1270-
assert_eq!(i.gensym("zebra"), Symbol::new(SymbolIndex::MAX_AS_U32));
1274+
let z = i.intern("zebra");
1275+
assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32));
12711276
// gensym of same string gets new number:
1272-
assert_eq!(i.gensym("zebra"), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
1277+
assert_eq!(i.gensymed(z), Symbol::new(SymbolIndex::MAX_AS_U32 - 1));
12731278
// gensym of *existing* string gets new number:
1274-
assert_eq!(i.gensym("dog"), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
1279+
let d = i.intern("dog");
1280+
assert_eq!(i.gensymed(d), Symbol::new(SymbolIndex::MAX_AS_U32 - 2));
12751281
}
12761282

12771283
#[test]

0 commit comments

Comments
 (0)