Skip to content

Commit f116ab6

Browse files
committed
proc_macro: Properly support raw identifiers
1 parent 47d4089 commit f116ab6

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/libproc_macro/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,6 @@ impl fmt::Display for Punct {
797797
/// REVIEW We should guarantee that `Ident` contains a valid identifier permitted by
798798
/// REVIEW the language and not a random unicode string, at least for a start.
799799
///
800-
/// REVIEW We need to support raw identifiers here (`r#ident`) or at least be future compatible
801-
/// REVIEW with them. Currently they are supported using "string typing" - if string "r#ident" is
802-
/// REVIEW passed to `Ident::new` it will be interpreted as a raw identifier later on, we should add
803-
/// REVIEW a field `is_raw` and a separate constructor for it (`Ident::new_raw` or something) and
804-
/// REVIEW keep it unstable until raw identifiers are stabilized.
805-
///
806800
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
807801
/// REVIEW Do we want to guarantee `Ident` to be `Copy`?
808802
#[derive(Copy, Clone, Debug)]
@@ -811,6 +805,7 @@ pub struct Ident {
811805
// REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
812806
sym: Symbol,
813807
span: Span,
808+
is_raw: bool,
814809
}
815810

816811
#[unstable(feature = "proc_macro", issue = "38356")]
@@ -844,9 +839,18 @@ impl Ident {
844839
Ident {
845840
sym: Symbol::intern(string),
846841
span,
842+
is_raw: false,
847843
}
848844
}
849845

846+
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
847+
#[unstable(feature = "proc_macro", issue = "38356")]
848+
pub fn new_raw(string: &str, span: Span) -> Ident {
849+
let mut ident = Ident::new(string, span);
850+
ident.is_raw = true;
851+
ident
852+
}
853+
850854
// FIXME: Remove this, do not stabilize
851855
/// Get a reference to the interned string.
852856
#[unstable(feature = "proc_macro", issue = "38356")]
@@ -1230,7 +1234,7 @@ impl TokenTree {
12301234
tt!(self::Ident::new(&ident.name.as_str(), Span(span)))
12311235
}
12321236
Ident(ident, true) => {
1233-
tt!(self::Ident::new(&format!("r#{}", ident), Span(span)))
1237+
tt!(self::Ident::new_raw(&ident.name.as_str(), Span(span)))
12341238
}
12351239
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),
12361240
DocComment(c) => {
@@ -1275,15 +1279,10 @@ impl TokenTree {
12751279
},
12761280
self::TokenTree::Ident(tt) => {
12771281
let ident = ast::Ident::new(tt.sym, tt.span.0);
1278-
let sym_str = tt.sym.to_string();
1279-
let token = if sym_str.starts_with("'") {
1282+
let token = if tt.sym.as_str().starts_with("'") {
12801283
Lifetime(ident)
1281-
} else if sym_str.starts_with("r#") {
1282-
let name = Symbol::intern(&sym_str[2..]);
1283-
let ident = ast::Ident::new(name, ident.span);
1284-
Ident(ident, true)
12851284
} else {
1286-
Ident(ident, false)
1285+
Ident(ident, tt.is_raw)
12871286
};
12881287
return TokenTree::Token(tt.span.0, token).into();
12891288
}

0 commit comments

Comments
 (0)