aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_expand/src/builtin_macro.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_expand/src/builtin_macro.rs')
-rw-r--r--crates/hir_expand/src/builtin_macro.rs40
1 files changed, 38 insertions, 2 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs
index 179de61f9..280c25f11 100644
--- a/crates/hir_expand/src/builtin_macro.rs
+++ b/crates/hir_expand/src/builtin_macro.rs
@@ -118,6 +118,7 @@ register_builtin! {
118 EAGER: 118 EAGER:
119 (compile_error, CompileError) => compile_error_expand, 119 (compile_error, CompileError) => compile_error_expand,
120 (concat, Concat) => concat_expand, 120 (concat, Concat) => concat_expand,
121 (concat_idents, ConcatIdents) => concat_idents_expand,
121 (include, Include) => include_expand, 122 (include, Include) => include_expand,
122 (include_bytes, IncludeBytes) => include_bytes_expand, 123 (include_bytes, IncludeBytes) => include_bytes_expand,
123 (include_str, IncludeStr) => include_str_expand, 124 (include_str, IncludeStr) => include_str_expand,
@@ -373,6 +374,28 @@ fn concat_expand(
373 ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err } 374 ExpandResult { value: Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr)), err }
374} 375}
375 376
377fn 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
376fn relative_file( 399fn relative_file(
377 db: &dyn AstDatabase, 400 db: &dyn AstDatabase,
378 call_id: MacroCallId, 401 call_id: MacroCallId,
@@ -578,6 +601,7 @@ mod tests {
578 krate, 601 krate,
579 kind: MacroCallKind::FnLike { 602 kind: MacroCallKind::FnLike {
580 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)), 603 ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
604 fragment: FragmentKind::Expr,
581 }, 605 },
582 }; 606 };
583 607
@@ -788,9 +812,21 @@ mod tests {
788 r##" 812 r##"
789 #[rustc_builtin_macro] 813 #[rustc_builtin_macro]
790 macro_rules! concat {} 814 macro_rules! concat {}
791 concat!("foo", "r", 0, r#"bar"#, false); 815 concat!("foo", "r", 0, r#"bar"#, "\n", false);
816 "##,
817 expect![[r#""foor0bar\nfalse""#]],
818 );
819 }
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);
792 "##, 828 "##,
793 expect![[r#""foor0barfalse""#]], 829 expect![[r#"foobar"#]],
794 ); 830 );
795 } 831 }
796} 832}