Skip to content

Commit

Permalink
Reject invalid raw identifiers in the fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mystor committed Jun 20, 2022
1 parent 2ecccd0 commit 0e97a92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/fallback.rs
Expand Up @@ -640,13 +640,24 @@ impl Ident {
fn _new(string: &str, raw: bool, span: Span) -> Self {
validate_ident(string);

if raw && !Self::can_be_raw(string) {
panic!("`{}` cannot be a raw identifier", string);
}

Ident {
sym: string.to_owned(),
span,
raw,
}
}

fn can_be_raw(string: &str) -> bool {
match string {
"" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" => false,
_ => true,
}
}

pub fn new(string: &str, span: Span) -> Self {
Ident::_new(string, false, span)
}
Expand Down
11 changes: 10 additions & 1 deletion tests/test.rs
Expand Up @@ -21,7 +21,16 @@ fn raw_idents() {
"r#String"
);
assert_eq!(Ident::new_raw("fn", Span::call_site()).to_string(), "r#fn");
assert_eq!(Ident::new_raw("_", Span::call_site()).to_string(), "r#_");
}

#[test]
#[should_panic(expected = "`_` cannot be a raw identifier")]
fn ident_raw_blocked() {
Ident::new_raw("_", Span::call_site());
Ident::new_raw("super", Span::call_site());
Ident::new_raw("self", Span::call_site());
Ident::new_raw("Self", Span::call_site());
Ident::new_raw("crate", Span::call_site());
}

#[test]
Expand Down

0 comments on commit 0e97a92

Please sign in to comment.