From 91f28e43a2686d0ac06f73b5fed11351aad428a9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 8 Dec 2019 09:26:17 +0100 Subject: [PATCH] Fix expansion of format_args --- crates/ra_hir_expand/src/builtin_macro.rs | 19 +++++++++++++------ crates/ra_ide/src/goto_definition.rs | 1 - 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index c7071fe85a..ec5ace757c 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -208,15 +208,20 @@ fn format_args_expand( _id: MacroCallId, tt: &tt::Subtree, ) -> Result { - // We expand `format_args!("", arg1, arg2)` to - // `std::fmt::Arguments::new_v1(&[], &[&arg1, &arg2])`, + // We expand `format_args!("", a1, a2)` to + // ``` + // std::fmt::Arguments::new_v1(&[], &[ + // std::fmt::ArgumentV1::new(&arg1,std::fmt::Display::fmt), + // std::fmt::ArgumentV1::new(&arg2,std::fmt::Display::fmt), + // ]) + // ```, // which is still not really correct, but close enough for now 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(tt::Subtree { delimiter: tt::Delimiter::None, token_trees: current }); + args.push(current); current = Vec::new(); } _ => { @@ -225,13 +230,15 @@ fn format_args_expand( } } if !current.is_empty() { - args.push(tt::Subtree { delimiter: tt::Delimiter::None, token_trees: current }); + args.push(current); } if args.is_empty() { return Err(mbe::ExpandError::NoMatchingRule); } let _format_string = args.remove(0); - let arg_tts = args.into_iter().flat_map(|arg| (quote! { & #arg , }).token_trees); + let arg_tts = args.into_iter().flat_map(|arg| { + quote! { std::fmt::ArgumentV1::new(&(##arg), std::fmt::Display::fmt), } + }.token_trees).collect::>(); let expanded = quote! { std::fmt::Arguments::new_v1(&[], &[##arg_tts]) }; @@ -360,6 +367,6 @@ mod tests { BuiltinFnLikeExpander::FormatArgs, ); - assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[&arg1(a,b,c),&arg2,])"#); + assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#); } } diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index 1b968134d6..077b0beda6 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -693,7 +693,6 @@ mod tests { ); } - #[should_panic] // currently failing because of expr mapping problems #[test] fn goto_through_format() { check_goto(