Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move raw ident validate into validate_ident #332

Merged
merged 2 commits into from Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/fallback.rs
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,15 @@ fn validate_ident(string: &str) {
if !ident_ok(validate) {
panic!("{:?} is not a valid Ident", string);
}

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

impl PartialEq for Ident {
Expand Down