mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Implement concat_idents
This commit is contained in:
parent
9a431c26f4
commit
bbc151ef32
@ -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"#]],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -212,6 +212,7 @@ pub mod known {
|
||||
std_panic,
|
||||
stringify,
|
||||
concat,
|
||||
concat_idents,
|
||||
include,
|
||||
include_bytes,
|
||||
include_str,
|
||||
|
Loading…
Reference in New Issue
Block a user