Skip to content

Commit 1b38a6c

Browse files
committed
Update //@ proc-macro aux build directive docs
1 parent 299a6c8 commit 1b38a6c

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

Diff for: src/tests/compiletest.md

+37-26
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ There are multiple [directives](directives.md) to assist with that:
586586
- `aux-crate`
587587
- `aux-bin`
588588
- `aux-codegen-backend`
589+
- `proc-macro`
589590

590591
`aux-build` will build a separate crate from the named source file. The source
591592
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.
618619

619620
### Auxiliary proc-macro
620621

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`:
623640

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+
```
626646

627-
```rust,ignore
628-
//@ force-host
629-
//@ no-prefer-dynamic
647+
```rs
648+
// tests/ui/cat/meow.rs
630649

631-
#![crate_type = "proc-macro"]
650+
//@ proc-macro: whiskers.rs
632651

633-
extern crate proc_macro;
634-
use proc_macro::TokenStream;
652+
extern crate whiskers; // needed as ui test defaults to edition 2015
635653

636-
#[proc_macro]
637-
pub fn foo(input: TokenStream) -> TokenStream {
638-
"".parse().unwrap()
654+
fn main() {
655+
whiskers::identity!();
639656
}
640657
```
641658

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
648661

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::*;
653664

654-
fn main() {
655-
my_proc_macro::foo!();
665+
#[proc_macro]
666+
pub fn identity(ts: TokenStream) -> TokenStream {
667+
ts
656668
}
657669
```
658670

659-
660671
## Revisions
661672

662673
Revisions allow a single test file to be used for multiple tests. This is done

0 commit comments

Comments
 (0)