@@ -2522,10 +2522,16 @@ impl fmt::Debug for Ident {
2522
2522
/// except that AST identifiers don't keep the rawness flag, so we have to guess it.
2523
2523
impl fmt:: Display for Ident {
2524
2524
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2525
- fmt:: Display :: fmt ( & IdentPrinter :: new ( self . name , self . is_raw_guess ( ) , None ) , f)
2525
+ fmt:: Display :: fmt ( & IdentPrinter :: new ( self . name , self . guess_print_mode ( ) , None ) , f)
2526
2526
}
2527
2527
}
2528
2528
2529
+ pub enum IdentPrintMode {
2530
+ Normal ,
2531
+ RawIdent ,
2532
+ RawLifetime ,
2533
+ }
2534
+
2529
2535
/// The most general type to print identifiers.
2530
2536
///
2531
2537
/// AST pretty-printer is used as a fallback for turning AST structures into token streams for
@@ -2541,40 +2547,59 @@ impl fmt::Display for Ident {
2541
2547
/// done for a token stream or a single token.
2542
2548
pub struct IdentPrinter {
2543
2549
symbol : Symbol ,
2544
- is_raw : bool ,
2550
+ mode : IdentPrintMode ,
2545
2551
/// Span used for retrieving the crate name to which `$crate` refers to,
2546
2552
/// if this field is `None` then the `$crate` conversion doesn't happen.
2547
2553
convert_dollar_crate : Option < Span > ,
2548
2554
}
2549
2555
2550
2556
impl IdentPrinter {
2551
2557
/// The most general `IdentPrinter` constructor. Do not use this.
2552
- pub fn new ( symbol : Symbol , is_raw : bool , convert_dollar_crate : Option < Span > ) -> IdentPrinter {
2553
- IdentPrinter { symbol, is_raw, convert_dollar_crate }
2558
+ pub fn new (
2559
+ symbol : Symbol ,
2560
+ mode : IdentPrintMode ,
2561
+ convert_dollar_crate : Option < Span > ,
2562
+ ) -> IdentPrinter {
2563
+ IdentPrinter { symbol, mode, convert_dollar_crate }
2554
2564
}
2555
2565
2556
2566
/// This implementation is supposed to be used when printing identifiers
2557
2567
/// as a part of pretty-printing for larger AST pieces.
2558
2568
/// Do not use this either.
2559
- pub fn for_ast_ident ( ident : Ident , is_raw : bool ) -> IdentPrinter {
2560
- IdentPrinter :: new ( ident. name , is_raw , Some ( ident. span ) )
2569
+ pub fn for_ast_ident ( ident : Ident , mode : IdentPrintMode ) -> IdentPrinter {
2570
+ IdentPrinter :: new ( ident. name , mode , Some ( ident. span ) )
2561
2571
}
2562
2572
}
2563
2573
2564
2574
impl fmt:: Display for IdentPrinter {
2565
2575
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2566
- if self . is_raw {
2567
- f. write_str ( "r#" ) ?;
2568
- } else if self . symbol == kw:: DollarCrate {
2569
- if let Some ( span) = self . convert_dollar_crate {
2576
+ let s = match self . mode {
2577
+ IdentPrintMode :: Normal
2578
+ if self . symbol == kw:: DollarCrate
2579
+ && let Some ( span) = self . convert_dollar_crate =>
2580
+ {
2570
2581
let converted = span. ctxt ( ) . dollar_crate_name ( ) ;
2571
2582
if !converted. is_path_segment_keyword ( ) {
2572
2583
f. write_str ( "::" ) ?;
2573
2584
}
2574
- return fmt :: Display :: fmt ( & converted, f ) ;
2585
+ converted
2575
2586
}
2576
- }
2577
- fmt:: Display :: fmt ( & self . symbol , f)
2587
+ IdentPrintMode :: Normal => self . symbol ,
2588
+ IdentPrintMode :: RawIdent => {
2589
+ f. write_str ( "r#" ) ?;
2590
+ self . symbol
2591
+ }
2592
+ IdentPrintMode :: RawLifetime => {
2593
+ f. write_str ( "'r#" ) ?;
2594
+ let s = self
2595
+ . symbol
2596
+ . as_str ( )
2597
+ . strip_prefix ( "'" )
2598
+ . expect ( "only lifetime idents should be passed with RawLifetime mode" ) ;
2599
+ Symbol :: intern ( s)
2600
+ }
2601
+ } ;
2602
+ s. fmt ( f)
2578
2603
}
2579
2604
}
2580
2605
@@ -3009,6 +3034,25 @@ impl Ident {
3009
3034
self . name . can_be_raw ( ) && self . is_reserved ( )
3010
3035
}
3011
3036
3037
+ pub fn is_raw_lifetime_guess ( self ) -> bool {
3038
+ // this should be kept consistent with `Parser::expect_lifetime` found under
3039
+ // compiler/rustc_parse/src/parser/ty.rs
3040
+ let name_without_apostrophe = self . without_first_quote ( ) ;
3041
+ name_without_apostrophe. name != self . name
3042
+ && ![ kw:: UnderscoreLifetime , kw:: StaticLifetime ] . contains ( & self . name )
3043
+ && name_without_apostrophe. is_raw_guess ( )
3044
+ }
3045
+
3046
+ pub fn guess_print_mode ( self ) -> IdentPrintMode {
3047
+ if self . is_raw_lifetime_guess ( ) {
3048
+ IdentPrintMode :: RawLifetime
3049
+ } else if self . is_raw_guess ( ) {
3050
+ IdentPrintMode :: RawIdent
3051
+ } else {
3052
+ IdentPrintMode :: Normal
3053
+ }
3054
+ }
3055
+
3012
3056
/// Whether this would be the identifier for a tuple field like `self.0`, as
3013
3057
/// opposed to a named field like `self.thing`.
3014
3058
pub fn is_numeric ( self ) -> bool {
0 commit comments