@@ -11,13 +11,13 @@ use rustc_parse::lexer::nfc_normalize;
1111use rustc_parse:: parse_stream_from_source_str;
1212use rustc_session:: parse:: ParseSess ;
1313use rustc_span:: def_id:: CrateNum ;
14- use rustc_span:: symbol:: { self , kw , sym, Symbol } ;
14+ use rustc_span:: symbol:: { self , sym, Symbol } ;
1515use rustc_span:: { BytePos , FileName , Pos , SourceFile , Span } ;
1616
17- use pm:: bridge:: { server, DelimSpan , ExpnGlobals , Group , Punct , TokenTree } ;
17+ use pm:: bridge:: { server, DelimSpan , ExpnGlobals , Group , Ident , Punct , TokenTree } ;
1818use pm:: { Delimiter , Level , LineColumn } ;
19+ use std:: ascii;
1920use std:: ops:: Bound ;
20- use std:: { ascii, panic} ;
2121
2222trait FromInternal < T > {
2323 fn from_internal ( x : T ) -> Self ;
@@ -50,7 +50,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
5050}
5151
5252impl FromInternal < ( TokenStream , & mut Rustc < ' _ , ' _ > ) >
53- for Vec < TokenTree < TokenStream , Span , Ident , Literal > >
53+ for Vec < TokenTree < TokenStream , Span , Symbol , Literal > >
5454{
5555 fn from_internal ( ( stream, rustc) : ( TokenStream , & mut Rustc < ' _ , ' _ > ) ) -> Self {
5656 use rustc_ast:: token:: * ;
@@ -135,13 +135,12 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
135135 Question => op ( "?" ) ,
136136 SingleQuote => op ( "'" ) ,
137137
138- Ident ( name, false ) if name == kw:: DollarCrate => trees. push ( TokenTree :: Ident ( Ident :: dollar_crate ( span) ) ) ,
139- Ident ( name, is_raw) => trees. push ( TokenTree :: Ident ( Ident :: new ( rustc. sess ( ) , name, is_raw, span) ) ) ,
138+ Ident ( sym, is_raw) => trees. push ( TokenTree :: Ident ( Ident { sym, is_raw, span } ) ) ,
140139 Lifetime ( name) => {
141140 let ident = symbol:: Ident :: new ( name, span) . without_first_quote ( ) ;
142141 trees. extend ( [
143142 TokenTree :: Punct ( Punct { ch : b'\'' , joint : true , span } ) ,
144- TokenTree :: Ident ( Ident :: new ( rustc . sess ( ) , ident. name , false , span) ) ,
143+ TokenTree :: Ident ( Ident { sym : ident. name , is_raw : false , span } ) ,
145144 ] ) ;
146145 }
147146 Literal ( lit) => trees. push ( TokenTree :: Literal ( self :: Literal { lit, span } ) ) ,
@@ -170,7 +169,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
170169 }
171170
172171 Interpolated ( nt) if let NtIdent ( ident, is_raw) = * nt => {
173- trees. push ( TokenTree :: Ident ( Ident :: new ( rustc . sess ( ) , ident. name , is_raw, ident. span ) ) )
172+ trees. push ( TokenTree :: Ident ( Ident { sym : ident. name , is_raw, span : ident. span } ) )
174173 }
175174
176175 Interpolated ( nt) => {
@@ -200,11 +199,14 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
200199 }
201200}
202201
203- impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Ident , Literal > {
202+ impl ToInternal < TokenStream >
203+ for ( TokenTree < TokenStream , Span , Symbol , Literal > , & mut Rustc < ' _ , ' _ > )
204+ {
204205 fn to_internal ( self ) -> TokenStream {
205206 use rustc_ast:: token:: * ;
206207
207- let ( ch, joint, span) = match self {
208+ let ( tree, rustc) = self ;
209+ let ( ch, joint, span) = match tree {
208210 TokenTree :: Punct ( Punct { ch, joint, span } ) => ( ch, joint, span) ,
209211 TokenTree :: Group ( Group { delimiter, stream, span : DelimSpan { open, close, .. } } ) => {
210212 return tokenstream:: TokenTree :: Delimited (
@@ -215,6 +217,7 @@ impl ToInternal<TokenStream> for TokenTree<TokenStream, Span, Ident, Literal> {
215217 . into ( ) ;
216218 }
217219 TokenTree :: Ident ( self :: Ident { sym, is_raw, span } ) => {
220+ rustc. sess ( ) . symbol_gallery . insert ( sym, span) ;
218221 return tokenstream:: TokenTree :: token ( Ident ( sym, is_raw) , span) . into ( ) ;
219222 }
220223 TokenTree :: Literal ( self :: Literal {
@@ -289,33 +292,6 @@ impl ToInternal<rustc_errors::Level> for Level {
289292
290293pub struct FreeFunctions ;
291294
292- #[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
293- pub struct Ident {
294- sym : Symbol ,
295- is_raw : bool ,
296- span : Span ,
297- }
298-
299- impl Ident {
300- fn new ( sess : & ParseSess , sym : Symbol , is_raw : bool , span : Span ) -> Ident {
301- let sym = nfc_normalize ( sym. as_str ( ) ) ;
302- let string = sym. as_str ( ) ;
303- if !rustc_lexer:: is_ident ( string) {
304- panic ! ( "`{:?}` is not a valid identifier" , string)
305- }
306- if is_raw && !sym. can_be_raw ( ) {
307- panic ! ( "`{}` cannot be a raw identifier" , string) ;
308- }
309- sess. symbol_gallery . insert ( sym, span) ;
310- Ident { sym, is_raw, span }
311- }
312-
313- fn dollar_crate ( span : Span ) -> Ident {
314- // `$crate` is accepted as an ident only if it comes from the compiler.
315- Ident { sym : kw:: DollarCrate , is_raw : false , span }
316- }
317- }
318-
319295// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
320296#[ derive( Clone , Debug ) ]
321297pub struct Literal {
@@ -357,12 +333,12 @@ impl<'a, 'b> Rustc<'a, 'b> {
357333impl server:: Types for Rustc < ' _ , ' _ > {
358334 type FreeFunctions = FreeFunctions ;
359335 type TokenStream = TokenStream ;
360- type Ident = Ident ;
361336 type Literal = Literal ;
362337 type SourceFile = Lrc < SourceFile > ;
363338 type MultiSpan = Vec < Span > ;
364339 type Diagnostic = Diagnostic ;
365340 type Span = Span ;
341+ type Symbol = Symbol ;
366342}
367343
368344impl server:: FreeFunctions for Rustc < ' _ , ' _ > {
@@ -453,22 +429,22 @@ impl server::TokenStream for Rustc<'_, '_> {
453429
454430 fn from_token_tree (
455431 & mut self ,
456- tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > ,
432+ tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > ,
457433 ) -> Self :: TokenStream {
458- tree. to_internal ( )
434+ ( tree, & mut * self ) . to_internal ( )
459435 }
460436
461437 fn concat_trees (
462438 & mut self ,
463439 base : Option < Self :: TokenStream > ,
464- trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > ,
440+ trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > ,
465441 ) -> Self :: TokenStream {
466442 let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
467443 if let Some ( base) = base {
468444 builder. push ( base) ;
469445 }
470446 for tree in trees {
471- builder. push ( tree. to_internal ( ) ) ;
447+ builder. push ( ( tree, & mut * self ) . to_internal ( ) ) ;
472448 }
473449 builder. build ( )
474450 }
@@ -491,25 +467,11 @@ impl server::TokenStream for Rustc<'_, '_> {
491467 fn into_trees (
492468 & mut self ,
493469 stream : Self :: TokenStream ,
494- ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > {
470+ ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > {
495471 FromInternal :: from_internal ( ( stream, self ) )
496472 }
497473}
498474
499- impl server:: Ident for Rustc < ' _ , ' _ > {
500- fn new ( & mut self , string : & str , span : Self :: Span , is_raw : bool ) -> Self :: Ident {
501- Ident :: new ( self . sess ( ) , Symbol :: intern ( string) , is_raw, span)
502- }
503-
504- fn span ( & mut self , ident : Self :: Ident ) -> Self :: Span {
505- ident. span
506- }
507-
508- fn with_span ( & mut self , ident : Self :: Ident , span : Self :: Span ) -> Self :: Ident {
509- Ident { span, ..ident }
510- }
511- }
512-
513475impl server:: Literal for Rustc < ' _ , ' _ > {
514476 fn from_str ( & mut self , s : & str ) -> Result < Self :: Literal , ( ) > {
515477 let name = FileName :: proc_macro_source_code ( s) ;
@@ -812,6 +774,13 @@ impl server::Span for Rustc<'_, '_> {
812774 }
813775}
814776
777+ impl server:: Symbol for Rustc < ' _ , ' _ > {
778+ fn normalize_and_validate_ident ( & mut self , string : & str ) -> Result < Self :: Symbol , ( ) > {
779+ let sym = nfc_normalize ( string) ;
780+ if rustc_lexer:: is_ident ( sym. as_str ( ) ) { Ok ( sym) } else { Err ( ( ) ) }
781+ }
782+ }
783+
815784impl server:: Server for Rustc < ' _ , ' _ > {
816785 fn globals ( & mut self ) -> ExpnGlobals < Self :: Span > {
817786 ExpnGlobals {
@@ -820,4 +789,12 @@ impl server::Server for Rustc<'_, '_> {
820789 mixed_site : self . mixed_site ,
821790 }
822791 }
792+
793+ fn intern_symbol ( string : & str ) -> Self :: Symbol {
794+ Symbol :: intern ( string)
795+ }
796+
797+ fn with_symbol_string ( symbol : & Self :: Symbol , f : impl FnOnce ( & str ) ) {
798+ f ( & symbol. as_str ( ) )
799+ }
823800}
0 commit comments