Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir_expand/src/builtin_macro.rs')
| -rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 297497ac1b..e148118348 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -180,22 +180,29 @@ fn assert_expand( _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { - // 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 comma = tt::Subtree { + delimiter: None, + token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { + char: ',', + spacing: tt::Spacing::Alone, + id: tt::TokenId::unspecified(), + }))], + }; + let cond = cond.clone(); + let panic_args = itertools::Itertools::intersperse(panic_args.iter().cloned(), comma); + quote! {{ + if !#cond { + #krate::panic!(##panic_args); + } + }} + } + [] => quote! {{}}, }; + ExpandResult::ok(expanded) } @@ -731,7 +738,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);}}"#]], ); } |