aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-25 04:51:42 +0000
committerGitHub <[email protected]>2019-11-25 04:51:42 +0000
commite00e6554ddc13be86733dc8a37a0a229931f378a (patch)
tree22947d2eb6fff1fa48488766c02e19fe752605ef /crates/ra_hir_expand/src
parenta888441c4745c3d72bee737c69873dfc9f52f4bb (diff)
parent67d3600f59684b4fe3eabe7036fe7e7ce4db3257 (diff)
Merge #2362
2362: Expand compile_error! r=edwin0cheng a=kjeremy Does not validate that the input is a string literal. I thought that I could `match_ast!` against the `macro_args` but that did not work. Even if it had I am not sure which error would be appropriate. Co-authored-by: Jeremy Kolb <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand/src')
-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 9b5305a80..d370dfb34 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
@@ -183,6 +184,26 @@ fn file_expand(
183 Ok(expanded) 184 Ok(expanded)
184} 185}
185 186
187fn compile_error_expand(
188 _db: &dyn AstDatabase,
189 _id: MacroCallId,
190 tt: &tt::Subtree,
191) -> Result<tt::Subtree, mbe::ExpandError> {
192 if tt.count() == 1 {
193 match &tt.token_trees[0] {
194 tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => {
195 let s = it.text.as_str();
196 if s.contains(r#"""#) {
197 return Ok(quote! { loop { #it }});
198 }
199 }
200 _ => {}
201 };
202 }
203
204 Err(mbe::ExpandError::BindingError("Must be a string".into()))
205}
206
186#[cfg(test)] 207#[cfg(test)]
187mod tests { 208mod tests {
188 use super::*; 209 use super::*;
@@ -270,4 +291,21 @@ mod tests {
270 291
271 assert_eq!(expanded, "\"\""); 292 assert_eq!(expanded, "\"\"");
272 } 293 }
294
295 #[test]
296 fn test_compile_error_expand() {
297 let expanded = expand_builtin_macro(
298 r#"
299 #[rustc_builtin_macro]
300 macro_rules! compile_error {
301 ($msg:expr) => ({ /* compiler built-in */ });
302 ($msg:expr,) => ({ /* compiler built-in */ })
303 }
304 compile_error!("error!");
305"#,
306 BuiltinFnLikeExpander::CompileError,
307 );
308
309 assert_eq!(expanded, r#"loop{"error!"}"#);
310 }
273} 311}
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");