From ee8c67887067657371b8d0b4b971bfe6d9db9313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Mon, 25 Jan 2021 12:53:44 +0200 Subject: Unquote strings and handle boolean literals in concat! --- crates/hir_expand/src/builtin_macro.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 80b60d59f..2806842cd 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -327,17 +327,12 @@ fn concat_expand( // 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, - }); + let component = unquote_str(&it).unwrap_or_else(|| it.text.to_string()); + text.push_str(&component); + } + // handle boolean literals + tt::TokenTree::Leaf(tt::Leaf::Ident(id)) if i % 2 == 0 => { + text.push_str(id.text.as_str()); } tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (), _ => { @@ -345,7 +340,6 @@ fn concat_expand( } } } - ExpandResult { value: Some((quote!(#text), FragmentKind::Expr)), err } } @@ -745,12 +739,10 @@ mod tests { r##" #[rustc_builtin_macro] macro_rules! concat {} - concat!("foo", 0, r#"bar"#); + concat!("foo", r, 0, r#"bar"#, false); "##, ); - assert_eq!(expanded, r#""foo0bar""#); - - // FIXME: `true`/`false` literals don't work. + assert_eq!(expanded, r#""foor0barfalse""#); } } -- cgit v1.2.3