Skip to content

Commit eb8ecf2

Browse files
committed
Runtime information of proc macros
1 parent bb61e7e commit eb8ecf2

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/procedural-macros.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Procedural macros allow you to run code at compile time that operates over Rust
1111
syntax, both consuming and producing Rust syntax. You can sort of think of
1212
procedural macros as functions from an AST to another AST.
1313

14-
### Crates and procedural macros
15-
1614
Procedural macros must be defined in a crate with the [crate type] of
1715
`proc-macro`.
1816

@@ -24,6 +22,20 @@ Procedural macros must be defined in a crate with the [crate type] of
2422
> proc-macro = true
2523
> ```
2624
25+
As functions, they must either return syntax, panic, or loop endlessly. Returned
26+
syntax either replaces or adds the syntax depending on the kind of procedural
27+
macro. Panics are caught by the compiler and are turned into a compiler error.
28+
Endless loops are not caught by the compiler which hangs the compiler.
29+
30+
Procedural macros run during compilation, and thus have the same resources that
31+
the compiler has. For example, standard input, error, and output are the same
32+
that the compiler has access to. Similarly, file access is the same. Because
33+
of this, procedural macros have the same security concerns that [Cargo's
34+
build scripts] have.
35+
36+
Procedural macros have two ways of reporting errors. The first is to panic. The
37+
second is to emit a [`compile_error`] macro invocation.
38+
2739
### The `proc_macro` crate
2840
2941
Procedural macro crates almost always will link to the compiler-provided
@@ -306,8 +318,10 @@ fn invoke4() {}
306318

307319
[`TokenStream`]: ../proc_macro/struct.TokenStream.html
308320
[`TokenStream`s]: ../proc_macro/struct.TokenStream.html
321+
[`compile_error`]: ../std/macro.compile_error.html
309322
[`derive`]: attributes.html#derive
310323
[`proc_macro` crate]: ../proc_macro/index.html
324+
[Cargo's build scripts]: ../cargo/reference/build-scripts.html
311325
[attributes]: attributes.html
312326
[custom attributes]: attributes.html
313327
[crate type]: linkage.html

0 commit comments

Comments
 (0)