diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 297497ac1b..eeee66cbd9 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -180,22 +180,28 @@ fn assert_expand( _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { - // A hacky implementation for goto def and hover - // We expand `assert!(cond, arg1, arg2)` to - // ``` - // {(cond, &(arg1), &(arg2));} - // ```, - // which is wrong but useful. - + let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() }; let args = parse_exprs_with_sep(tt, ','); - - let arg_tts = args.into_iter().flat_map(|arg| { - quote! { &(#arg), } - }.token_trees); - - let expanded = quote! { - { { (##arg_tts); } } + let expanded = match &*args { + [cond, panic_args @ ..] => { + let cond = cond.clone(); + let panic_args = panic_args.iter().cloned().intersperse(tt::Subtree { + delimiter: None, + token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { + char: ',', + spacing: tt::Spacing::Alone, + id: tt::TokenId::unspecified(), + }))], + }); + quote! {{ + if !#cond { + #krate::panic!(##panic_args); + } + }} + } + [] => quote! {{}}, }; + ExpandResult::ok(expanded) } @@ -731,7 +737,7 @@ mod tests { } assert!(true, "{} {:?}", arg1(a, b, c), arg2); "#, - expect![["{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}"]], + expect![[r#"{if!true{$crate::panic!("{} {:?}",arg1(a,b,c),arg2);}}"#]], ); } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index d0ad7945a7..2992755ded 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -74,7 +74,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } #[rustc_builtin_macro(std_panic)] +#[macro_export] macro_rules! panic {} +#[rustc_builtin_macro] +macro_rules! assert {} fn main() { // from https://doc.rust-lang.org/std/fmt/index.html @@ -127,4 +130,6 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd println!("{:x?} {} ", thingy, n2); panic!("{}", 0); panic!("more {}", 1); + assert!(true, "{}", 1); + assert!(true, "{} asdasd", 1); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 91d29a96bf..d5018f2327 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -469,7 +469,10 @@ mod panic { } #[rustc_builtin_macro(std_panic)] +#[macro_export] macro_rules! panic {} +#[rustc_builtin_macro] +macro_rules! assert {} fn main() { // from https://doc.rust-lang.org/std/fmt/index.html @@ -522,6 +525,8 @@ fn main() { println!("{:x?} {} ", thingy, n2); panic!("{}", 0); panic!("more {}", 1); + assert!(true, "{}", 1); + assert!(true, "{} asdasd", 1); }"# .trim(), expect_file!["./test_data/highlight_strings.html"],