ide/
expand_macro.rs

1use hir::db::ExpandDatabase;
2use hir::{ExpandResult, InFile, Semantics};
3use ide_db::{
4    FileId, RootDatabase, base_db::Crate, helpers::pick_best_token,
5    syntax_helpers::prettify_macro_expansion,
6};
7use span::{SpanMap, TextRange, TextSize};
8use stdx::format_to;
9use syntax::{AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T, ast, ted};
10
11use crate::FilePosition;
12
13pub struct ExpandedMacro {
14    pub name: String,
15    pub expansion: String,
16}
17
18// Feature: Expand Macro Recursively
19//
20// Shows the full macro expansion of the macro at the current caret position.
21//
22// | Editor  | Action Name |
23// |---------|-------------|
24// | VS Code | **rust-analyzer: Expand macro recursively at caret** |
25//
26// ![Expand Macro Recursively](https://user-images.githubusercontent.com/48062697/113020648-b3973180-917a-11eb-84a9-ecb921293dc5.gif)
27pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<ExpandedMacro> {
28    let sema = Semantics::new(db);
29    let file_id = sema.attach_first_edition(position.file_id);
30    let file = sema.parse(file_id);
31    let krate = sema.file_to_module_def(file_id.file_id(db))?.krate(db).into();
32
33    let tok = pick_best_token(file.syntax().token_at_offset(position.offset), |kind| match kind {
34        SyntaxKind::IDENT => 1,
35        _ => 0,
36    })?;
37
38    // due to how rust-analyzer works internally, we need to special case derive attributes,
39    // otherwise they might not get found, e.g. here with the cursor at $0 `#[attr]` would expand:
40    // ```
41    // #[attr]
42    // #[derive($0Foo)]
43    // struct Bar;
44    // ```
45
46    let derive = sema.descend_into_macros_exact(tok.clone()).into_iter().find_map(|descended| {
47        let macro_file = sema.hir_file_for(&descended.parent()?).macro_file()?;
48        if !macro_file.is_derive_attr_pseudo_expansion(db) {
49            return None;
50        }
51
52        let name = descended.parent_ancestors().filter_map(ast::Path::cast).last()?.to_string();
53        // up map out of the #[derive] expansion
54        let InFile { file_id, value: tokens } =
55            hir::InMacroFile::new(macro_file, descended).upmap_once(db);
56        let token = sema.parse_or_expand(file_id).covering_element(tokens[0]).into_token()?;
57        let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
58        let expansions = sema.expand_derive_macro(&attr)?;
59        let idx = attr
60            .token_tree()?
61            .token_trees_and_tokens()
62            .filter_map(NodeOrToken::into_token)
63            .take_while(|it| it != &token)
64            .filter(|it| it.kind() == T![,])
65            .count();
66        let ExpandResult { err, value: expansion } = expansions.get(idx)?.clone()?;
67        let expansion_file_id = sema.hir_file_for(&expansion).macro_file()?;
68        let expansion_span_map = db.expansion_span_map(expansion_file_id);
69        let mut expansion = format(
70            db,
71            SyntaxKind::MACRO_ITEMS,
72            position.file_id,
73            expansion,
74            &expansion_span_map,
75            krate,
76        );
77        if let Some(err) = err {
78            expansion.insert_str(
79                0,
80                &format!("Expansion had errors: {}\n\n", err.render_to_string(sema.db)),
81            );
82        }
83        Some(ExpandedMacro { name, expansion })
84    });
85
86    if derive.is_some() {
87        return derive;
88    }
89
90    let syntax_token = sema.descend_into_macros_exact(tok);
91    'tokens: for syntax_token in syntax_token {
92        let mut anc = syntax_token.parent_ancestors();
93        let mut span_map = SpanMap::empty();
94        let mut error = String::new();
95        let (name, expanded, kind) = loop {
96            let Some(node) = anc.next() else {
97                continue 'tokens;
98            };
99
100            if let Some(item) = ast::Item::cast(node.clone())
101                && let Some(def) = sema.resolve_attr_macro_call(&item)
102            {
103                break (
104                    def.name(db).display(db, file_id.edition(db)).to_string(),
105                    expand_macro_recur(&sema, &item, &mut error, &mut span_map, TextSize::new(0))?,
106                    SyntaxKind::MACRO_ITEMS,
107                );
108            }
109            if let Some(mac) = ast::MacroCall::cast(node) {
110                let mut name = mac.path()?.segment()?.name_ref()?.to_string();
111                name.push('!');
112                let syntax_kind =
113                    mac.syntax().parent().map(|it| it.kind()).unwrap_or(SyntaxKind::MACRO_ITEMS);
114                break (
115                    name,
116                    expand_macro_recur(
117                        &sema,
118                        &ast::Item::MacroCall(mac),
119                        &mut error,
120                        &mut span_map,
121                        TextSize::new(0),
122                    )?,
123                    syntax_kind,
124                );
125            }
126        };
127
128        // FIXME:
129        // macro expansion may lose all white space information
130        // But we hope someday we can use ra_fmt for that
131        let mut expansion = format(db, kind, position.file_id, expanded, &span_map, krate);
132
133        if !error.is_empty() {
134            expansion.insert_str(0, &format!("Expansion had errors:{error}\n\n"));
135        }
136        return Some(ExpandedMacro { name, expansion });
137    }
138    None
139}
140
141fn expand_macro_recur(
142    sema: &Semantics<'_, RootDatabase>,
143    macro_call: &ast::Item,
144    error: &mut String,
145    result_span_map: &mut SpanMap,
146    offset_in_original_node: TextSize,
147) -> Option<SyntaxNode> {
148    let ExpandResult { value: expanded, err } = match macro_call {
149        item @ ast::Item::MacroCall(macro_call) => sema
150            .expand_attr_macro(item)
151            .map(|it| it.map(|it| it.value))
152            .or_else(|| sema.expand_allowed_builtins(macro_call))?,
153        item => sema.expand_attr_macro(item)?.map(|it| it.value),
154    };
155    let expanded = expanded.clone_for_update();
156    if let Some(err) = err {
157        format_to!(error, "\n{}", err.render_to_string(sema.db));
158    }
159    let file_id =
160        sema.hir_file_for(&expanded).macro_file().expect("expansion must produce a macro file");
161    let expansion_span_map = sema.db.expansion_span_map(file_id);
162    result_span_map.merge(
163        TextRange::at(offset_in_original_node, macro_call.syntax().text_range().len()),
164        expanded.text_range().len(),
165        &expansion_span_map,
166    );
167    Some(expand(sema, expanded, error, result_span_map, u32::from(offset_in_original_node) as i32))
168}
169
170fn expand(
171    sema: &Semantics<'_, RootDatabase>,
172    expanded: SyntaxNode,
173    error: &mut String,
174    result_span_map: &mut SpanMap,
175    mut offset_in_original_node: i32,
176) -> SyntaxNode {
177    let children = expanded.descendants().filter_map(ast::Item::cast);
178    let mut replacements = Vec::new();
179
180    for child in children {
181        if let Some(new_node) = expand_macro_recur(
182            sema,
183            &child,
184            error,
185            result_span_map,
186            TextSize::new(
187                (offset_in_original_node + (u32::from(child.syntax().text_range().start()) as i32))
188                    as u32,
189            ),
190        ) {
191            offset_in_original_node = offset_in_original_node
192                + (u32::from(new_node.text_range().len()) as i32)
193                - (u32::from(child.syntax().text_range().len()) as i32);
194            // check if the whole original syntax is replaced
195            if expanded == *child.syntax() {
196                return new_node;
197            }
198            replacements.push((child, new_node));
199        }
200    }
201
202    replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
203    expanded
204}
205
206fn format(
207    db: &RootDatabase,
208    kind: SyntaxKind,
209    file_id: FileId,
210    expanded: SyntaxNode,
211    span_map: &SpanMap,
212    krate: Crate,
213) -> String {
214    let expansion = prettify_macro_expansion(db, expanded, span_map, krate).to_string();
215
216    _format(db, kind, file_id, &expansion).unwrap_or(expansion)
217}
218
219#[cfg(any(test, target_arch = "wasm32", target_os = "emscripten"))]
220fn _format(
221    _db: &RootDatabase,
222    _kind: SyntaxKind,
223    _file_id: FileId,
224    expansion: &str,
225) -> Option<String> {
226    // remove trailing spaces for test
227    use itertools::Itertools;
228    Some(expansion.lines().map(|x| x.trim_end()).join("\n"))
229}
230
231#[cfg(not(any(test, target_arch = "wasm32", target_os = "emscripten")))]
232fn _format(
233    db: &RootDatabase,
234    kind: SyntaxKind,
235    file_id: FileId,
236    expansion: &str,
237) -> Option<String> {
238    use ide_db::base_db::RootQueryDb;
239
240    // hack until we get hygiene working (same character amount to preserve formatting as much as possible)
241    const DOLLAR_CRATE_REPLACE: &str = "__r_a_";
242    const BUILTIN_REPLACE: &str = "builtin__POUND";
243    let expansion =
244        expansion.replace("$crate", DOLLAR_CRATE_REPLACE).replace("builtin #", BUILTIN_REPLACE);
245    let (prefix, suffix) = match kind {
246        SyntaxKind::MACRO_PAT => ("fn __(", ": u32);"),
247        SyntaxKind::MACRO_EXPR | SyntaxKind::MACRO_STMTS => ("fn __() {", "}"),
248        SyntaxKind::MACRO_TYPE => ("type __ =", ";"),
249        _ => ("", ""),
250    };
251    let expansion = format!("{prefix}{expansion}{suffix}");
252
253    let &crate_id = db.relevant_crates(file_id).iter().next()?;
254    let edition = crate_id.data(db).edition;
255
256    #[allow(clippy::disallowed_methods)]
257    let mut cmd = std::process::Command::new(toolchain::Tool::Rustfmt.path());
258    cmd.arg("--edition");
259    cmd.arg(edition.to_string());
260
261    let mut rustfmt = cmd
262        .stdin(std::process::Stdio::piped())
263        .stdout(std::process::Stdio::piped())
264        .stderr(std::process::Stdio::piped())
265        .spawn()
266        .ok()?;
267
268    std::io::Write::write_all(&mut rustfmt.stdin.as_mut()?, expansion.as_bytes()).ok()?;
269
270    let output = rustfmt.wait_with_output().ok()?;
271    let captured_stdout = String::from_utf8(output.stdout).ok()?;
272
273    if output.status.success() && !captured_stdout.trim().is_empty() {
274        let output = captured_stdout
275            .replace(DOLLAR_CRATE_REPLACE, "$crate")
276            .replace(BUILTIN_REPLACE, "builtin #");
277        let output = output.trim().strip_prefix(prefix)?;
278        let output = match kind {
279            SyntaxKind::MACRO_PAT => {
280                output.strip_suffix(suffix).or_else(|| output.strip_suffix(": u32,\n);"))?
281            }
282            _ => output.strip_suffix(suffix)?,
283        };
284        let trim_indent = stdx::trim_indent(output);
285        tracing::debug!("expand_macro: formatting succeeded");
286        Some(trim_indent)
287    } else {
288        None
289    }
290}
291
292#[cfg(test)]
293mod tests {
294    use expect_test::{Expect, expect};
295
296    use crate::fixture;
297
298    #[track_caller]
299    fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
300        let (analysis, pos) = fixture::position(ra_fixture);
301        let expansion = analysis.expand_macro(pos).unwrap().unwrap();
302        let actual = format!("{}\n{}", expansion.name, expansion.expansion);
303        expect.assert_eq(&actual);
304    }
305
306    #[test]
307    fn expand_allowed_builtin_macro() {
308        check(
309            r#"
310//- minicore: concat
311$0concat!("test", 10, 'b', true);"#,
312            expect![[r#"
313                concat!
314                "test10btrue""#]],
315        );
316    }
317
318    #[test]
319    fn do_not_expand_disallowed_macro() {
320        let (analysis, pos) = fixture::position(
321            r#"
322//- minicore: asm
323$0asm!("0x300, x0");"#,
324        );
325        let expansion = analysis.expand_macro(pos).unwrap();
326        assert!(expansion.is_none());
327    }
328
329    #[test]
330    fn macro_expand_as_keyword() {
331        check(
332            r#"
333macro_rules! bar {
334    ($i:tt) => { $i as _ }
335}
336fn main() {
337    let x: u64 = ba$0r!(5i64);
338}
339"#,
340            expect![[r#"
341                bar!
342                5i64 as _"#]],
343        );
344    }
345
346    #[test]
347    fn macro_expand_underscore() {
348        check(
349            r#"
350macro_rules! bar {
351    ($i:tt) => { for _ in 0..$i {} }
352}
353fn main() {
354    ba$0r!(42);
355}
356"#,
357            expect![[r#"
358                bar!
359                for _ in 0..42{}"#]],
360        );
361    }
362
363    #[test]
364    fn macro_expand_recursive_expansion() {
365        check(
366            r#"
367macro_rules! bar {
368    () => { fn  b() {} }
369}
370macro_rules! foo {
371    () => { bar!(); }
372}
373macro_rules! baz {
374    () => { foo!(); }
375}
376f$0oo!();
377"#,
378            expect![[r#"
379                foo!
380                fn b(){}"#]],
381        );
382    }
383
384    #[test]
385    fn macro_expand_multiple_lines() {
386        check(
387            r#"
388macro_rules! foo {
389    () => {
390        fn some_thing() -> u32 {
391            let a = 0;
392            a + 10
393        }
394    }
395}
396f$0oo!();
397        "#,
398            expect![[r#"
399                foo!
400                fn some_thing() -> u32 {
401                    let a = 0;
402                    a+10
403                }"#]],
404        );
405    }
406
407    #[test]
408    fn macro_expand_match_ast() {
409        check(
410            r#"
411macro_rules! match_ast {
412    (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
413    (match ($node:expr) {
414        $( ast::$ast:ident($it:ident) => $res:block, )*
415        _ => $catch_all:expr $(,)?
416    }) => {{
417        $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
418        { $catch_all }
419    }};
420}
421
422fn main() {
423    mat$0ch_ast! {
424        match container {
425            ast::TraitDef(it) => {},
426            ast::ImplDef(it) => {},
427            _ => { continue },
428        }
429    }
430}
431"#,
432            expect![[r#"
433                match_ast!
434                {
435                    if let Some(it) = ast::TraitDef::cast(container.clone()){}
436                    else if let Some(it) = ast::ImplDef::cast(container.clone()){}
437                    else {
438                        {
439                            continue
440                        }
441                    }
442                }"#]],
443        );
444    }
445
446    #[test]
447    fn macro_expand_match_ast_inside_let_statement() {
448        check(
449            r#"
450macro_rules! match_ast {
451    (match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
452    (match ($node:expr) {}) => {{}};
453}
454
455fn main() {
456    let p = f(|it| {
457        let res = mat$0ch_ast! { match c {}};
458        Some(res)
459    })?;
460}
461"#,
462            expect![[r#"
463                match_ast!
464                {}"#]],
465        );
466    }
467
468    #[test]
469    fn macro_expand_inner_macro_rules() {
470        check(
471            r#"
472macro_rules! foo {
473    ($t:tt) => {{
474        macro_rules! bar {
475            () => {
476                $t
477            }
478        }
479        bar!()
480    }};
481}
482
483fn main() {
484    foo$0!(42);
485}
486            "#,
487            expect![[r#"
488                foo!
489                {
490                    macro_rules! bar {
491                        () => {
492                            42
493                        }
494                    }
495                    42
496                }"#]],
497        );
498    }
499
500    #[test]
501    fn macro_expand_inner_macro_fail_to_expand() {
502        check(
503            r#"
504macro_rules! bar {
505    (BAD) => {};
506}
507macro_rules! foo {
508    () => {bar!()};
509}
510
511fn main() {
512    let res = fo$0o!();
513}
514"#,
515            expect![[r#"
516                foo!
517                Expansion had errors:
518                expected ident: `BAD`
519
520            "#]],
521        );
522    }
523
524    #[test]
525    fn macro_expand_with_dollar_crate() {
526        check(
527            r#"
528#[macro_export]
529macro_rules! bar {
530    () => {0};
531}
532macro_rules! foo {
533    () => {$crate::bar!()};
534}
535
536fn main() {
537    let res = fo$0o!();
538}
539"#,
540            expect![[r#"
541                foo!
542                0"#]],
543        );
544    }
545
546    #[test]
547    fn macro_expand_with_dyn_absolute_path() {
548        check(
549            r#"
550macro_rules! foo {
551    () => {fn f<T>(_: &dyn ::std::marker::Copy) {}};
552}
553
554fn main() {
555    fo$0o!()
556}
557"#,
558            expect![[r#"
559                foo!
560                fn f<T>(_: &dyn ::std::marker::Copy){}"#]],
561        );
562    }
563
564    #[test]
565    fn macro_expand_item_expansion_in_expression_call() {
566        check(
567            r#"
568macro_rules! foo {
569    () => {fn f<T>() {}};
570}
571
572fn main() {
573    let res = fo$0o!();
574}
575"#,
576            expect![[r#"
577                foo!
578                fn f<T>(){}"#]],
579        );
580    }
581
582    #[test]
583    fn macro_expand_derive() {
584        check(
585            r#"
586//- proc_macros: identity, derive_identity
587//- minicore: derive
588
589#[proc_macros::identity]
590#[derive(proc_macros::DeriveIde$0ntity)]
591struct Foo {}
592"#,
593            expect![[r#"
594                proc_macros::DeriveIdentity
595                struct Foo{}"#]],
596        );
597    }
598
599    #[test]
600    fn macro_expand_derive2() {
601        check(
602            r#"
603//- proc_macros: derive_identity
604//- minicore: derive
605
606#[derive(proc_macros::$0DeriveIdentity)]
607#[derive(proc_macros::DeriveIdentity)]
608struct Foo {}
609"#,
610            expect![[r#"
611                proc_macros::DeriveIdentity
612                #[derive(proc_macros::DeriveIdentity)]
613                struct Foo{}"#]],
614        );
615    }
616
617    #[test]
618    fn macro_expand_derive_multi() {
619        check(
620            r#"
621//- proc_macros: derive_identity
622//- minicore: derive
623
624#[derive(proc_macros::DeriveIdent$0ity, proc_macros::DeriveIdentity)]
625struct Foo {}
626"#,
627            expect![[r#"
628                proc_macros::DeriveIdentity
629                struct Foo{}"#]],
630        );
631        check(
632            r#"
633//- proc_macros: derive_identity
634//- minicore: derive
635
636#[derive(proc_macros::DeriveIdentity, proc_macros::De$0riveIdentity)]
637struct Foo {}
638"#,
639            expect![[r#"
640                proc_macros::DeriveIdentity
641                struct Foo{}"#]],
642        );
643    }
644
645    #[test]
646    fn dollar_crate() {
647        check(
648            r#"
649//- /a.rs crate:a
650pub struct Foo;
651#[macro_export]
652macro_rules! m {
653    ( $i:ident ) => { $crate::Foo; $crate::Foo; $i::Foo; };
654}
655//- /b.rs crate:b deps:a
656pub struct Foo;
657#[macro_export]
658macro_rules! m {
659    () => { a::m!($crate); $crate::Foo; $crate::Foo; };
660}
661//- /c.rs crate:c deps:b,a
662pub struct Foo;
663#[macro_export]
664macro_rules! m {
665    () => { b::m!(); $crate::Foo; $crate::Foo; };
666}
667fn bar() {
668    m$0!();
669}
670"#,
671            expect![[r#"
672m!
673a::Foo;
674a::Foo;
675b::Foo;
676;
677b::Foo;
678b::Foo;
679;
680crate::Foo;
681crate::Foo;"#]],
682        );
683    }
684
685    #[test]
686    fn semi_glueing() {
687        check(
688            r#"
689macro_rules! __log_value {
690    ($key:ident :$capture:tt =) => {};
691}
692
693macro_rules! __log {
694    ($key:tt $(:$capture:tt)? $(= $value:expr)?; $($arg:tt)+) => {
695        __log_value!($key $(:$capture)* = $($value)*);
696    };
697}
698
699__log!(written:%; "Test"$0);
700    "#,
701            expect![[r#"
702                __log!
703            "#]],
704        );
705    }
706
707    #[test]
708    fn assoc_call() {
709        check(
710            r#"
711macro_rules! mac {
712    () => { fn assoc() {} }
713}
714impl () {
715    mac$0!();
716}
717    "#,
718            expect![[r#"
719                mac!
720                fn assoc(){}"#]],
721        );
722    }
723
724    #[test]
725    fn eager() {
726        check(
727            r#"
728//- minicore: concat
729macro_rules! my_concat {
730    ($head:expr, $($tail:tt)*) => { concat!($head, $($tail)*) };
731}
732
733
734fn test() {
735    _ = my_concat!(
736        conc$0at!("<", ">"),
737        "hi",
738    );
739}
740    "#,
741            expect![[r#"
742                concat!
743                "<>""#]],
744        );
745    }
746
747    #[test]
748    fn in_included() {
749        check(
750            r#"
751//- minicore: include
752//- /main.rs crate:main
753include!("./included.rs");
754//- /included.rs
755macro_rules! foo {
756    () => { fn item() {} };
757}
758foo$0!();
759"#,
760            expect![[r#"
761                foo!
762                fn item(){}"#]],
763        );
764    }
765
766    #[test]
767    fn include() {
768        check(
769            r#"
770//- minicore: include
771//- /main.rs crate:main
772include$0!("./included.rs");
773//- /included.rs
774macro_rules! foo {
775    () => { fn item() {} };
776}
777foo();
778"#,
779            expect![[r#"
780                include!
781                macro_rules! foo {
782                    () => {
783                        fn item(){}
784
785                    };
786                }
787                foo();"#]],
788        );
789    }
790
791    #[test]
792    fn works_in_sig() {
793        check(
794            r#"
795macro_rules! foo {
796    () => { u32 };
797}
798fn foo() -> foo$0!() {
799    42
800}
801"#,
802            expect![[r#"
803                foo!
804                u32"#]],
805        );
806        check(
807            r#"
808macro_rules! foo {
809    () => { u32 };
810}
811fn foo(_: foo$0!() ) {}
812"#,
813            expect![[r#"
814                foo!
815                u32"#]],
816        );
817    }
818
819    #[test]
820    fn works_in_generics() {
821        check(
822            r#"
823trait Trait {}
824macro_rules! foo {
825    () => { Trait };
826}
827impl<const C: foo$0!()> Trait for () {}
828"#,
829            expect![[r#"
830                foo!
831                Trait"#]],
832        );
833    }
834
835    #[test]
836    fn works_in_fields() {
837        check(
838            r#"
839macro_rules! foo {
840    () => { u32 };
841}
842struct S {
843    field: foo$0!(),
844}
845"#,
846            expect![[r#"
847                foo!
848                u32"#]],
849        );
850    }
851
852    #[test]
853    fn regression_21489() {
854        check(
855            r#"
856//- proc_macros: derive_identity
857//- minicore: derive, fmt
858#[derive(Debug, proc_macros::DeriveIdentity$0)]
859struct Foo;
860        "#,
861            expect![[r#"
862                proc_macros::DeriveIdentity
863                struct Foo;"#]],
864        );
865    }
866}