From e04304ffa3faad9677f313a199020c1f78f74fa0 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 19 Jun 2022 18:03:46 -0700 Subject: [PATCH 1/2] Move raw ident validate into validate_ident --- src/fallback.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index 80e5cced..8567a13b 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -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(), @@ -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) } @@ -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"); @@ -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 { From 868a8ea2d94a705e709dc77a6803cee05ed3d5e4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 19 Jun 2022 18:08:19 -0700 Subject: [PATCH 2/2] Restore compatibility with rustc older than 1.33 error[E0658]: multiple patterns in `if let` and `while let` are unstable (see issue #48215) --> src/fallback.rs:704:9 | 704 | / if let "" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" = string { 705 | | panic!("`{}` cannot be a raw identifier", string); 706 | | } | |_________^ --- src/fallback.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index 8567a13b..fecc9b3e 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -701,8 +701,11 @@ fn validate_ident(string: &str, raw: bool) { } if raw { - if let "" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" = string { - panic!("`{}` cannot be a raw identifier", string); + match string { + "" | "_" | "super" | "self" | "Self" | "crate" | "$crate" | "{{root}}" => { + panic!("`{}` cannot be a raw identifier", string); + } + _ => {} } } }