about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-03-27 10:47:49 +0200
committerGitHub <noreply@github.com>2018-03-27 10:47:49 +0200
commitdbd6c56f325daf9b2ae141f507ead35d178b2380 (patch)
tree6d5aab317a1b53d70371784aada837f5056b8e26 /src/libsyntax
parent68a2e73d8cf8bd15c5627148785899f46d851c73 (diff)
parenta637dd00c8536d86cfbe59d8a3881e29b3e55eeb (diff)
downloadrust-dbd6c56f325daf9b2ae141f507ead35d178b2380.tar.gz
rust-dbd6c56f325daf9b2ae141f507ead35d178b2380.zip
Rollup merge of #49369 - petrochenkov:rprint, r=oli-obk
Fix pretty-printing for raw identifiers
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/parse/token.rs9
-rw-r--r--src/libsyntax/print/pprust.rs6
2 files changed, 13 insertions, 2 deletions
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 7798a7a77ee..e2dfca5d10a 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -142,6 +142,13 @@ 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 {
+    ident.name != keywords::Invalid.name() &&
+    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 +243,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 50577a26abf..ae045fc095a 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))
     }