Skip to content

Commit 76e2e41

Browse files
committed
Auto merge of rust-lang#13652 - jhgg:hir-expand/fix-compile-error-expansion, r=Veykril
hir-expand: fix compile_error! expansion not unquoting strings expanding `compile_error!` would not properly unquote strings, leading to quite ugly diagnostic messages: ![image](https://user-images.githubusercontent.com/5489149/202893481-2486ede8-c79a-4972-9713-416d6a704064.png) this fixes it, using the conveniently placed `unquote_str` function, which now makes errors look like: ![image](https://user-images.githubusercontent.com/5489149/202893466-0763efad-9240-4d55-80a6-6c62000d5d2b.png) additionally, using `unquote_str` has the cool side-effect of *also* handling raw strings, so this fixes a fixme too!
2 parents 5e3ad5d + 427b63b commit 76e2e41

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ macro_rules! compile_error {
163163
}
164164
165165
// This expands to nothing (since it's in item position), but emits an error.
166-
compile_error!("error!");
166+
compile_error!("error, with an escaped quote: \"");
167+
compile_error!(r"this is a raw string");
167168
"#,
168169
expect![[r##"
169170
#[rustc_builtin_macro]
@@ -172,7 +173,8 @@ macro_rules! compile_error {
172173
($msg:expr,) => ({ /* compiler built-in */ })
173174
}
174175
175-
/* error: error! */
176+
/* error: error, with an escaped quote: " */
177+
/* error: this is a raw string */
176178
"##]],
177179
);
178180
}

crates/hir-expand/src/builtin_fn_macro.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,10 @@ fn compile_error_expand(
379379
tt: &tt::Subtree,
380380
) -> ExpandResult<ExpandedEager> {
381381
let err = match &*tt.token_trees {
382-
[tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
383-
let text = it.text.as_str();
384-
if text.starts_with('"') && text.ends_with('"') {
385-
// FIXME: does not handle raw strings
386-
ExpandError::Other(text[1..text.len() - 1].into())
387-
} else {
388-
ExpandError::Other("`compile_error!` argument must be a string".into())
389-
}
390-
}
382+
[tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => match unquote_str(it) {
383+
Some(unquoted) => ExpandError::Other(unquoted.into()),
384+
None => ExpandError::Other("`compile_error!` argument must be a string".into()),
385+
},
391386
_ => ExpandError::Other("`compile_error!` argument must be a string".into()),
392387
};
393388

0 commit comments

Comments
 (0)