aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Kolb <[email protected]>2019-11-25 00:01:51 +0000
committerJeremy Kolb <[email protected]>2019-11-25 00:01:51 +0000
commit67d3600f59684b4fe3eabe7036fe7e7ce4db3257 (patch)
tree49c8682288939bbba55b94a79f3b6f629873f79d
parentf7f9757b6b144385ab8ce57b15764473b1f57331 (diff)
Expand compile_error!
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs38
-rw-r--r--crates/ra_hir_expand/src/name.rs1
2 files changed, 39 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index c0e0436c0..ffd796ae2 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -46,6 +46,7 @@ macro_rules! register_builtin {
46 46
47register_builtin! { 47register_builtin! {
48 (COLUMN_MACRO, Column) => column_expand, 48 (COLUMN_MACRO, Column) => column_expand,
49 (COMPILE_ERROR_MACRO, CompileError) => compile_error_expand,
49 (FILE_MACRO, File) => file_expand, 50 (FILE_MACRO, File) => file_expand,
50 (LINE_MACRO, Line) => line_expand, 51 (LINE_MACRO, Line) => line_expand,
51 (STRINGIFY_MACRO, Stringify) => stringify_expand 52 (STRINGIFY_MACRO, Stringify) => stringify_expand
@@ -172,6 +173,26 @@ fn file_expand(
172 Ok(expanded) 173 Ok(expanded)
173} 174}
174 175
176fn compile_error_expand(
177 _db: &dyn AstDatabase,
178 _id: MacroCallId,
179 tt: &tt::Subtree,
180) -> Result<tt::Subtree, mbe::ExpandError> {
181 if tt.count() == 1 {
182 match &tt.token_trees[0] {
183 tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
184 let s = it.text.as_str();
185 if s.contains(r#"""#) {
186 return Ok(quote! { loop { #it }});
187 }
188 }
189 _ => {}
190 };
191 }
192
193 Err(mbe::ExpandError::BindingError("Must be a string".into()))
194}
195
175#[cfg(test)] 196#[cfg(test)]
176mod tests { 197mod tests {
177 use super::*; 198 use super::*;
@@ -259,4 +280,21 @@ mod tests {
259 280
260 assert_eq!(expanded, "\"\""); 281 assert_eq!(expanded, "\"\"");
261 } 282 }
283
284 #[test]
285 fn test_compile_error_expand() {
286 let expanded = expand_builtin_macro(
287 r#"
288 #[rustc_builtin_macro]
289 macro_rules! compile_error {
290 ($msg:expr) => ({ /* compiler built-in */ });
291 ($msg:expr,) => ({ /* compiler built-in */ })
292 }
293 compile_error!("error!");
294"#,
295 BuiltinFnLikeExpander::CompileError,
296 );
297
298 assert_eq!(expanded, r#"loop{"error!"}"#);
299 }
262} 300}
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index eaea7a6a8..7824489d7 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -144,5 +144,6 @@ pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
144// Builtin Macros 144// Builtin Macros
145pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file"); 145pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file");
146pub const COLUMN_MACRO: Name = Name::new_inline_ascii(6, b"column"); 146pub const COLUMN_MACRO: Name = Name::new_inline_ascii(6, b"column");
147pub const COMPILE_ERROR_MACRO: Name = Name::new_inline_ascii(13, b"compile_error");
147pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); 148pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
148pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify"); 149pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");