Skip to content

Commit 20c5044

Browse files
committed
Introduce rustc_lexer::is_ident and use it in couple of places
1 parent 1275cc1 commit 20c5044

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,7 @@ dependencies = [
32573257
"rustc_data_structures",
32583258
"rustc_errors",
32593259
"rustc_feature",
3260+
"rustc_lexer",
32603261
"rustc_macros",
32613262
"rustc_serialize",
32623263
"rustc_session",

src/librustc_attr/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc_errors = { path = "../librustc_errors" }
1616
rustc_span = { path = "../librustc_span" }
1717
rustc_data_structures = { path = "../librustc_data_structures" }
1818
rustc_feature = { path = "../librustc_feature" }
19+
rustc_lexer = { path = "../librustc_lexer" }
1920
rustc_macros = { path = "../librustc_macros" }
2021
rustc_session = { path = "../librustc_session" }
2122
rustc_ast = { path = "../librustc_ast" }

src/librustc_attr/builtin.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum AttrError {
2020
MultipleItem(String),
2121
UnknownMetaItem(String, &'static [&'static str]),
2222
MissingSince,
23+
NonIdentFeature,
2324
MissingFeature,
2425
MultipleStabilityLevels,
2526
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
@@ -40,6 +41,9 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
4041
AttrError::MissingSince => {
4142
struct_span_err!(diag, span, E0542, "missing 'since'").emit();
4243
}
44+
AttrError::NonIdentFeature => {
45+
struct_span_err!(diag, span, E0546, "'feature' is not an identifier").emit();
46+
}
4347
AttrError::MissingFeature => {
4448
struct_span_err!(diag, span, E0546, "missing 'feature'").emit();
4549
}
@@ -344,6 +348,14 @@ where
344348

345349
match (feature, reason, issue) {
346350
(Some(feature), reason, Some(_)) => {
351+
if !rustc_lexer::is_ident(&feature.as_str()) {
352+
handle_errors(
353+
&sess.parse_sess,
354+
attr.span,
355+
AttrError::NonIdentFeature,
356+
);
357+
continue;
358+
}
347359
let level = Unstable { reason, issue: issue_num, is_soft };
348360
if sym::unstable == meta_name {
349361
stab = Some(Stability { level, feature });

src/librustc_expand/proc_macro_server.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,10 @@ pub struct Ident {
319319
}
320320

321321
impl Ident {
322-
fn is_valid(string: &str) -> bool {
323-
let mut chars = string.chars();
324-
if let Some(start) = chars.next() {
325-
rustc_lexer::is_id_start(start) && chars.all(rustc_lexer::is_id_continue)
326-
} else {
327-
false
328-
}
329-
}
330322
fn new(sess: &ParseSess, sym: Symbol, is_raw: bool, span: Span) -> Ident {
331323
let sym = nfc_normalize(&sym.as_str());
332324
let string = sym.as_str();
333-
if !Self::is_valid(&string) {
325+
if !rustc_lexer::is_ident(&string) {
334326
panic!("`{:?}` is not a valid identifier", string)
335327
}
336328
if is_raw && !sym.can_be_raw() {

src/librustc_lexer/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ pub fn is_id_continue(c: char) -> bool {
274274
|| (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_continue(c))
275275
}
276276

277+
/// The passed string is lexically an identifier.
278+
pub fn is_ident(string: &str) -> bool {
279+
let mut chars = string.chars();
280+
if let Some(start) = chars.next() {
281+
is_id_start(start) && chars.all(is_id_continue)
282+
} else {
283+
false
284+
}
285+
}
286+
277287
impl Cursor<'_> {
278288
/// Parses a token from the input string.
279289
fn advance_token(&mut self) -> Token {

0 commit comments

Comments
 (0)