about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-11-24 21:00:48 +0000
committerbors <bors@rust-lang.org>2022-11-24 21:00:48 +0000
commit76e2e41121af3f4ca4835a0a0c8fdf290d5e36ba (patch)
treed131f40516f891ef65803a11a97d47e867d57275
parent5e3ad5ddf6d6ec44a447454493725032ee6e8358 (diff)
parent427b63b676c89dc6be407181f25f74ddc9950ea1 (diff)
downloadrust-76e2e41121af3f4ca4835a0a0c8fdf290d5e36ba.tar.gz
rust-76e2e41121af3f4ca4835a0a0c8fdf290d5e36ba.zip
Auto merge of #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!
-rw-r--r--crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs6
-rw-r--r--crates/hir-expand/src/builtin_fn_macro.rs13
2 files changed, 8 insertions, 11 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs b/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
index c04cd165192..bb45266725c 100644
--- a/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
+++ b/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
@@ -163,7 +163,8 @@ macro_rules! compile_error {
 }
 
 // This expands to nothing (since it's in item position), but emits an error.
-compile_error!("error!");
+compile_error!("error, with an escaped quote: \"");
+compile_error!(r"this is a raw string");
 "#,
         expect![[r##"
 #[rustc_builtin_macro]
@@ -172,7 +173,8 @@ macro_rules! compile_error {
     ($msg:expr,) => ({ /* compiler built-in */ })
 }
 
-/* error: error! */
+/* error: error, with an escaped quote: " */
+/* error: this is a raw string */
 "##]],
     );
 }
diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs
index 7b19518e25a..985954d44e4 100644
--- a/crates/hir-expand/src/builtin_fn_macro.rs
+++ b/crates/hir-expand/src/builtin_fn_macro.rs
@@ -379,15 +379,10 @@ fn compile_error_expand(
     tt: &tt::Subtree,
 ) -> ExpandResult<ExpandedEager> {
     let err = match &*tt.token_trees {
-        [tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
-            let text = it.text.as_str();
-            if text.starts_with('"') && text.ends_with('"') {
-                // FIXME: does not handle raw strings
-                ExpandError::Other(text[1..text.len() - 1].into())
-            } else {
-                ExpandError::Other("`compile_error!` argument must be a string".into())
-            }
-        }
+        [tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => match unquote_str(it) {
+            Some(unquoted) => ExpandError::Other(unquoted.into()),
+            None => ExpandError::Other("`compile_error!` argument must be a string".into()),
+        },
         _ => ExpandError::Other("`compile_error!` argument must be a string".into()),
     };