Skip to content

Commit bbc151e

Browse files
committed
Implement concat_idents
1 parent 9a431c2 commit bbc151e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

crates/hir_expand/src/builtin_macro.rs

+35
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ register_builtin! {
118118
EAGER:
119119
(compile_error, CompileError) => compile_error_expand,
120120
(concat, Concat) => concat_expand,
121+
(concat_idents, ConcatIdents) => concat_idents_expand,
121122
(include, Include) => include_expand,
122123
(include_bytes, IncludeBytes) => include_bytes_expand,
123124
(include_str, IncludeStr) => include_str_expand,
@@ -373,6 +374,28 @@ fn concat_expand(
373374
ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err }
374375
}
375376

377+
fn concat_idents_expand(
378+
_db: &dyn AstDatabase,
379+
_arg_id: EagerMacroId,
380+
tt: &tt::Subtree,
381+
) -> ExpandResult<Option<ExpandedEager>> {
382+
let mut err = None;
383+
let mut ident = String::new();
384+
for (i, t) in tt.token_trees.iter().enumerate() {
385+
match t {
386+
tt::TokenTree::Leaf(tt::Leaf::Ident(id)) => {
387+
ident.push_str(id.text.as_str());
388+
}
389+
tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if i % 2 == 1 && punct.char == ',' => (),
390+
_ => {
391+
err.get_or_insert(mbe::ExpandError::UnexpectedToken);
392+
}
393+
}
394+
}
395+
let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() };
396+
ExpandResult { value: Some(ExpandedEager::new(quote!(#ident), FragmentKind::Expr)), err }
397+
}
398+
376399
fn relative_file(
377400
db: &dyn AstDatabase,
378401
call_id: MacroCallId,
@@ -794,4 +817,16 @@ mod tests {
794817
expect![[r#""foor0bar\nfalse""#]],
795818
);
796819
}
820+
821+
#[test]
822+
fn test_concat_idents_expand() {
823+
check_expansion(
824+
r##"
825+
#[rustc_builtin_macro]
826+
macro_rules! concat_idents {}
827+
concat_idents!(foo, bar);
828+
"##,
829+
expect![[r#"foobar"#]],
830+
);
831+
}
797832
}

crates/hir_expand/src/name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub mod known {
212212
std_panic,
213213
stringify,
214214
concat,
215+
concat_idents,
215216
include,
216217
include_bytes,
217218
include_str,

0 commit comments

Comments
 (0)