Skip to content

Commit a637dd0

Browse files
committed
Fix pretty-printing for raw identifiers
1 parent ab8b961 commit a637dd0

File tree

7 files changed

+19
-12
lines changed

7 files changed

+19
-12
lines changed

src/librustc/hir/print.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::AnnNode::*;
1313
use syntax::abi::Abi;
1414
use syntax::ast;
1515
use syntax::codemap::{CodeMap, Spanned};
16-
use syntax::parse::ParseSess;
16+
use syntax::parse::{token, ParseSess};
1717
use syntax::parse::lexer::comments;
1818
use syntax::print::pp::{self, Breaks};
1919
use 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

src/libsyntax/parse/token.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ 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+
ident.name != keywords::Invalid.name() &&
149+
is_reserved_ident(ident) && !is_path_segment_keyword(ident)
150+
}
151+
145152
// Returns true for reserved identifiers used internally for elided lifetimes,
146153
// unnamed method parameters, crate root module, error recovery etc.
147154
pub fn is_special_ident(id: ast::Ident) -> bool {
@@ -236,7 +243,7 @@ impl Token {
236243

237244
/// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary.
238245
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))
240247
}
241248

242249
/// Returns `true` if the token starts with '>'.

src/libsyntax/print/pprust.rs

+5-1
Original file line numberDiff line numberDiff 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

src/test/run-pass/rfc-2151-raw-identifiers/attr.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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
use std::mem;

src/test/run-pass/rfc-2151-raw-identifiers/basic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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
fn r#fn(r#match: u32) -> u32 {

src/test/run-pass/rfc-2151-raw-identifiers/items.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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)]

src/test/run-pass/rfc-2151-raw-identifiers/macros.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
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

0 commit comments

Comments
 (0)