-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Document custom derive. #38770
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
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 :)
src/doc/book/procedural-macros.md
Outdated
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
?
src/doc/book/procedural-macros.md
Outdated
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
.
src/doc/book/procedural-macros.md
Outdated
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?
src/doc/book/procedural-macros.md
Outdated
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.
src/doc/book/procedural-macros.md
Outdated
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
src/doc/book/procedural-macros.md
Outdated
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.
src/doc/book/procedural-macros.md
Outdated
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?
src/doc/book/procedural-macros.md
Outdated
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"?
src/doc/book/procedural-macros.md
Outdated
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. |
src/doc/book/procedural-macros.md
Outdated
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
src/doc/reference.md
Outdated
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
src/doc/reference.md
Outdated
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.
src/doc/reference.md
Outdated
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`/
src/doc/reference.md
Outdated
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.
src/doc/book/procedural-macros.md
Outdated
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
src/doc/book/procedural-macros.md
Outdated
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/
src/doc/reference.md
Outdated
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/
src/doc/book/procedural-macros.md
Outdated
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
src/doc/book/procedural-macros.md
Outdated
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 |
src/doc/book/SUMMARY.md
Outdated
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.
|
src/doc/reference.md
Outdated
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.
src/doc/book/procedural-macros.md
Outdated
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 |
⌛ Testing commit 3075c1f with merge 5d994d8... |
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.