Implement concat_idents

This commit is contained in:
Edwin Cheng 2021-05-14 06:42:10 +08:00
parent 9a431c26f4
commit bbc151ef32
2 changed files with 36 additions and 0 deletions

View File

@ -118,6 +118,7 @@ register_builtin! {
EAGER:
(compile_error, CompileError) => compile_error_expand,
(concat, Concat) => concat_expand,
(concat_idents, ConcatIdents) => concat_idents_expand,
(include, Include) => include_expand,
(include_bytes, IncludeBytes) => include_bytes_expand,
(include_str, IncludeStr) => include_str_expand,
@ -373,6 +374,28 @@ fn concat_expand(
ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err }
}
fn concat_idents_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> ExpandResult<Option<ExpandedEager>> {
let mut err = None;
let mut ident = String::new();
for (i, t) in tt.token_trees.iter().enumerate() {
match t {
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => {
ident.push_str(id.text.as_str());
}
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (),
_ => {
err.get_or_insert(mbe::ExpandError::UnexpectedToken);
}
}
}
let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() };
ExpandResult { value: Some(ExpandedEager::new(quote!(#ident), FragmentKind::Expr)), err }
}
fn relative_file(
db: &dyn AstDatabase,
call_id: MacroCallId,
@ -794,4 +817,16 @@ mod tests {
expect![[r#""foor0bar\nfalse""#]],
);
}
#[test]
fn test_concat_idents_expand() {
check_expansion(
r##"
#[rustc_builtin_macro]
macro_rules! concat_idents {}
concat_idents!(foo, bar);
"##,
expect![[r#"foobar"#]],
);
}
}

View File

@ -212,6 +212,7 @@ pub mod known {
std_panic,
stringify,
concat,
concat_idents,
include,
include_bytes,
include_str,