@@ -586,6 +586,7 @@ There are multiple [directives](directives.md) to assist with that:
586
586
- ` aux-crate `
587
587
- ` aux-bin `
588
588
- ` aux-codegen-backend `
589
+ - ` proc-macro `
589
590
590
591
` aux-build ` will build a separate crate from the named source file. The source
591
592
file should be in a directory called ` auxiliary ` beside the test file.
@@ -618,45 +619,55 @@ for tests in `tests/ui-fulldeps`, since it requires the use of compiler crates.
618
619
619
620
### Auxiliary proc-macro
620
621
621
- If you want a proc-macro dependency, then there currently is some ceremony
622
- needed.
622
+ If you want a proc-macro dependency, then you can use the ` proc-macro `
623
+ directive. This directive behaves just like ` aux-build ` , i.e. that you should
624
+ place the proc-macro test auxiliary file under a ` auxiliary ` folder under the
625
+ same parent folder as the main test file. However, it also has four additional
626
+ preset behavior compared to ` aux-build ` for the proc-macro test auxiliary:
627
+
628
+ 1 . The aux test file is built with ` --crate-type=proc-macro ` .
629
+ 2 . The aux test file is built without ` -C prefer-dynamic ` , i.e. it will not try
630
+ to produce a dylib for the aux crate.
631
+ 3 . The aux crate is made available to the test file via extern prelude with
632
+ ` --extern <aux_crate_name> ` . Note that since UI tests default to edition
633
+ 2015, you still need to specify ` extern <aux_crate_name> ` unless the main
634
+ test file is using an edition that is 2018 or newer.
635
+ 4 . The ` proc_macro ` crate is made available as an extern prelude module. Same
636
+ edition 2015 vs newer edition distinction for ` extern proc_macro; ` applies.
637
+
638
+ For example, you might have a test ` tests/ui/cat/meow.rs ` and proc-macro
639
+ auxiliary ` tests/ui/cat/auxiliary/whiskers.rs ` :
623
640
624
- Place the proc-macro itself in a file like ` auxiliary/my-proc-macro.rs ` with the
625
- following structure:
641
+ ``` text
642
+ tests/ui/cat/
643
+ meow.rs # main test file
644
+ auxiliary/whiskers.rs # auxiliary
645
+ ```
626
646
627
- ``` rust,ignore
628
- //@ force-host
629
- //@ no-prefer-dynamic
647
+ ``` rs
648
+ // tests/ui/cat/meow.rs
630
649
631
- #![crate_type = " proc-macro"]
650
+ // @ proc-macro: whiskers.rs
632
651
633
- extern crate proc_macro;
634
- use proc_macro::TokenStream;
652
+ extern crate whiskers; // needed as ui test defaults to edition 2015
635
653
636
- #[proc_macro]
637
- pub fn foo(input: TokenStream) -> TokenStream {
638
- "".parse().unwrap()
654
+ fn main () {
655
+ whiskers :: identity! ();
639
656
}
640
657
```
641
658
642
- The ` force-host ` is needed because proc-macros are loaded in the host compiler,
643
- and ` no-prefer-dynamic ` is needed to tell compiletest to not use
644
- ` prefer-dynamic ` which is not compatible with proc-macros. The ` #![crate_type] `
645
- attribute is needed to specify the correct crate-type.
646
-
647
- Then in your test, you can build with ` aux-build ` :
659
+ ``` rs
660
+ // tests/ui/cat/auxiliary/whiskers.rs
648
661
649
- ``` rust,ignore
650
- //@ aux-build: my-proc-macro.rs
651
-
652
- extern crate my_proc_macro;
662
+ extern crate proc_macro;
663
+ use proc_macro :: * ;
653
664
654
- fn main() {
655
- my_proc_macro::foo!();
665
+ #[proc_macro]
666
+ pub fn identity (ts : TokenStream ) -> TokenStream {
667
+ ts
656
668
}
657
669
```
658
670
659
-
660
671
## Revisions
661
672
662
673
Revisions allow a single test file to be used for multiple tests. This is done
0 commit comments