Skip to content

Commit bcc7089

Browse files
committed
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.
2 parents f13c776 + 7c44d45 commit bcc7089

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

+18
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,21 @@ fn main() { foobar; }
528528
"##]],
529529
);
530530
}
531+
532+
#[test]
533+
fn test_quote_string() {
534+
check(
535+
r##"
536+
#[rustc_builtin_macro]
537+
macro_rules! stringify {}
538+
539+
fn main() { stringify!("hello"); }
540+
"##,
541+
expect![[r##"
542+
#[rustc_builtin_macro]
543+
macro_rules! stringify {}
544+
545+
fn main() { "\"hello\""; }
546+
"##]],
547+
);
548+
}

crates/hir-expand/src/builtin/fn_macro.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -483,19 +483,19 @@ fn concat_expand(
483483
match it.kind {
484484
tt::LitKind::Char => {
485485
if let Ok(c) = unescape_char(it.symbol.as_str()) {
486-
text.extend(c.escape_default());
486+
text.push(c);
487487
}
488488
record_span(it.span);
489489
}
490490
tt::LitKind::Integer | tt::LitKind::Float => {
491491
format_to!(text, "{}", it.symbol.as_str())
492492
}
493493
tt::LitKind::Str => {
494-
text.push_str(it.symbol.as_str());
494+
text.push_str(unescape_str(&it.symbol).as_str());
495495
record_span(it.span);
496496
}
497497
tt::LitKind::StrRaw(_) => {
498-
format_to!(text, "{}", it.symbol.as_str().escape_debug());
498+
format_to!(text, "{}", it.symbol.as_str());
499499
record_span(it.span);
500500
}
501501
tt::LitKind::Byte
@@ -813,7 +813,7 @@ fn include_str_expand(
813813

814814
fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &Symbol) -> Option<String> {
815815
let krate = db.lookup_intern_macro_call(arg_id).krate;
816-
db.crate_graph()[krate].env.get(key.as_str()).map(|it| it.escape_debug().to_string())
816+
db.crate_graph()[krate].env.get(key.as_str())
817817
}
818818

819819
fn env_expand(

crates/hir-expand/src/builtin/quote.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use intern::{sym, Symbol};
55
use span::Span;
6+
use syntax::ToSmolStr;
67
use tt::IdentIsRaw;
78

89
use crate::name::Name;
@@ -211,8 +212,8 @@ impl_to_to_tokentrees! {
211212
_span: crate::tt::Literal => self { self };
212213
_span: crate::tt::Ident => self { self };
213214
_span: crate::tt::Punct => self { self };
214-
span: &str => self { crate::tt::Literal{symbol: Symbol::intern(self), span, kind: tt::LitKind::Str, suffix: None }};
215-
span: String => self { crate::tt::Literal{symbol: Symbol::intern(&self), span, kind: tt::LitKind::Str, suffix: None }};
215+
span: &str => self { crate::tt::Literal{symbol: Symbol::intern(&self.escape_default().to_smolstr()), span, kind: tt::LitKind::Str, suffix: None }};
216+
span: String => self { crate::tt::Literal{symbol: Symbol::intern(&self.escape_default().to_smolstr()), span, kind: tt::LitKind::Str, suffix: None }};
216217
span: Name => self {
217218
let (is_raw, s) = IdentIsRaw::split_from_symbol(self.as_str());
218219
crate::tt::Ident{sym: Symbol::intern(s), span, is_raw }

0 commit comments

Comments
 (0)