Skip to content

Commit 98aca06

Browse files
committed
Add reference-level explanation with grammar additions
1 parent cae86ff commit 98aca06

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

text/3698-declarative-derive-macros.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ syntax without an unsafe derive will trigger an "unused unsafe"
8282
lint. (RFC 3715 defines the equivalent mechanism for proc macro
8383
derives.)
8484

85+
# Reference-level explanation
86+
[reference-level-explanation]: #reference-level-explanation
87+
88+
The grammar for macros is extended as follows:
89+
90+
> _MacroRule_ :\
91+
> &nbsp;&nbsp; ( `unsafe`<sup>?</sup> `derive` `(` `)` )<sup>?</sup> _MacroMatcher_ `=>` _MacroTranscriber_
92+
93+
The _MacroMatcher_ matches the entire construct the attribute was
94+
applied to, receiving precisely what a proc-macro-based attribute
95+
would in the same place.
96+
97+
(The empty parentheses after `derive` reserve future syntax space
98+
for derives accepting arguments, at which time they'll be replaced
99+
by a second _MacroMatcher_ that matches the arguments.)
100+
101+
A derive invocation that uses an `unsafe derive` rule will produce
102+
an error if invoked without using the `unsafe` derive syntax. A
103+
derive invocation that uses an `derive` rule (without `unsafe`)
104+
will trigger the "unused unsafe" lint if invoked using the `unsafe`
105+
derive syntax. A single derive macro may have both `derive` and
106+
`unsafe derive` rules, such as if only some invocations are unsafe.
107+
108+
This grammar addition is backwards compatible: previously, a _MacroRule_ could
109+
only start with `(`, `[`, or `{`, so the parser can easily distinguish rules
110+
that start with `derive` or `unsafe`.
111+
112+
Adding `derive` rules to an existing macro is a semver-compatible change,
113+
though in practice, it will likely be uncommon.
114+
115+
If a user invokes a macro as a derive and that macro does not have any `derive`
116+
rules, the compiler should give a clear error stating that the macro is not
117+
usable as a derive because it does not have any `derive` rules.
118+
85119
# Drawbacks
86120
[drawbacks]: #drawbacks
87121

0 commit comments

Comments
 (0)