about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-09-10 16:18:30 +0000
committerbors <bors@rust-lang.org>2024-09-10 16:18:30 +0000
commit14a4f07fd18873bf6d8c2946101da0bd607de0cc (patch)
tree78670c4a3abe2aa4fe90d4073b415243e2900577 /src/tools
parent2053d7d23e1d92f1cd0993f9b600380daeb2ce40 (diff)
parent16f3eb969f319b5846e96b38c46a4933c24e03d8 (diff)
downloadrust-14a4f07fd18873bf6d8c2946101da0bd607de0cc.tar.gz
rust-14a4f07fd18873bf6d8c2946101da0bd607de0cc.zip
Auto merge of #18092 - ChayimFriedman2:fix-stringify, r=lnicola
fix: Correctly escape strings in our quote macro

This is a small change, but it was the cause of 90% of the errors in `rust-analyzer diagnostics .` 🫢 (because this worked incorrectly with `stringify!()`, which means every `quote!()` (the original one) quoting a string also didn't work).

With this change and #18085 together, all remaining errors are type errors.

This may mean we can enable more errors, but this is out of scope for this PR.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs18
-rw-r--r--src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs8
-rw-r--r--src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs5
3 files changed, 25 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs b/src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
index 37ae1ab39b4..a3b48831a0b 100644
--- a/src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
+++ b/src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
@@ -528,3 +528,21 @@ fn main() { foobar; }
 "##]],
     );
 }
+
+#[test]
+fn test_quote_string() {
+    check(
+        r##"
+#[rustc_builtin_macro]
+macro_rules! stringify {}
+
+fn main() { stringify!("hello"); }
+"##,
+        expect![[r##"
+#[rustc_builtin_macro]
+macro_rules! stringify {}
+
+fn main() { "\"hello\""; }
+"##]],
+    );
+}
diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs b/src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs
index 15fed45caf0..d04225b8722 100644
--- a/src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs
+++ b/src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs
@@ -483,7 +483,7 @@ fn concat_expand(
                 match it.kind {
                     tt::LitKind::Char => {
                         if let Ok(c) = unescape_char(it.symbol.as_str()) {
-                            text.extend(c.escape_default());
+                            text.push(c);
                         }
                         record_span(it.span);
                     }
@@ -491,11 +491,11 @@ fn concat_expand(
                         format_to!(text, "{}", it.symbol.as_str())
                     }
                     tt::LitKind::Str => {
-                        text.push_str(it.symbol.as_str());
+                        text.push_str(unescape_str(&it.symbol).as_str());
                         record_span(it.span);
                     }
                     tt::LitKind::StrRaw(_) => {
-                        format_to!(text, "{}", it.symbol.as_str().escape_debug());
+                        format_to!(text, "{}", it.symbol.as_str());
                         record_span(it.span);
                     }
                     tt::LitKind::Byte
@@ -813,7 +813,7 @@ fn include_str_expand(
 
 fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &Symbol) -> Option<String> {
     let krate = db.lookup_intern_macro_call(arg_id).krate;
-    db.crate_graph()[krate].env.get(key.as_str()).map(|it| it.escape_debug().to_string())
+    db.crate_graph()[krate].env.get(key.as_str())
 }
 
 fn env_expand(
diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs b/src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs
index 5c33f817f9e..fc248c906da 100644
--- a/src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs
+++ b/src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs
@@ -3,6 +3,7 @@
 
 use intern::{sym, Symbol};
 use span::Span;
+use syntax::ToSmolStr;
 use tt::IdentIsRaw;
 
 use crate::name::Name;
@@ -211,8 +212,8 @@ impl_to_to_tokentrees! {
     _span: crate::tt::Literal => self { self };
     _span: crate::tt::Ident => self { self };
     _span: crate::tt::Punct => self { self };
-    span: &str => self { crate::tt::Literal{symbol: Symbol::intern(self), span, kind: tt::LitKind::Str, suffix: None }};
-    span: String => self { crate::tt::Literal{symbol: Symbol::intern(&self), span, kind: tt::LitKind::Str, suffix: None }};
+    span: &str => self { crate::tt::Literal{symbol: Symbol::intern(&self.escape_default().to_smolstr()), span, kind: tt::LitKind::Str, suffix: None }};
+    span: String => self { crate::tt::Literal{symbol: Symbol::intern(&self.escape_default().to_smolstr()), span, kind: tt::LitKind::Str, suffix: None }};
     span: Name => self {
         let (is_raw, s) = IdentIsRaw::split_from_symbol(self.as_str());
         crate::tt::Ident{sym: Symbol::intern(s), span, is_raw }