2222//! cargo expand > /tmp/rustc_span.rs # it's a big file
2323//! ```
2424
25+ use indexmap:: IndexMap ;
2526use proc_macro2:: { Span , TokenStream } ;
2627use quote:: quote;
27- use std:: collections:: HashMap ;
2828use syn:: parse:: { Parse , ParseStream , Result } ;
2929use syn:: { braced, punctuated:: Punctuated , Expr , Ident , Lit , LitStr , Macro , Token } ;
3030
@@ -136,30 +136,29 @@ pub fn symbols(input: TokenStream) -> TokenStream {
136136 output
137137}
138138
139- struct Preinterned {
140- idx : u32 ,
141- span_of_name : Span ,
142- }
143-
144139struct Entries {
145- map : HashMap < String , Preinterned > ,
140+ map : IndexMap < String , Span > ,
146141}
147142
148143impl Entries {
149144 fn with_capacity ( capacity : usize ) -> Self {
150- Entries { map : HashMap :: with_capacity ( capacity) }
145+ Entries { map : IndexMap :: with_capacity ( capacity) }
151146 }
152147
153148 fn insert ( & mut self , span : Span , str : & str , errors : & mut Errors ) -> u32 {
154- if let Some ( prev) = self . map . get ( str) {
155- errors. error ( span, format ! ( "Symbol `{str}` is duplicated" ) ) ;
156- errors. error ( prev. span_of_name , "location of previous definition" . to_string ( ) ) ;
157- prev. idx
158- } else {
159- let idx = self . len ( ) ;
160- self . map . insert ( str. to_string ( ) , Preinterned { idx, span_of_name : span } ) ;
161- idx
162- }
149+ let idx = match self . map . entry ( str. to_string ( ) ) {
150+ indexmap:: map:: Entry :: Occupied ( prev) => {
151+ errors. error ( span, format ! ( "Symbol `{str}` is duplicated" ) ) ;
152+ errors. error ( * prev. get ( ) , "location of previous definition" . to_string ( ) ) ;
153+ prev. index ( )
154+ }
155+ indexmap:: map:: Entry :: Vacant ( entry) => {
156+ let idx = entry. index ( ) ;
157+ entry. insert ( span) ;
158+ idx
159+ }
160+ } ;
161+ u32:: try_from ( idx) . expect ( "way too many symbols" )
163162 }
164163
165164 fn len ( & self ) -> u32 {
@@ -273,8 +272,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
273272 }
274273 } ;
275274
276- let idx = if let Some ( prev) = entries. map . get ( & value) {
277- prev. idx
275+ let idx = if let Some ( prev) = entries. map . get_index_of ( & value) {
276+ u32 :: try_from ( prev) . expect ( "way too many symbols" )
278277 } else {
279278 prefill_stream. extend ( quote ! {
280279 #value,
@@ -288,7 +287,8 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
288287 } ) ;
289288 }
290289
291- let symbol_digits_base = entries. map [ "0" ] . idx ;
290+ let symbol_digits_base =
291+ u32:: try_from ( entries. map . get_index_of ( "0" ) . unwrap ( ) ) . expect ( "way too many symbols" ) ;
292292 let preinterned_symbols_count = entries. len ( ) ;
293293 let output = quote ! {
294294 const SYMBOL_DIGITS_BASE : u32 = #symbol_digits_base;
0 commit comments