-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document custom derive. #38770
Document custom derive. #38770
Conversation
@steveklabnik: no appropriate reviewer found, use r? to override |
/cc @rust-lang/docs @rust-lang/lang |
Would it make sense to document it in the reference as well? Derive is documented there too. |
Ah ha! I knew I forgot something. Yes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. Added a few inline comments with suggestions :)
With some kind of nice output, like `Hello, World! My name is Pancakes.`. | ||
|
||
Let's go ahead and write up what we think our macro will look like from a user | ||
perspective. In `hello-world\main.rs` we write: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't that be src/main.rs
?
``` | ||
|
||
Great. So now we just need to actually write the procedural macro. Let's start | ||
a new crate called `hello-world-macro` inside our `hello-world` project. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a sentence about naming convention? I've seen a bunch of crates that call these <name>_derive
.
``` | ||
|
||
So there is a lot going on here. We have introduced two new crates: `syn` and | ||
`quote`. As you may have noticed, `input: TokenSteam` is immediately converted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Link "syn" and "quote" to their repositories or crates.io pages?
to a String. This String is a string representation of the rust code for which | ||
we are deriving `HelloWorld` for--that's it. So what we really need is to be | ||
able to _parse_ rust code into something usable. This is where `syn` comes to | ||
play. `syn` is a `nom` based rust parser, and it's extremely useful. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People who don't know nom will be confused by this.
|
||
So there is a lot going on here. We have introduced two new crates: `syn` and | ||
`quote`. As you may have noticed, `input: TokenSteam` is immediately converted | ||
to a String. This String is a string representation of the rust code for which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lowercase "rust", same a few times below
} | ||
``` | ||
|
||
So there is a lot going on here. We have introduced two new crates: `syn` and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double space after sentence here and a few times below; I don't think the rest of the book does this.
|
||
So there is a lot going on here. We have introduced two new crates: `syn` and | ||
`quote`. As you may have noticed, `input: TokenSteam` is immediately converted | ||
to a String. This String is a string representation of the rust code for which |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put "String" in back ticks?
able to _parse_ rust code into something usable. This is where `syn` comes to | ||
play. `syn` is a `nom` based rust parser, and it's extremely useful. The | ||
other crate we've introduced is `quote`. It's essentially the dual of `syn` as | ||
it will make serializing rust code really easy. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe "serializing rust code" → "generating Rust code"?
let s = input.to_string(); | ||
|
||
// Parse the string representation | ||
let ast = syn::parse_macro_input(&s).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error handling probably deserves a short paragraph as well, even if it's just "panicking the proc macro code will show it as a compiler error pointing to the derive".
How do you feel about making |
Okay! I think I've fixed up everything, @withoutboats @killercup . Thanks for the review 😄 Still have to update the reference, should push that in a minute. |
|
||
```bash | ||
error: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type | ||
--> hello-world-macro\src\lib.rs:8:3 | ||
--> hello-world-derive\src\lib.rs:8:3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this path uses backslashes, the rest of the book probably not
|
||
* [Macros](book/macros.html) define new syntax in a higher-level, | ||
declarative way. | ||
* [Procedural Macros][procedural macros] can be used to implement custom derive. | ||
|
||
## Macros |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little weird to me that ## Macros
is a subheading within # Macros
, but I see why you did it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i would like to re-do this and other parts of the reference but i'm trying to be minimally invasive here
@@ -2319,6 +2337,8 @@ impl<T: PartialEq> PartialEq for Foo<T> { | |||
} | |||
``` | |||
|
|||
You can implement derive for your own type through [procedural macros](#procedural-macros). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Earlier in this section, "derive" is styled as
`derive`
with backticks, so might be good to be consistent.
* `--crate-type=proc-macro`, `#[crate_type = "proc-macro"]` - The output | ||
produced is not specified, but if a `-L` path is provided to it then the | ||
compiler will recognize the output artifacts as a macro and it can be loaded | ||
for a program. If a crate is compiled with the proc-macro crate type t will |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/t will/it will/
It looks like crate types are styled with backticks, so also:
s/proc-macro/`proc-macro`/
forbid exporting any items in the crate other than those functions tagged | ||
`#[proc_macro_derive]` and those functions must also be placed at the crate | ||
root. Finally, the compiler will automatically set the `cfg(proc_macro)` | ||
annotation whenever any crate type of a compilation is the rustc-macro crate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/rustc-macro/`rustc-macro`/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed like a decent summary. The one thing I would say is that I would have expected a bit more up-front material discussing the 'basic model' for derive (string in, string out, you do some parsing and serialization in the model), but that's because I tend towards more of a "top-down" style, I think, whereas this is kind of "bottom-up". (i.e., here is the code to write, let's explain why you wrote it) In any case, all the important stuff seems to be there, and it reads pretty smoothly.
I left some nits.
|
||
Rust includes several traits that you can derive, but it also lets you define | ||
your own. We can accomplish this task through a feature of Rust called | ||
"procedrual macros." Eventually, procedural macros will allow for all sorts of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: procedural
type using `ast.ident`. The `quote!` macro let's us write up the Rust code | ||
that we wish to return and convert it into `Tokens`. `quote!` let's us use some | ||
really cool templating mechanics; we simply write `#name` and `quote!` will | ||
replace it with the variable named `name`. You can even do some repition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/repition/repetition/
@@ -652,6 +648,28 @@ Rust syntax is restricted in two ways: | |||
|
|||
[RFC 550]: https://github.com/rust-lang/rfcs/blob/master/text/0550-macro-future-proofing.md | |||
|
|||
## Procedrual Macros |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/Procedrual/Procedural/
@@ -0,0 +1,209 @@ | |||
% Procedrual Macros (and custom Derive) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: s/Procedrual/Procedural/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i am... really bad at this spelling apparently, haha
``` | ||
|
||
Oh, so it appears that we need to declare that our `hello-world-derive` crate is | ||
a `proc-macro` crate type. How do we do this? Like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stream of consciousness bit felt a little over the top to me, but it's fine. =)
I'm not totally opposed to this; I was thinking about making it future proof. That is, right now it's string in string out because it's a hack. It's really |
@@ -52,6 +52,7 @@ | |||
* [Borrow and AsRef](borrow-and-asref.md) | |||
* [Release Channels](release-channels.md) | |||
* [Using Rust without the standard library](using-rust-without-the-standard-library.md) | |||
* [Procedural Macros (and custom Derive)](procedural-macros.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we’re continuing the tradition of inconsistent capitalisation—nay, taking it to a new level, with mixed title and sentence case all in one line!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah "Derive" felt like something that deserved a capital here, maybe not, idk
Thanks for writing the doc. I can get the basic concept of proc-macro now, but still haunted by some questions.
|
|
||
A number of minor features of Rust are not central enough to have their own | ||
syntax, and yet are not implementable as functions. Instead, they are given | ||
names, and invoked through a consistent syntax: `some_extension!(...)`. | ||
|
||
Users of `rustc` can define new syntax extensions in two ways: | ||
|
||
* [Compiler plugins][plugin] can include arbitrary Rust code that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably continue to document compiler plugins in the reference for the time being; they still exist & will for some time, and of course some project are heavily reliant on them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't removing documentation; it's re-ordering it. I thought putting macros by example first was a better way of doing things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it went from Compiler plugins, Macros
to Macros, Procedural Macros
, shouldn't it be Macros, Procedural Macros, Compiler plugins
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that procedural macros are compiler plugins. That is, today "compiler plugins" are "using libsyntax to extend the compiler" but that's never going to be stable, only the new interface we're calling "procedural macros"
The names of all of this stuff has been very hard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, but we've implemented (and now stabilized) the new procedural macros interface only for controlling derive macros. Users who want to define custom attributes or function style procedural macros (without using a hack) are still using the never-to-be-stabilized compiler plugin interface.
Until we've fully implemented procedural macros, compiler plugins will still be used by these users and so we shouldn't drop whatever docs we have on it (even if we don't focus any attention on improving it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't actually remove any of those docs generally, just the small description here. Let me fix it.
``` | ||
|
||
To make sure that our `hello-world` crate is able to find this new crate we've | ||
crated, we'll add it to our toml: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/crated/created/
Sure, but it's much, much, much harder. The benefits are that syn and quote do parsing and generation for you.
This is the difference between macros 1.1 and macros 2.0, in a nutshell.
This is the entire API. That's it. There's nothing else to explain. It is extremely bare-bones. |
eb2c83f
to
16f76c4
Compare
Okay, I believe that I've addressed everyone's comments now, both on GitHub and in the text. I've squashed, since this is going to need to be backported, and want to make that easy. |
Let's do it. @bors r+ |
📌 Commit eb2c83f has been approved by |
@bors rollup |
16f76c4
to
c0efdbf
Compare
@bors: r=nikomatsakis rollup (#38770 (comment)) |
📌 Commit c0efdbf has been approved by |
…ikomatsakis Document custom derive. These are some bare-bones documentation for custom derive, needed to stabilize "macros 1.1", rust-lang#35900 The book chapter is based off of a blog post by @cbreeden, https://cbreeden.github.io/Macros11/ Normally, we have a policy of not mentioning external crates in documentation. However, given that syn/quote are basically neccesary for properly using macros 1.1, I feel that not including them here would make the documentation very bad. So the rules should be bent in this instance. So far, this PR includes only docs; @alexcrichton said in rust-lang#35900 that he'd be okay with landing them before stabilization; I don't mind either way.
@bors: rollup- p=1 Let's land this quickly so we can backport |
🔒 Merge conflict |
☔ The latest upstream changes (presumably #38783) made this pull request unmergeable. Please resolve the merge conflicts. |
These are some bare-bones documentation for custom derive, needed to stabilize "macros 1.1", rust-lang#35900 The book chapter is based off of a blog post by @cbreeden, https://cbreeden.github.io/Macros11/ Normally, we have a policy of not mentioning external crates in documentation. However, given that syn/quote are basically neccesary for properly using macros 1.1, I feel that not including them here would make the documentation very bad. So the rules should be bent in this instance.
c0efdbf
to
3075c1f
Compare
@bors: r=alexcrichton p=1 |
📌 Commit 3075c1f has been approved by |
moment, procedural macros need to be in their own crate. Eventually, this | ||
restriction may be lifted, but for now, it's required. As such, there's a | ||
convention; for a crate named `foo`, a custom derive procedural macro is called | ||
`foo-derive`. Let's start a new crate called `hello-world-derive` inside our |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the convention is foo_derive
not foo-derive
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
package names are supposed to prefer -
over _
.
⌛ Testing commit 3075c1f with merge 8b9bc29... |
💔 Test failed - status-travis |
@bors: retry (travis is spurious) |
@bors: force |
Document custom derive. These are some bare-bones documentation for custom derive, needed to stabilize "macros 1.1", #35900 The book chapter is based off of a blog post by @cbreeden, https://cbreeden.github.io/Macros11/ Normally, we have a policy of not mentioning external crates in documentation. However, given that syn/quote are basically neccesary for properly using macros 1.1, I feel that not including them here would make the documentation very bad. So the rules should be bent in this instance. So far, this PR includes only docs; @alexcrichton said in #35900 that he'd be okay with landing them before stabilization; I don't mind either way.
☀️ Test successful - status-appveyor, status-travis |
These are some bare-bones documentation for custom derive, needed
to stabilize "macros 1.1",
#35900
The book chapter is based off of a blog post by @cbreeden,
https://cbreeden.github.io/Macros11/
Normally, we have a policy of not mentioning external crates in
documentation. However, given that syn/quote are basically neccesary
for properly using macros 1.1, I feel that not including them here
would make the documentation very bad. So the rules should be bent
in this instance.
So far, this PR includes only docs; @alexcrichton said in #35900 that he'd be okay with landing them before stabilization; I don't mind either way.