From 759bcea96de448a7758276a9ce5696a56b44b465 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 11 Mar 2020 23:08:12 +0800 Subject: Implement dummy assert macro --- crates/ra_hir_expand/src/builtin_macro.rs | 56 +++++++++++++++++++++++++++++++ crates/ra_hir_expand/src/name.rs | 1 + crates/ra_hir_expand/src/quote.rs | 1 + crates/ra_ide/src/hover.rs | 8 ++--- 4 files changed, 60 insertions(+), 6 deletions(-) (limited to 'crates') 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! { (compile_error, CompileError) => compile_error_expand, (file, File) => file_expand, (line, Line) => line_expand, + (assert, Assert) => assert_expand, (stringify, Stringify) => stringify_expand, (format_args, FormatArgs) => format_args_expand, // format_args_nl only differs in that it adds a newline in the end, @@ -151,6 +152,45 @@ fn column_expand( Ok(expanded) } +fn assert_expand( + _db: &dyn AstDatabase, + _id: LazyMacroId, + tt: &tt::Subtree, +) -> Result { + // A hacky implementation for goto def and hover + // We expand `assert!("", arg1, arg2)` to + // ``` + // {(&(arg1), &(arg2));} + // ```, + // which is wrong but useful. + + let mut args = Vec::new(); + let mut current = Vec::new(); + for tt in tt.token_trees.iter().cloned() { + match tt { + tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => { + args.push(current); + current = Vec::new(); + } + _ => { + current.push(tt); + } + } + } + if !current.is_empty() { + args.push(current); + } + + let arg_tts = args.into_iter().flat_map(|arg| { + quote! { &(##arg), } + }.token_trees).collect::>(); + + let expanded = quote! { + { { (##arg_tts); } } + }; + Ok(expanded) +} + fn file_expand( _db: &dyn AstDatabase, _id: LazyMacroId, @@ -493,6 +533,22 @@ mod tests { assert_eq!(expanded, "\"\""); } + #[test] + fn test_assert_expand() { + let expanded = expand_builtin_macro( + r#" + #[rustc_builtin_macro] + macro_rules! assert { + ($fmt:expr) => ({ /* compiler built-in */ }); + ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) + } + assert!(true, "{} {:?}", arg1(a, b, c), arg2); + "#, + ); + + assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}"); + } + #[test] fn test_compile_error_expand() { let expanded = expand_builtin_macro( 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 { column, compile_error, line, + assert, stringify, concat, 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 { ( & ) => {$crate::__quote!(@PUNCT '&')}; ( , ) => {$crate::__quote!(@PUNCT ',')}; ( : ) => {$crate::__quote!(@PUNCT ':')}; + ( ; ) => {$crate::__quote!(@PUNCT ';')}; ( :: ) => {$crate::__quote!(@PUNCT ':', ':')}; ( . ) => {$crate::__quote!(@PUNCT '.')}; ( < ) => {$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; }; } r#" //- /lib.rs #[rustc_builtin_macro] - macro_rules! assert { - ($cond:expr) => {{ /* compiler built-in */ }}; - ($cond:expr,) => {{ /* compiler built-in */ }}; - ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }}; - } + macro_rules! format {} fn foo() { - assert!("hel<|>lo"); + format!("hel<|>lo {}", 0); } "#, ); -- cgit v1.2.3 From 8c159b54e9617de4248f947db8cbe0272b640915 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 11 Mar 2020 23:14:15 +0800 Subject: Add test on hover --- crates/ra_ide/src/hover.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'crates') diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 89640efb6..0bbba4855 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -832,6 +832,25 @@ fn func(foo: i32) { if true { <|>foo; }; } assert_eq!(hover_on, "\"Tracks\""); } + #[test] + fn test_hover_through_assert_macro() { + let hover_on = check_hover_result( + r#" + //- /lib.rs + #[rustc_builtin_macro] + macro_rules! assert {} + + fn bar() -> bool { true } + fn foo() { + assert!(ba<|>r()); + } + "#, + &["fn bar() -> bool"], + ); + + assert_eq!(hover_on, "bar"); + } + #[test] fn test_hover_through_literal_string_in_builtin_macro() { check_hover_no_result( -- cgit v1.2.3 From 8f7703b006bb35ab08959b01ed8ffb27d07f4d0b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 12 Mar 2020 01:57:34 +0800 Subject: Update comment Co-Authored-By: bjorn3 --- crates/ra_hir_expand/src/builtin_macro.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 01f78fc77..f9d3787f6 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -158,9 +158,9 @@ fn assert_expand( tt: &tt::Subtree, ) -> Result { // A hacky implementation for goto def and hover - // We expand `assert!("", arg1, arg2)` to + // We expand `assert!(cond, arg1, arg2)` to // ``` - // {(&(arg1), &(arg2));} + // {(cond, &(arg1), &(arg2));} // ```, // which is wrong but useful. @@ -539,8 +539,8 @@ mod tests { r#" #[rustc_builtin_macro] macro_rules! assert { - ($fmt:expr) => ({ /* compiler built-in */ }); - ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ }) + ($cond:expr) => ({ /* compiler built-in */ }); + ($cond:expr, $($args:tt)*) => ({ /* compiler built-in */ }) } assert!(true, "{} {:?}", arg1(a, b, c), arg2); "#, -- cgit v1.2.3