diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 3d38c0c8ed9e5..ff501f30c891a 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -13,7 +13,7 @@ pub use self::AnnNode::*; use syntax::abi::Abi; use syntax::ast; use syntax::codemap::{CodeMap, Spanned}; -use syntax::parse::ParseSess; +use syntax::parse::{token, ParseSess}; use syntax::parse::lexer::comments; use syntax::print::pp::{self, Breaks}; use syntax::print::pp::Breaks::{Consistent, Inconsistent}; @@ -1561,7 +1561,11 @@ impl<'a> State<'a> { } pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> { - self.s.word(&name.as_str())?; + if token::is_raw_guess(ast::Ident::with_empty_ctxt(name)) { + self.s.word(&format!("r#{}", name))?; + } else { + self.s.word(&name.as_str())?; + } self.ann.post(self, NodeName(&name)) } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 7798a7a77ee6c..868f95fa7e468 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -142,6 +142,12 @@ pub fn is_path_segment_keyword(id: ast::Ident) -> bool { id.name == keywords::DollarCrate.name() } +// We see this identifier in a normal identifier position, like variable name or a type. +// How was it written originally? Did it use the raw form? Let's try to guess. +pub fn is_raw_guess(ident: ast::Ident) -> bool { + is_reserved_ident(ident) && !is_path_segment_keyword(ident) +} + // Returns true for reserved identifiers used internally for elided lifetimes, // unnamed method parameters, crate root module, error recovery etc. pub fn is_special_ident(id: ast::Ident) -> bool { @@ -236,7 +242,7 @@ impl Token { /// Recovers a `Token` from an `ast::Ident`. This creates a raw identifier if necessary. pub fn from_ast_ident(ident: ast::Ident) -> Token { - Ident(ident, is_reserved_ident(ident) && !is_path_segment_keyword(ident)) + Ident(ident, is_raw_guess(ident)) } /// Returns `true` if the token starts with '>'. diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 50577a26abf41..ae045fc095a50 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2373,7 +2373,11 @@ impl<'a> State<'a> { } pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> { - self.s.word(&ident.name.as_str())?; + if token::is_raw_guess(ident) { + self.s.word(&format!("r#{}", ident))?; + } else { + self.s.word(&ident.name.as_str())?; + } self.ann.post(self, NodeIdent(&ident)) } diff --git a/src/test/run-pass/rfc-2151-raw-identifiers/attr.rs b/src/test/run-pass/rfc-2151-raw-identifiers/attr.rs index 3566babaf4cbd..6cea75cf1d11e 100644 --- a/src/test/run-pass/rfc-2151-raw-identifiers/attr.rs +++ b/src/test/run-pass/rfc-2151-raw-identifiers/attr.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-pretty - #![feature(raw_identifiers)] use std::mem; diff --git a/src/test/run-pass/rfc-2151-raw-identifiers/basic.rs b/src/test/run-pass/rfc-2151-raw-identifiers/basic.rs index bd1f52a9b24e0..5d495c4e9e557 100644 --- a/src/test/run-pass/rfc-2151-raw-identifiers/basic.rs +++ b/src/test/run-pass/rfc-2151-raw-identifiers/basic.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-pretty - #![feature(raw_identifiers)] fn r#fn(r#match: u32) -> u32 { diff --git a/src/test/run-pass/rfc-2151-raw-identifiers/items.rs b/src/test/run-pass/rfc-2151-raw-identifiers/items.rs index 5fdc13df8dcb2..256bd263d38d4 100644 --- a/src/test/run-pass/rfc-2151-raw-identifiers/items.rs +++ b/src/test/run-pass/rfc-2151-raw-identifiers/items.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-pretty - #![feature(raw_identifiers)] #[derive(Debug, PartialEq, Eq)] diff --git a/src/test/run-pass/rfc-2151-raw-identifiers/macros.rs b/src/test/run-pass/rfc-2151-raw-identifiers/macros.rs index 82d44c57e181b..4bd16ded52fbd 100644 --- a/src/test/run-pass/rfc-2151-raw-identifiers/macros.rs +++ b/src/test/run-pass/rfc-2151-raw-identifiers/macros.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// ignore-pretty - #![feature(decl_macro)] #![feature(raw_identifiers)]