about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChayim Refael Friedman <chayimfr@gmail.com>2025-04-26 21:10:43 +0300
committerChayim Refael Friedman <chayimfr@gmail.com>2025-04-26 21:10:43 +0300
commit2b701e00f12e41ed2d9a75b6fd14fb20640d159d (patch)
tree2849475ec4191003cf40a5bb280d9480f07ae7e8
parentf801657241d6f4ff0e3f9dbadef8f4dbbdddcbfe (diff)
downloadrust-2b701e00f12e41ed2d9a75b6fd14fb20640d159d.tar.gz
rust-2b701e00f12e41ed2d9a75b6fd14fb20640d159d.zip
Escape raw names in labels properly
-rw-r--r--src/tools/rust-analyzer/crates/hir-expand/src/name.rs15
-rw-r--r--src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs16
2 files changed, 26 insertions, 5 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/name.rs b/src/tools/rust-analyzer/crates/hir-expand/src/name.rs
index d43ef38f291..37290edae4e 100644
--- a/src/tools/rust-analyzer/crates/hir-expand/src/name.rs
+++ b/src/tools/rust-analyzer/crates/hir-expand/src/name.rs
@@ -191,7 +191,7 @@ impl Name {
     // FIXME: Remove this in favor of `display`, see fixme on `as_str`
     #[doc(hidden)]
     pub fn display_no_db(&self, edition: Edition) -> impl fmt::Display + '_ {
-        Display { name: self, needs_escaping: is_raw_identifier(self.symbol.as_str(), edition) }
+        Display { name: self, edition }
     }
 
     pub fn symbol(&self) -> &Symbol {
@@ -201,15 +201,20 @@ impl Name {
 
 struct Display<'a> {
     name: &'a Name,
-    needs_escaping: bool,
+    edition: Edition,
 }
 
 impl fmt::Display for Display<'_> {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        if self.needs_escaping {
-            write!(f, "r#")?;
+        let mut symbol = self.name.symbol.as_str();
+        if let Some(s) = symbol.strip_prefix('\'') {
+            f.write_str("'")?;
+            symbol = s;
         }
-        fmt::Display::fmt(self.name.symbol.as_str(), f)
+        if is_raw_identifier(symbol, self.edition) {
+            f.write_str("r#")?;
+        }
+        f.write_str(symbol)
     }
 }
 
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs
index b30ac43bf8f..27d6bc7b14f 100644
--- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs
+++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/expression.rs
@@ -2110,3 +2110,19 @@ fn foo() {
         "#]],
     );
 }
+
+#[test]
+fn escaped_label() {
+    check(
+        r#"
+fn main() {
+    'r#break: {
+        break '$0;
+    }
+}
+    "#,
+        expect![[r#"
+            lb 'r#break
+        "#]],
+    );
+}