Skip to content

Commit

Permalink
Fix pretty-printing for raw identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 26, 2018
1 parent ab8b961 commit a637dd0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/librustc/hir/print.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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))
}

Expand Down
9 changes: 8 additions & 1 deletion src/libsyntax/parse/token.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 '>'.
Expand Down
6 changes: 5 additions & 1 deletion src/libsyntax/print/pprust.rs
Expand Up @@ -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))
}

Expand Down
2 changes: 0 additions & 2 deletions src/test/run-pass/rfc-2151-raw-identifiers/attr.rs
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/test/run-pass/rfc-2151-raw-identifiers/basic.rs
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/test/run-pass/rfc-2151-raw-identifiers/items.rs
Expand Up @@ -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)]
Expand Down
2 changes: 0 additions & 2 deletions src/test/run-pass/rfc-2151-raw-identifiers/macros.rs
Expand Up @@ -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)]

Expand Down

0 comments on commit a637dd0

Please sign in to comment.