@@ -10,7 +10,6 @@ use rustc_data_structures::sync::Lrc;
10
10
use rustc_errors:: Diagnostic ;
11
11
use rustc_lint_defs:: builtin:: PROC_MACRO_BACK_COMPAT ;
12
12
use rustc_lint_defs:: BuiltinLintDiagnostics ;
13
- use rustc_parse:: lexer:: nfc_normalize;
14
13
use rustc_parse:: { nt_to_tokenstream, parse_stream_from_source_str} ;
15
14
use rustc_session:: parse:: ParseSess ;
16
15
use rustc_span:: def_id:: CrateNum ;
@@ -19,10 +18,10 @@ use rustc_span::hygiene::ExpnKind;
19
18
use rustc_span:: symbol:: { self , kw, sym, Symbol } ;
20
19
use rustc_span:: { BytePos , FileName , MultiSpan , Pos , RealFileName , SourceFile , Span } ;
21
20
22
- use pm:: bridge:: { server, DelimSpan , Group , Punct , TokenTree } ;
21
+ use pm:: bridge:: { server, DelimSpan , Group , Ident , Punct , TokenTree } ;
23
22
use pm:: { Delimiter , Level , LineColumn } ;
23
+ use std:: ascii;
24
24
use std:: ops:: Bound ;
25
- use std:: { ascii, panic} ;
26
25
27
26
trait FromInternal < T > {
28
27
fn from_internal ( x : T ) -> Self ;
@@ -55,7 +54,7 @@ impl ToInternal<token::DelimToken> for Delimiter {
55
54
}
56
55
57
56
impl FromInternal < ( TokenStream , & mut Rustc < ' _ > ) >
58
- for Vec < TokenTree < TokenStream , Span , Ident , Literal > >
57
+ for Vec < TokenTree < TokenStream , Span , Symbol , Literal > >
59
58
{
60
59
fn from_internal ( ( stream, rustc) : ( TokenStream , & mut Rustc < ' _ > ) ) -> Self {
61
60
use rustc_ast:: token:: * ;
@@ -157,12 +156,11 @@ impl FromInternal<(TokenStream, &mut Rustc<'_>)>
157
156
Question => op ! ( '?' ) ,
158
157
SingleQuote => op ! ( '\'' ) ,
159
158
160
- Ident ( name, false ) if name == kw:: DollarCrate => tt ! ( Ident :: dollar_crate( ) ) ,
161
- Ident ( name, is_raw) => tt ! ( Ident :: new( rustc. sess, name, is_raw) ) ,
159
+ Ident ( sym, is_raw) => tt ! ( Ident { sym, is_raw } ) ,
162
160
Lifetime ( name) => {
163
161
let ident = symbol:: Ident :: new ( name, span) . without_first_quote ( ) ;
164
162
tt ! ( Punct { ch: '\'' , joint: true } ) ;
165
- tt ! ( Ident :: new ( rustc . sess , ident. name, false ) ) ;
163
+ tt ! ( Ident { sym : ident. name, is_raw : false } ) ;
166
164
}
167
165
Literal ( lit) => tt ! ( Literal { lit } ) ,
168
166
DocComment ( _, attr_style, data) => {
@@ -191,9 +189,11 @@ impl FromInternal<(TokenStream, &mut Rustc<'_>)>
191
189
192
190
Interpolated ( nt) => {
193
191
if let Some ( ( name, is_raw) ) = ident_name_compatibility_hack ( & nt, span, rustc) {
194
- trees. push ( TokenTree :: Ident ( Ident :: new (
195
- rustc. sess , name. name , is_raw, name. span ,
196
- ) ) ) ;
192
+ trees. push ( TokenTree :: Ident ( Ident {
193
+ sym : name. name ,
194
+ is_raw,
195
+ span : name. span ,
196
+ } ) ) ;
197
197
} else {
198
198
let stream =
199
199
nt_to_tokenstream ( & nt, rustc. sess , CanSynthesizeMissingTokens :: No ) ;
@@ -217,7 +217,7 @@ impl FromInternal<(TokenStream, &mut Rustc<'_>)>
217
217
}
218
218
}
219
219
220
- impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Ident , Literal > {
220
+ impl ToInternal < TokenStream > for TokenTree < TokenStream , Span , Symbol , Literal > {
221
221
fn to_internal ( self ) -> TokenStream {
222
222
use rustc_ast:: token:: * ;
223
223
@@ -306,32 +306,6 @@ impl ToInternal<rustc_errors::Level> for Level {
306
306
307
307
pub struct FreeFunctions ;
308
308
309
- #[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
310
- pub struct Ident {
311
- sym : Symbol ,
312
- is_raw : bool ,
313
- span : Span ,
314
- }
315
-
316
- impl Ident {
317
- fn new ( sess : & ParseSess , sym : Symbol , is_raw : bool , span : Span ) -> Ident {
318
- let sym = nfc_normalize ( & sym. as_str ( ) ) ;
319
- let string = sym. as_str ( ) ;
320
- if !rustc_lexer:: is_ident ( & string) {
321
- panic ! ( "`{:?}` is not a valid identifier" , string)
322
- }
323
- if is_raw && !sym. can_be_raw ( ) {
324
- panic ! ( "`{}` cannot be a raw identifier" , string) ;
325
- }
326
- sess. symbol_gallery . insert ( sym, span) ;
327
- Ident { sym, is_raw, span }
328
- }
329
- fn dollar_crate ( span : Span ) -> Ident {
330
- // `$crate` is accepted as an ident only if it comes from the compiler.
331
- Ident { sym : kw:: DollarCrate , is_raw : false , span }
332
- }
333
- }
334
-
335
309
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
336
310
#[ derive( Clone , Debug ) ]
337
311
pub struct Literal {
@@ -382,12 +356,12 @@ impl<'a> Rustc<'a> {
382
356
impl server:: Types for Rustc < ' _ > {
383
357
type FreeFunctions = FreeFunctions ;
384
358
type TokenStream = TokenStream ;
385
- type Ident = Ident ;
386
359
type Literal = Literal ;
387
360
type SourceFile = Lrc < SourceFile > ;
388
361
type MultiSpan = Vec < Span > ;
389
362
type Diagnostic = Diagnostic ;
390
363
type Span = Span ;
364
+ type Symbol = Symbol ;
391
365
}
392
366
393
367
impl server:: FreeFunctions for Rustc < ' _ > {
@@ -413,14 +387,14 @@ impl server::TokenStream for Rustc<'_> {
413
387
}
414
388
fn from_token_tree (
415
389
& mut self ,
416
- tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > ,
390
+ tree : TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > ,
417
391
) -> Self :: TokenStream {
418
392
tree. to_internal ( )
419
393
}
420
394
fn concat_trees (
421
395
& mut self ,
422
396
base : Option < Self :: TokenStream > ,
423
- trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > ,
397
+ trees : Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > ,
424
398
) -> Self :: TokenStream {
425
399
let mut builder = tokenstream:: TokenStreamBuilder :: new ( ) ;
426
400
if let Some ( base) = base {
@@ -448,23 +422,11 @@ impl server::TokenStream for Rustc<'_> {
448
422
fn into_iter (
449
423
& mut self ,
450
424
stream : Self :: TokenStream ,
451
- ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Ident , Self :: Literal > > {
425
+ ) -> Vec < TokenTree < Self :: TokenStream , Self :: Span , Self :: Symbol , Self :: Literal > > {
452
426
FromInternal :: from_internal ( ( stream, self ) )
453
427
}
454
428
}
455
429
456
- impl server:: Ident for Rustc < ' _ > {
457
- fn new ( & mut self , string : & str , span : Self :: Span , is_raw : bool ) -> Self :: Ident {
458
- Ident :: new ( self . sess , Symbol :: intern ( string) , is_raw, span)
459
- }
460
- fn span ( & mut self , ident : Self :: Ident ) -> Self :: Span {
461
- ident. span
462
- }
463
- fn with_span ( & mut self , ident : Self :: Ident , span : Self :: Span ) -> Self :: Ident {
464
- Ident { span, ..ident }
465
- }
466
- }
467
-
468
430
impl server:: Literal for Rustc < ' _ > {
469
431
fn from_str ( & mut self , s : & str ) -> Result < Self :: Literal , ( ) > {
470
432
let override_span = None ;
@@ -729,6 +691,28 @@ impl server::Context for Rustc<'_> {
729
691
fn mixed_site ( & mut self ) -> Self :: Span {
730
692
self . mixed_site
731
693
}
694
+
695
+ // NOTE: May be run on any thread, so cannot use `nfc_normalize`
696
+ fn validate_ident ( s : & str ) -> Result < Option < String > , ( ) > {
697
+ use unicode_normalization:: { is_nfc_quick, IsNormalized , UnicodeNormalization } ;
698
+ let normalized: Option < String > = match is_nfc_quick ( s. chars ( ) ) {
699
+ IsNormalized :: Yes => None ,
700
+ _ => Some ( s. chars ( ) . nfc ( ) . collect ( ) ) ,
701
+ } ;
702
+ if rustc_lexer:: is_ident ( normalized. as_ref ( ) . map ( |s| & s[ ..] ) . unwrap_or ( s) ) {
703
+ Ok ( normalized)
704
+ } else {
705
+ Err ( ( ) )
706
+ }
707
+ }
708
+
709
+ fn intern_symbol ( string : & str ) -> Self :: Symbol {
710
+ Symbol :: intern ( string)
711
+ }
712
+
713
+ fn with_symbol_string ( symbol : & Self :: Symbol , f : impl FnOnce ( & str ) ) {
714
+ f ( & symbol. as_str ( ) )
715
+ }
732
716
}
733
717
734
718
// See issue #74616 for details
0 commit comments