aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-03-11 15:08:12 +0000
committerEdwin Cheng <[email protected]>2020-03-11 15:08:12 +0000
commit759bcea96de448a7758276a9ce5696a56b44b465 (patch)
tree05d41725af5744110390becb6b813a907e685b51 /crates
parentbddf6b5266997dd7e017fcb963e54a86b68afbaa (diff)
Implement dummy assert macro
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs56
-rw-r--r--crates/ra_hir_expand/src/name.rs1
-rw-r--r--crates/ra_hir_expand/src/quote.rs1
-rw-r--r--crates/ra_ide/src/hover.rs8
4 files changed, 60 insertions, 6 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index a90007f26..01f78fc77 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -88,6 +88,7 @@ register_builtin! {
88 (compile_error, CompileError) => compile_error_expand, 88 (compile_error, CompileError) => compile_error_expand,
89 (file, File) => file_expand, 89 (file, File) => file_expand,
90 (line, Line) => line_expand, 90 (line, Line) => line_expand,
91 (assert, Assert) => assert_expand,
91 (stringify, Stringify) => stringify_expand, 92 (stringify, Stringify) => stringify_expand,
92 (format_args, FormatArgs) => format_args_expand, 93 (format_args, FormatArgs) => format_args_expand,
93 // format_args_nl only differs in that it adds a newline in the end, 94 // format_args_nl only differs in that it adds a newline in the end,
@@ -151,6 +152,45 @@ fn column_expand(
151 Ok(expanded) 152 Ok(expanded)
152} 153}
153 154
155fn assert_expand(
156 _db: &dyn AstDatabase,
157 _id: LazyMacroId,
158 tt: &tt::Subtree,
159) -> Result<tt::Subtree, mbe::ExpandError> {
160 // A hacky implementation for goto def and hover
161 // We expand `assert!("", arg1, arg2)` to
162 // ```
163 // {(&(arg1), &(arg2));}
164 // ```,
165 // which is wrong but useful.
166
167 let mut args = Vec::new();
168 let mut current = Vec::new();
169 for tt in tt.token_trees.iter().cloned() {
170 match tt {
171 tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => {
172 args.push(current);
173 current = Vec::new();
174 }
175 _ => {
176 current.push(tt);
177 }
178 }
179 }
180 if !current.is_empty() {
181 args.push(current);
182 }
183
184 let arg_tts = args.into_iter().flat_map(|arg| {
185 quote! { &(##arg), }
186 }.token_trees).collect::<Vec<_>>();
187
188 let expanded = quote! {
189 { { (##arg_tts); } }
190 };
191 Ok(expanded)
192}
193
154fn file_expand( 194fn file_expand(
155 _db: &dyn AstDatabase, 195 _db: &dyn AstDatabase,
156 _id: LazyMacroId, 196 _id: LazyMacroId,
@@ -494,6 +534,22 @@ mod tests {
494 } 534 }
495 535
496 #[test] 536 #[test]
537 fn test_assert_expand() {
538 let expanded = expand_builtin_macro(
539 r#"
540 #[rustc_builtin_macro]
541 macro_rules! assert {
542 ($fmt:expr) => ({ /* compiler built-in */ });
543 ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
544 }
545 assert!(true, "{} {:?}", arg1(a, b, c), arg2);
546 "#,
547 );
548
549 assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
550 }
551
552 #[test]
497 fn test_compile_error_expand() { 553 fn test_compile_error_expand() {
498 let expanded = expand_builtin_macro( 554 let expanded = expand_builtin_macro(
499 r#" 555 r#"
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index 6d201256f..25cc1e9fc 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -172,6 +172,7 @@ pub mod known {
172 column, 172 column,
173 compile_error, 173 compile_error,
174 line, 174 line,
175 assert,
175 stringify, 176 stringify,
176 concat, 177 concat,
177 include, 178 include,
diff --git a/crates/ra_hir_expand/src/quote.rs b/crates/ra_hir_expand/src/quote.rs
index 57e7eebf9..3fd4233da 100644
--- a/crates/ra_hir_expand/src/quote.rs
+++ b/crates/ra_hir_expand/src/quote.rs
@@ -99,6 +99,7 @@ macro_rules! __quote {
99 ( & ) => {$crate::__quote!(@PUNCT '&')}; 99 ( & ) => {$crate::__quote!(@PUNCT '&')};
100 ( , ) => {$crate::__quote!(@PUNCT ',')}; 100 ( , ) => {$crate::__quote!(@PUNCT ',')};
101 ( : ) => {$crate::__quote!(@PUNCT ':')}; 101 ( : ) => {$crate::__quote!(@PUNCT ':')};
102 ( ; ) => {$crate::__quote!(@PUNCT ';')};
102 ( :: ) => {$crate::__quote!(@PUNCT ':', ':')}; 103 ( :: ) => {$crate::__quote!(@PUNCT ':', ':')};
103 ( . ) => {$crate::__quote!(@PUNCT '.')}; 104 ( . ) => {$crate::__quote!(@PUNCT '.')};
104 ( < ) => {$crate::__quote!(@PUNCT '<')}; 105 ( < ) => {$crate::__quote!(@PUNCT '<')};
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 25e038a55..89640efb6 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -838,14 +838,10 @@ fn func(foo: i32) { if true { <|>foo; }; }
838 r#" 838 r#"
839 //- /lib.rs 839 //- /lib.rs
840 #[rustc_builtin_macro] 840 #[rustc_builtin_macro]
841 macro_rules! assert { 841 macro_rules! format {}
842 ($cond:expr) => {{ /* compiler built-in */ }};
843 ($cond:expr,) => {{ /* compiler built-in */ }};
844 ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};
845 }
846 842
847 fn foo() { 843 fn foo() {
848 assert!("hel<|>lo"); 844 format!("hel<|>lo {}", 0);
849 } 845 }
850 "#, 846 "#,
851 ); 847 );