From bb28aef9184c98450b81ec7faca99a5c327619ea Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 8 Dec 2020 20:06:41 +0100 Subject: Fix `concat!` with integer literals --- crates/hir_expand/src/builtin_macro.rs | 42 +++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'crates/hir_expand') diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 44a5556b6..79b970850 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -287,23 +287,34 @@ fn concat_expand( _arg_id: EagerMacroId, tt: &tt::Subtree, ) -> ExpandResult> { + let mut err = None; let mut text = String::new(); for (i, t) in tt.token_trees.iter().enumerate() { match t { tt::TokenTree::Leaf(tt::Leaf::Literal(it)) if i % 2 == 0 => { - text += &match unquote_str(&it) { - Some(s) => s, - None => { - return ExpandResult::only_err(mbe::ExpandError::ConversionError); - } - }; + // concat works with string and char literals, so remove any quotes. + // It also works with integer, float and boolean literals, so just use the rest + // as-is. + + text += it + .text + .trim_start_matches(|c| match c { + 'r' | '#' | '\'' | '"' => true, + _ => false, + }) + .trim_end_matches(|c| match c { + '#' | '\'' | '"' => true, + _ => false, + }); } tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (), - _ => return ExpandResult::only_err(mbe::ExpandError::UnexpectedToken), + _ => { + err.get_or_insert(mbe::ExpandError::UnexpectedToken); + } } } - ExpandResult::ok(Some((quote!(#text), FragmentKind::Expr))) + ExpandResult { value: Some((quote!(#text), FragmentKind::Expr)), err } } fn relative_file( @@ -686,4 +697,19 @@ mod tests { assert_eq!(expanded, r#"b"""#); } + + #[test] + fn test_concat_expand() { + let expanded = expand_builtin_macro( + r##" + #[rustc_builtin_macro] + macro_rules! concat {} + concat!("foo", 0, r#"bar"#); + "##, + ); + + assert_eq!(expanded, r#""foo0bar""#); + + // FIXME: `true`/`false` literals don't work. + } } -- cgit v1.2.3