From 57f0e9c1006896bc6bead470c87aea8b015e5af8 Mon Sep 17 00:00:00 2001 From: Ryo Yoshida Date: Mon, 13 Feb 2023 18:05:23 +0900 Subject: [PATCH] Disallow invalid raw ident names --- crates/ide-db/src/rename.rs | 9 ++++++++- crates/ide/src/rename.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/ide-db/src/rename.rs b/crates/ide-db/src/rename.rs index 689d23efd7..84d70b258f 100644 --- a/crates/ide-db/src/rename.rs +++ b/crates/ide-db/src/rename.rs @@ -537,7 +537,14 @@ impl IdentifierKind { pub fn classify(new_name: &str) -> Result { match parser::LexedStr::single_token(new_name) { Some(res) => match res { - (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident), + (SyntaxKind::IDENT, _) => { + if let Some(inner) = new_name.strip_prefix("r#") { + if matches!(inner, "self" | "crate" | "super" | "Self") { + bail!("Invalid name: `{}` cannot be a raw identifier", inner); + } + } + Ok(IdentifierKind::Ident) + } (T![_], _) => Ok(IdentifierKind::Underscore), (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => { Ok(IdentifierKind::Lifetime) diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index 84a14ccd97..8e89160ef5 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -562,6 +562,15 @@ impl Foo { ); } + #[test] + fn test_rename_mod_invalid_raw_ident() { + check( + "r#self", + r#"mod foo$0 {}"#, + "error: Invalid name: `self` cannot be a raw identifier", + ); + } + #[test] fn test_rename_for_local() { check(