Skip to content

Commit 6ad55b3

Browse files
committed
syntax: Introduce Ident::can_be_raw
1 parent 5cb5083 commit 6ad55b3

File tree

10 files changed

+33
-36
lines changed

10 files changed

+33
-36
lines changed

src/libsyntax/parse/lexer/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ast::{self, Ident};
22
use crate::source_map::{SourceMap, FilePathMapping};
33
use crate::parse::{token, ParseSess};
4-
use crate::symbol::{Symbol, keywords};
4+
use crate::symbol::Symbol;
55

66
use errors::{Applicability, FatalError, Diagnostic, DiagnosticBuilder};
77
use syntax_pos::{BytePos, CharPos, Pos, Span, NO_EXPANSION};
@@ -1249,15 +1249,11 @@ impl<'a> StringReader<'a> {
12491249
// FIXME: perform NFKC normalization here. (Issue #2253)
12501250
let ident = self.mk_ident(string);
12511251

1252-
if is_raw_ident && (ident.is_path_segment_keyword() ||
1253-
ident.name == keywords::Underscore.name()) {
1254-
self.fatal_span_(raw_start, self.pos,
1255-
&format!("`r#{}` is not currently supported.", ident.name)
1256-
).raise();
1257-
}
1258-
12591252
if is_raw_ident {
12601253
let span = self.mk_sp(raw_start, self.pos);
1254+
if !ident.can_be_raw() {
1255+
self.err_span(span, &format!("`{}` cannot be a raw identifier", ident));
1256+
}
12611257
self.sess.raw_identifier_spans.borrow_mut().push(span);
12621258
}
12631259

src/libsyntax/parse/parser.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,7 @@ impl<'a> Parser<'a> {
895895
&format!("expected identifier, found {}",
896896
self.this_token_descr()));
897897
if let token::Ident(ident, false) = &self.token {
898-
if ident.is_reserved() && !ident.is_path_segment_keyword() &&
899-
ident.name != keywords::Underscore.name()
900-
{
898+
if ident.is_raw_guess() {
901899
err.span_suggestion(
902900
self.span,
903901
"you can escape reserved keywords to use them as identifiers",

src/libsyntax_ext/proc_macro_decls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a> CollectProcMacros<'a> {
128128
}
129129
};
130130

131-
if trait_ident.is_path_segment_keyword() {
131+
if !trait_ident.can_be_raw() {
132132
self.handler.span_err(trait_attr.span(),
133133
&format!("`{}` cannot be a name of derive macro", trait_ident));
134134
}
@@ -162,7 +162,7 @@ impl<'a> CollectProcMacros<'a> {
162162
return None;
163163
}
164164
};
165-
if ident.is_path_segment_keyword() {
165+
if !ident.can_be_raw() {
166166
self.handler.span_err(
167167
attr.span(),
168168
&format!("`{}` cannot be a name of derive helper attribute", ident),

src/libsyntax_ext/proc_macro_server.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,8 @@ impl Ident {
340340
if !Self::is_valid(string) {
341341
panic!("`{:?}` is not a valid identifier", string)
342342
}
343-
if is_raw {
344-
let normalized_sym = Symbol::intern(string);
345-
if normalized_sym == keywords::Underscore.name() ||
346-
ast::Ident::with_empty_ctxt(normalized_sym).is_path_segment_keyword() {
347-
panic!("`{:?}` is not a valid raw identifier", string)
348-
}
343+
if is_raw && !ast::Ident::from_str(string).can_be_raw() {
344+
panic!("`{}` cannot be a raw identifier", string);
349345
}
350346
Ident { sym, is_raw, span }
351347
}

src/libsyntax_pos/symbol.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,16 @@ impl Ident {
484484
self.name == keywords::DollarCrate.name()
485485
}
486486

487-
// We see this identifier in a normal identifier position, like variable name or a type.
488-
// How was it written originally? Did it use the raw form? Let's try to guess.
489-
pub fn is_raw_guess(self) -> bool {
487+
/// This identifier can be a raw identifier.
488+
pub fn can_be_raw(self) -> bool {
490489
self.name != keywords::Invalid.name() && self.name != keywords::Underscore.name() &&
491-
self.is_reserved() && !self.is_path_segment_keyword()
490+
!self.is_path_segment_keyword()
491+
}
492+
493+
/// We see this identifier in a normal identifier position, like variable name or a type.
494+
/// How was it written originally? Did it use the raw form? Let's try to guess.
495+
pub fn is_raw_guess(self) -> bool {
496+
self.can_be_raw() && self.is_reserved()
492497
}
493498
}
494499

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
fn self_test(r#self: u32) {
2-
//~^ ERROR `r#self` is not currently supported.
1+
fn main() {
2+
let r#self;
3+
//~^ ERROR `self` cannot be a raw identifier
34
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: `r#self` is not currently supported.
2-
--> $DIR/raw-literal-self.rs:1:14
1+
error: `self` cannot be a raw identifier
2+
--> $DIR/raw-literal-self.rs:2:9
33
|
4-
LL | fn self_test(r#self: u32) {
5-
| ^^^^^^
4+
LL | let r#self;
5+
| ^^^^^^
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
fn underscore_test(r#_: u32) {
2-
//~^ ERROR `r#_` is not currently supported.
1+
fn main() {
2+
let r#_;
3+
//~^ ERROR `_` cannot be a raw identifier
34
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: `r#_` is not currently supported.
2-
--> $DIR/raw-literal-underscore.rs:1:20
1+
error: `_` cannot be a raw identifier
2+
--> $DIR/raw-literal-underscore.rs:2:9
33
|
4-
LL | fn underscore_test(r#_: u32) {
5-
| ^^^
4+
LL | let r#_;
5+
| ^^^
66

77
error: aborting due to previous error
88

src/test/ui/proc-macro/invalid-punct-ident-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: proc macro panicked
44
LL | invalid_raw_ident!();
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: message: `"self"` is not a valid raw identifier
7+
= help: message: `self` cannot be a raw identifier
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)