Skip to content

Fix concat! with integer literals #6768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions crates/hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,34 @@ fn concat_expand(
_arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> {
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(
Expand Down Expand Up @@ -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.
}
}