@@ -11,13 +11,13 @@ use rustc_parse::lexer::nfc_normalize;
11
11
use rustc_parse:: parse_stream_from_source_str;
12
12
use rustc_session:: parse:: ParseSess ;
13
13
use rustc_span:: def_id:: CrateNum ;
14
- use rustc_span:: symbol:: { self , kw , sym, Symbol } ;
14
+ use rustc_span:: symbol:: { self , sym, Symbol } ;
15
15
use rustc_span:: { BytePos , FileName , Pos , SourceFile , Span } ;
16
16
17
- use pm:: bridge:: { server, DelimSpan , ExpnGlobals , Group , Punct , TokenTree } ;
17
+ use pm:: bridge:: { server, DelimSpan , ExpnGlobals , Group , Ident , Punct , TokenTree } ;
18
18
use pm:: { Delimiter , Level , LineColumn } ;
19
+ use std:: ascii;
19
20
use std:: ops:: Bound ;
20
- use std:: { ascii, panic} ;
21
21
22
22
trait FromInternal < T > {
23
23
fn from_internal ( x : T ) -> Self ;
@@ -50,7 +50,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
50
50
}
51
51
52
52
impl FromInternal < ( TokenStream , & mut Rustc < ' _ , ' _ > ) >
53
- for Vec < TokenTree < TokenStream , Span , Ident , Literal > >
53
+ for Vec < TokenTree < TokenStream , Span , Symbol , Literal > >
54
54
{
55
55
fn from_internal ( ( stream, rustc) : ( TokenStream , & mut Rustc < ' _ , ' _ > ) ) -> Self {
56
56
use rustc_ast:: token:: * ;
@@ -135,13 +135,12 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
135
135
Question => op ( "?" ) ,
136
136
SingleQuote => op ( "'" ) ,
137
137
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 } ) ) ,
140
139
Lifetime ( name) => {
141
140
let ident = symbol:: Ident :: new ( name, span) . without_first_quote ( ) ;
142
141
trees. extend ( [
143
142
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 } ) ,
145
144
] ) ;
146
145
}
147
146
Literal ( lit) => trees. push ( TokenTree :: Literal ( self :: Literal { lit, span } ) ) ,
@@ -170,7 +169,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
170
169
}
171
170
172
171
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 } ) )
174
173
}
175
174
176
175
Interpolated ( nt) => {
@@ -200,11 +199,14 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
200
199
}
201
200
}
202
201
203
- impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Ident , Literal > {
202
+ impl ToInternal < TokenStream >
203
+ for ( TokenTree < TokenStream , Span , Symbol , Literal > , & mut Rustc < ' _ , ' _ > )
204
+ {
204
205
fn to_internal ( self ) -> TokenStream {
205
206
use rustc_ast:: token:: * ;
206
207
207
- let ( ch, joint, span) = match self {
208
+ let ( tree, rustc) = self ;
209
+ let ( ch, joint, span) = match tree {
208
210
TokenTree :: Punct ( Punct { ch, joint, span } ) => ( ch, joint, span) ,
209
211
TokenTree :: Group ( Group { delimiter, stream, span : DelimSpan { open, close, .. } } ) => {
210
212
return tokenstream:: TokenTree :: Delimited (
@@ -215,6 +217,7 @@ impl ToInternal<TokenStream> for TokenTree<TokenStream, Span, Ident, Literal> {
215
217
. into ( ) ;
216
218
}
217
219
TokenTree :: Ident ( self :: Ident { sym, is_raw, span } ) => {
220
+ rustc. sess ( ) . symbol_gallery . insert ( sym, span) ;
218
221
return tokenstream:: TokenTree :: token ( Ident ( sym, is_raw) , span) . into ( ) ;
219
222
}
220
223
TokenTree :: Literal ( self :: Literal {
@@ -289,33 +292,6 @@ impl ToInternal<rustc_errors::Level> for Level {
289
292
290
293
pub struct FreeFunctions ;
291
294
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
-
319
295
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
320
296
#[ derive( Clone , Debug ) ]
321
297
pub struct Literal {
@@ -357,12 +333,12 @@ impl<'a, 'b> Rustc<'a, 'b> {
357
333
impl server:: Types for Rustc < ' _ , ' _ > {
358
334
type FreeFunctions = FreeFunctions ;
359
335
type TokenStream = TokenStream ;
360
- type Ident = Ident ;
361
336
type Literal = Literal ;
362
337
type SourceFile = Lrc < SourceFile > ;
363
338
type MultiSpan = Vec < Span > ;
364
339
type Diagnostic = Diagnostic ;
365
340
type Span = Span ;
341
+ type Symbol = Symbol ;
366
342
}
367
343
368
344
impl server:: FreeFunctions for Rustc < ' _ , ' _ > {
@@ -453,22 +429,22 @@ impl server::TokenStream for Rustc<'_, '_> {
453
429
454
430
fn from_token_tree (
455
431
& mut self ,
456
- tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > ,
432
+ tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > ,
457
433
) -> Self :: TokenStream {
458
- tree. to_internal ( )
434
+ ( tree, & mut * self ) . to_internal ( )
459
435
}
460
436
461
437
fn concat_trees (
462
438
& mut self ,
463
439
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 > > ,
465
441
) -> Self :: TokenStream {
466
442
let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
467
443
if let Some ( base) = base {
468
444
builder. push ( base) ;
469
445
}
470
446
for tree in trees {
471
- builder. push ( tree. to_internal ( ) ) ;
447
+ builder. push ( ( tree, & mut * self ) . to_internal ( ) ) ;
472
448
}
473
449
builder. build ( )
474
450
}
@@ -491,25 +467,11 @@ impl server::TokenStream for Rustc<'_, '_> {
491
467
fn into_trees (
492
468
& mut self ,
493
469
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 > > {
495
471
FromInternal :: from_internal ( ( stream, self ) )
496
472
}
497
473
}
498
474
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
-
513
475
impl server:: Literal for Rustc < ' _ , ' _ > {
514
476
fn from_str ( & mut self , s : & str ) -> Result < Self :: Literal , ( ) > {
515
477
let name = FileName :: proc_macro_source_code ( s) ;
@@ -812,6 +774,13 @@ impl server::Span for Rustc<'_, '_> {
812
774
}
813
775
}
814
776
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
+
815
784
impl server:: Server for Rustc < ' _ , ' _ > {
816
785
fn globals ( & mut self ) -> ExpnGlobals < Self :: Span > {
817
786
ExpnGlobals {
@@ -820,4 +789,12 @@ impl server::Server for Rustc<'_, '_> {
820
789
mixed_site : self . mixed_site ,
821
790
}
822
791
}
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
+ }
823
800
}
0 commit comments