File tree 7 files changed +19
-12
lines changed
test/run-pass/rfc-2151-raw-identifiers
7 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ pub use self::AnnNode::*;
13
13
use syntax:: abi:: Abi ;
14
14
use syntax:: ast;
15
15
use syntax:: codemap:: { CodeMap , Spanned } ;
16
- use syntax:: parse:: ParseSess ;
16
+ use syntax:: parse:: { token , ParseSess } ;
17
17
use syntax:: parse:: lexer:: comments;
18
18
use syntax:: print:: pp:: { self , Breaks } ;
19
19
use syntax:: print:: pp:: Breaks :: { Consistent , Inconsistent } ;
@@ -1561,7 +1561,11 @@ impl<'a> State<'a> {
1561
1561
}
1562
1562
1563
1563
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
+ }
1565
1569
self . ann . post ( self , NodeName ( & name) )
1566
1570
}
1567
1571
Original file line number Diff line number Diff line change @@ -142,6 +142,13 @@ pub fn is_path_segment_keyword(id: ast::Ident) -> bool {
142
142
id. name == keywords:: DollarCrate . name ( )
143
143
}
144
144
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
+ ident. name != keywords:: Invalid . name ( ) &&
149
+ is_reserved_ident ( ident) && !is_path_segment_keyword ( ident)
150
+ }
151
+
145
152
// Returns true for reserved identifiers used internally for elided lifetimes,
146
153
// unnamed method parameters, crate root module, error recovery etc.
147
154
pub fn is_special_ident ( id : ast:: Ident ) -> bool {
@@ -236,7 +243,7 @@ impl Token {
236
243
237
244
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
238
245
pub fn from_ast_ident ( ident : ast:: Ident ) -> Token {
239
- Ident ( ident, is_reserved_ident ( ident ) && ! is_path_segment_keyword ( ident) )
246
+ Ident ( ident, is_raw_guess ( ident) )
240
247
}
241
248
242
249
/// 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> {
2373
2373
}
2374
2374
2375
2375
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
+ }
2377
2381
self . ann . post ( self , NodeIdent ( & ident) )
2378
2382
}
2379
2383
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // ignore-pretty
12
-
13
11
#![ feature( raw_identifiers) ]
14
12
15
13
use std:: mem;
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // ignore-pretty
12
-
13
11
#![ feature( raw_identifiers) ]
14
12
15
13
fn r#fn ( r#match : u32 ) -> u32 {
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // ignore-pretty
12
-
13
11
#![ feature( raw_identifiers) ]
14
12
15
13
#[ derive( Debug , PartialEq , Eq ) ]
Original file line number Diff line number Diff line change 8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // ignore-pretty
12
-
13
11
#![ feature( decl_macro) ]
14
12
#![ feature( raw_identifiers) ]
15
13
You can’t perform that action at this time.
0 commit comments