Skip to content

Commit

Permalink
Move raw ident validate into validate_ident
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jun 20, 2022
1 parent 8b3a2dd commit e04304f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,7 @@ pub(crate) struct Ident {

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);
}
validate_ident(string, raw);

Ident {
sym: string.to_owned(),
Expand All @@ -651,13 +647,6 @@ impl Ident {
}
}

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 All @@ -683,7 +672,7 @@ pub(crate) fn is_ident_continue(c: char) -> bool {
unicode_ident::is_xid_continue(c)
}

fn validate_ident(string: &str) {
fn validate_ident(string: &str, raw: bool) {
let validate = string;
if validate.is_empty() {
panic!("Ident is not allowed to be empty; use Option<Ident>");
Expand All @@ -710,6 +699,12 @@ fn validate_ident(string: &str) {
if !ident_ok(validate) {
panic!("{:?} is not a valid Ident", string);
}

if raw {
if let "" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" = string {
panic!("`{}` cannot be a raw identifier", string);
}
}
}

impl PartialEq for Ident {
Expand Down

0 comments on commit e04304f

Please sign in to comment.