File tree Expand file tree Collapse file tree 7 files changed +18
-12
lines changed
test/run-pass/rfc-2151-raw-identifiers Expand file tree Collapse file tree 7 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ pub use self::AnnNode::*;
1313use syntax:: abi:: Abi ;
1414use syntax:: ast;
1515use syntax:: codemap:: { CodeMap , Spanned } ;
16- use syntax:: parse:: ParseSess ;
16+ use syntax:: parse:: { token , ParseSess } ;
1717use syntax:: parse:: lexer:: comments;
1818use syntax:: print:: pp:: { self , Breaks } ;
1919use syntax:: print:: pp:: Breaks :: { Consistent , Inconsistent } ;
@@ -1561,7 +1561,11 @@ impl<'a> State<'a> {
15611561 }
15621562
15631563 pub fn print_name ( & mut self , name : ast:: Name ) -> io:: Result < ( ) > {
1564- self . s . word ( & name. as_str ( ) ) ?;
1564+ if token:: is_raw_guess ( ast:: Ident :: with_empty_ctxt ( name) ) {
1565+ self . s . word ( & format ! ( "r#{}" , name) ) ?;
1566+ } else {
1567+ self . s . word ( & name. as_str ( ) ) ?;
1568+ }
15651569 self . ann . post ( self , NodeName ( & name) )
15661570 }
15671571
Original file line number Diff line number Diff line change @@ -142,6 +142,12 @@ pub fn is_path_segment_keyword(id: ast::Ident) -> bool {
142142 id. name == keywords:: DollarCrate . name ( )
143143}
144144
145+ // We see this identifier in a normal identifier position, like variable name or a type.
146+ // How was it written originally? Did it use the raw form? Let's try to guess.
147+ pub fn is_raw_guess ( ident : ast:: Ident ) -> bool {
148+ is_reserved_ident ( ident) && !is_path_segment_keyword ( ident)
149+ }
150+
145151// Returns true for reserved identifiers used internally for elided lifetimes,
146152// unnamed method parameters, crate root module, error recovery etc.
147153pub fn is_special_ident ( id : ast:: Ident ) -> bool {
@@ -236,7 +242,7 @@ impl Token {
236242
237243 /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
238244 pub fn from_ast_ident ( ident : ast:: Ident ) -> Token {
239- Ident ( ident, is_reserved_ident ( ident ) && ! is_path_segment_keyword ( ident) )
245+ Ident ( ident, is_raw_guess ( ident) )
240246 }
241247
242248 /// Returns `true` if the token starts with '>'.
Original file line number Diff line number Diff line change @@ -2373,7 +2373,11 @@ impl<'a> State<'a> {
23732373 }
23742374
23752375 pub fn print_ident ( & mut self , ident : ast:: Ident ) -> io:: Result < ( ) > {
2376- self . s . word ( & ident. name . as_str ( ) ) ?;
2376+ if token:: is_raw_guess ( ident) {
2377+ self . s . word ( & format ! ( "r#{}" , ident) ) ?;
2378+ } else {
2379+ self . s . word ( & ident. name . as_str ( ) ) ?;
2380+ }
23772381 self . ann . post ( self , NodeIdent ( & ident) )
23782382 }
23792383
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- // ignore-pretty
12-
1311#![ feature( raw_identifiers) ]
1412
1513use std:: mem;
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- // ignore-pretty
12-
1311#![ feature( raw_identifiers) ]
1412
1513fn r#fn ( r#match : u32 ) -> u32 {
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- // ignore-pretty
12-
1311#![ feature( raw_identifiers) ]
1412
1513#[ derive( Debug , PartialEq , Eq ) ]
Original file line number Diff line number Diff line change 88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- // ignore-pretty
12-
1311#![ feature( decl_macro) ]
1412#![ feature( raw_identifiers) ]
1513
You can’t perform that action at this time.
0 commit comments