Fix expansion of format_args

This commit is contained in:
Florian Diebold 2019-12-08 09:26:17 +01:00
parent 596e6db46c
commit 91f28e43a2
2 changed files with 13 additions and 7 deletions

View file

@ -208,15 +208,20 @@ fn format_args_expand(
_id: MacroCallId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// 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::<Vec<_>>();
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),])"#);
}
}

View file

@ -693,7 +693,6 @@ mod tests {
);
}
#[should_panic] // currently failing because of expr mapping problems
#[test]
fn goto_through_format() {
check_goto(