]> git.lizzy.rs Git - rust.git/commitdiff
Expand compile_error!
authorJeremy Kolb <kjeremy@gmail.com>
Mon, 25 Nov 2019 00:01:51 +0000 (19:01 -0500)
committerJeremy Kolb <kjeremy@gmail.com>
Mon, 25 Nov 2019 00:01:51 +0000 (19:01 -0500)
crates/ra_hir_expand/src/builtin_macro.rs
crates/ra_hir_expand/src/name.rs

index c0e0436c0a597eae56f028db68418a26ce311437..ffd796ae2b3d2ab868c4fe441d5c661ace4c545d 100644 (file)
@@ -46,6 +46,7 @@ pub fn find_builtin_macro(
 
 register_builtin! {
     (COLUMN_MACRO, Column) => column_expand,
+    (COMPILE_ERROR_MACRO, CompileError) => compile_error_expand,
     (FILE_MACRO, File) => file_expand,
     (LINE_MACRO, Line) => line_expand,
     (STRINGIFY_MACRO, Stringify) => stringify_expand
@@ -172,6 +173,26 @@ fn file_expand(
     Ok(expanded)
 }
 
+fn compile_error_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    if tt.count() == 1 {
+        match &tt.token_trees[0] {
+            tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
+                let s = it.text.as_str();
+                if s.contains(r#"""#) {
+                    return Ok(quote! { loop { #it }});
+                }
+            }
+            _ => {}
+        };
+    }
+
+    Err(mbe::ExpandError::BindingError("Must be a string".into()))
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -259,4 +280,21 @@ macro_rules! file {() => {}}
 
         assert_eq!(expanded, "\"\"");
     }
+
+    #[test]
+    fn test_compile_error_expand() {
+        let expanded = expand_builtin_macro(
+            r#"
+        #[rustc_builtin_macro]
+        macro_rules! compile_error {
+            ($msg:expr) => ({ /* compiler built-in */ });
+            ($msg:expr,) => ({ /* compiler built-in */ })
+        }
+        compile_error!("error!");
+"#,
+            BuiltinFnLikeExpander::CompileError,
+        );
+
+        assert_eq!(expanded, r#"loop{"error!"}"#);
+    }
 }
index eaea7a6a86de295f8440349538cc9bb2a259a255..7824489d7f4cd40ae7761a6854539508c82e9b35 100644 (file)
@@ -144,5 +144,6 @@ fn as_name(&self) -> Name {
 // Builtin Macros
 pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file");
 pub const COLUMN_MACRO: Name = Name::new_inline_ascii(6, b"column");
+pub const COMPILE_ERROR_MACRO: Name = Name::new_inline_ascii(13, b"compile_error");
 pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
 pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");