@@ -7,17 +7,16 @@ use rustc_ast_pretty::pprust;
7
7
use rustc_data_structures:: fx:: FxHashMap ;
8
8
use rustc_data_structures:: sync:: Lrc ;
9
9
use rustc_errors:: { Diagnostic , MultiSpan , PResult } ;
10
- use rustc_parse:: lexer:: nfc_normalize;
11
10
use rustc_parse:: parse_stream_from_source_str;
12
11
use rustc_session:: parse:: ParseSess ;
13
12
use rustc_span:: def_id:: CrateNum ;
14
- use rustc_span:: symbol:: { self , kw , sym, Symbol } ;
13
+ use rustc_span:: symbol:: { self , sym, Symbol } ;
15
14
use rustc_span:: { BytePos , FileName , Pos , SourceFile , Span } ;
16
15
17
- use pm:: bridge:: { server, DelimSpan , Group , Punct , TokenTree } ;
16
+ use pm:: bridge:: { server, DelimSpan , Group , Ident , Punct , TokenTree } ;
18
17
use pm:: { Delimiter , Level , LineColumn } ;
18
+ use std:: ascii;
19
19
use std:: ops:: Bound ;
20
- use std:: { ascii, panic} ;
21
20
22
21
trait FromInternal < T > {
23
22
fn from_internal ( x : T ) -> Self ;
@@ -50,7 +49,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
50
49
}
51
50
52
51
impl FromInternal < ( TokenStream , & mut Rustc < ' _ , ' _ > ) >
53
- for Vec < TokenTree < TokenStream , Span , Ident , Literal > >
52
+ for Vec < TokenTree < TokenStream , Span , Symbol , Literal > >
54
53
{
55
54
fn from_internal ( ( stream, rustc) : ( TokenStream , & mut Rustc < ' _ , ' _ > ) ) -> Self {
56
55
use rustc_ast:: token:: * ;
@@ -84,9 +83,6 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
84
83
span,
85
84
} ) )
86
85
) ;
87
- ( $ty: ident:: $method: ident( $( $value: expr) ,* ) ) => (
88
- trees. push( TokenTree :: $ty( self :: $ty:: $method( $( $value, ) * span) ) )
89
- ) ;
90
86
}
91
87
macro_rules! op {
92
88
( $a: expr) => { {
@@ -152,12 +148,11 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
152
148
Question => op ! ( '?' ) ,
153
149
SingleQuote => op ! ( '\'' ) ,
154
150
155
- Ident ( name, false ) if name == kw:: DollarCrate => tt ! ( Ident :: dollar_crate( ) ) ,
156
- Ident ( name, is_raw) => tt ! ( Ident :: new( rustc. sess( ) , name, is_raw) ) ,
151
+ Ident ( sym, is_raw) => tt ! ( Ident { sym, is_raw } ) ,
157
152
Lifetime ( name) => {
158
153
let ident = symbol:: Ident :: new ( name, span) . without_first_quote ( ) ;
159
154
tt ! ( Punct { ch: '\'' , joint: true } ) ;
160
- tt ! ( Ident :: new ( rustc . sess ( ) , ident. name, false ) ) ;
155
+ tt ! ( Ident { sym : ident. name, is_raw : false } ) ;
161
156
}
162
157
Literal ( lit) => tt ! ( Literal { lit } ) ,
163
158
DocComment ( _, attr_style, data) => {
@@ -185,7 +180,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
185
180
}
186
181
187
182
Interpolated ( nt) if let NtIdent ( ident, is_raw) = * nt => {
188
- trees. push ( TokenTree :: Ident ( Ident :: new ( rustc . sess ( ) , ident. name , is_raw, ident. span ) ) )
183
+ trees. push ( TokenTree :: Ident ( Ident { sym : ident. name , is_raw, span : ident. span } ) )
189
184
}
190
185
191
186
Interpolated ( nt) => {
@@ -209,7 +204,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)>
209
204
}
210
205
}
211
206
212
- impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Ident , Literal > {
207
+ impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Symbol , Literal > {
213
208
fn to_internal ( self ) -> TokenStream {
214
209
use rustc_ast:: token:: * ;
215
210
@@ -298,32 +293,6 @@ impl ToInternal<rustc_errors::Level> for Level {
298
293
299
294
pub struct FreeFunctions ;
300
295
301
- #[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
302
- pub struct Ident {
303
- sym : Symbol ,
304
- is_raw : bool ,
305
- span : Span ,
306
- }
307
-
308
- impl Ident {
309
- fn new ( sess : & ParseSess , sym : Symbol , is_raw : bool , span : Span ) -> Ident {
310
- let sym = nfc_normalize ( sym. as_str ( ) ) ;
311
- let string = sym. as_str ( ) ;
312
- if !rustc_lexer:: is_ident ( string) {
313
- panic ! ( "`{:?}` is not a valid identifier" , string)
314
- }
315
- if is_raw && !sym. can_be_raw ( ) {
316
- panic ! ( "`{}` cannot be a raw identifier" , string) ;
317
- }
318
- sess. symbol_gallery . insert ( sym, span) ;
319
- Ident { sym, is_raw, span }
320
- }
321
- fn dollar_crate ( span : Span ) -> Ident {
322
- // `$crate` is accepted as an ident only if it comes from the compiler.
323
- Ident { sym : kw:: DollarCrate , is_raw : false , span }
324
- }
325
- }
326
-
327
296
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
328
297
#[ derive( Clone , Debug ) ]
329
298
pub struct Literal {
@@ -368,12 +337,12 @@ impl<'a, 'b> Rustc<'a, 'b> {
368
337
impl server:: Types for Rustc < ' _ , ' _ > {
369
338
type FreeFunctions = FreeFunctions ;
370
339
type TokenStream = TokenStream ;
371
- type Ident = Ident ;
372
340
type Literal = Literal ;
373
341
type SourceFile = Lrc < SourceFile > ;
374
342
type MultiSpan = Vec < Span > ;
375
343
type Diagnostic = Diagnostic ;
376
344
type Span = Span ;
345
+ type Symbol = Symbol ;
377
346
}
378
347
379
348
impl server:: FreeFunctions for Rustc < ' _ , ' _ > {
@@ -456,14 +425,14 @@ impl server::TokenStream for Rustc<'_, '_> {
456
425
}
457
426
fn from_token_tree (
458
427
& mut self ,
459
- tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > ,
428
+ tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > ,
460
429
) -> Self :: TokenStream {
461
430
tree. to_internal ( )
462
431
}
463
432
fn concat_trees (
464
433
& mut self ,
465
434
base : Option < Self :: TokenStream > ,
466
- trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > ,
435
+ trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > ,
467
436
) -> Self :: TokenStream {
468
437
let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
469
438
if let Some ( base) = base {
@@ -491,23 +460,11 @@ impl server::TokenStream for Rustc<'_, '_> {
491
460
fn into_iter (
492
461
& mut self ,
493
462
stream : Self :: TokenStream ,
494
- ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > {
463
+ ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > {
495
464
FromInternal :: from_internal ( ( stream, self ) )
496
465
}
497
466
}
498
467
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
- fn span ( & mut self , ident : Self :: Ident ) -> Self :: Span {
504
- ident. span
505
- }
506
- fn with_span ( & mut self , ident : Self :: Ident , span : Self :: Span ) -> Self :: Ident {
507
- Ident { span, ..ident }
508
- }
509
- }
510
-
511
468
impl server:: Literal for Rustc < ' _ , ' _ > {
512
469
fn from_str ( & mut self , s : & str ) -> Result < Self :: Literal , ( ) > {
513
470
let name = FileName :: proc_macro_source_code ( s) ;
@@ -789,4 +746,26 @@ impl server::Context for Rustc<'_, '_> {
789
746
fn mixed_site ( & mut self ) -> Self :: Span {
790
747
self . mixed_site
791
748
}
749
+
750
+ // NOTE: May be run on any thread, so cannot use `nfc_normalize`
751
+ fn validate_ident ( s : & str ) -> Result < Option < String > , ( ) > {
752
+ use unicode_normalization:: { is_nfc_quick, IsNormalized , UnicodeNormalization } ;
753
+ let normalized: Option < String > = match is_nfc_quick ( s. chars ( ) ) {
754
+ IsNormalized :: Yes => None ,
755
+ _ => Some ( s. chars ( ) . nfc ( ) . collect ( ) ) ,
756
+ } ;
757
+ if rustc_lexer:: is_ident ( normalized. as_ref ( ) . map ( |s| & s[ ..] ) . unwrap_or ( s) ) {
758
+ Ok ( normalized)
759
+ } else {
760
+ Err ( ( ) )
761
+ }
762
+ }
763
+
764
+ fn intern_symbol ( string : & str ) -> Self :: Symbol {
765
+ Symbol :: intern ( string)
766
+ }
767
+
768
+ fn with_symbol_string ( symbol : & Self :: Symbol , f : impl FnOnce ( & str ) ) {
769
+ f ( & symbol. as_str ( ) )
770
+ }
792
771
}
0 commit comments