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 { - // 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);}}"#]], ); } diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index 5bbadb0f45..ce24311115 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -31,14 +31,16 @@ fn is_format_string(string: &ast::String) -> Option<()> { let parent = string.syntax().parent()?; let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?; - if !matches!(name.text().as_str(), "format_args" | "format_args_nl") { + if !matches!( + name.text().as_str(), + "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021" + ) { return None; } let first_literal = parent .children_with_tokens() - .filter_map(|it| it.as_token().cloned().and_then(ast::String::cast)) - .next()?; + .find_map(|it| it.as_token().cloned().and_then(ast::String::cast))?; if &first_literal != string { return None; } 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 9c20c8ae4b..2992755ded 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -51,6 +51,34 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; } +mod panic { + pub macro panic_2015 { + () => ( + $crate::panicking::panic("explicit panic") + ), + ($msg:literal $(,)?) => ( + $crate::panicking::panic($msg) + ), + // Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint. + ($msg:expr $(,)?) => ( + $crate::panicking::panic_str($msg) + ), + // Special-case the single-argument case for const_panic. + ("{}", $arg:expr $(,)?) => ( + $crate::panicking::panic_display(&$arg) + ), + ($fmt:expr, $($arg:tt)+) => ( + $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+)) + ), + } +} + +#[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 println!("Hello"); // => "Hello" @@ -100,4 +128,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd println!("{ничоси}", ничоси = 92); 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 9fc730e007..d5018f2327 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -446,6 +446,34 @@ macro_rules! format_args_nl { ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; } +mod panic { + pub macro panic_2015 { + () => ( + $crate::panicking::panic("explicit panic") + ), + ($msg:literal $(,)?) => ( + $crate::panicking::panic($msg) + ), + // Use `panic_str` instead of `panic_display::<&str>` for non_fmt_panic lint. + ($msg:expr $(,)?) => ( + $crate::panicking::panic_str($msg) + ), + // Special-case the single-argument case for const_panic. + ("{}", $arg:expr $(,)?) => ( + $crate::panicking::panic_display(&$arg) + ), + ($fmt:expr, $($arg:tt)+) => ( + $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+)) + ), + } +} + +#[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 println!("Hello"); // => "Hello" @@ -495,6 +523,10 @@ fn main() { println!("{ничоси}", ничоси = 92); println!("{:x?} {} ", thingy, n2); + panic!("{}", 0); + panic!("more {}", 1); + assert!(true, "{}", 1); + assert!(true, "{} asdasd", 1); }"# .trim(), expect_file!["./test_data/highlight_strings.html"],