-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add Documentation for Custom Attributes and Error Reporting in Procedural Macros #39845
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
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 have some small formatting comments, but looks good to me!
@@ -209,5 +209,73 @@ Ok so now, let's compile `hello-world`. Executing `cargo run` now yields: | |||
Hello, World! My name is FrenchToast | |||
Hello, World! My name is Waffles | |||
``` | |||
## Custom Attributes | |||
In some cases it might make sense to allow users some kind of configuration. |
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.
could you put a newline above this line, please?
|
||
|
||
## Raising Errors | ||
Let's assume that we do not want to accept `Enums` as input to our custom derive method. |
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.
can you add a newline above this line?
``` | ||
|
||
Multiple attributes can be specified that way. | ||
|
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.
could you delete one of these newlines?
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.
Do you mean the newline before the heading or after the code block?
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.
there should only be one newline, like this:
Multiple attributes can be specified that way.
## Raising Errors
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 is, before the heading)
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.
So the fixed whitespace issues
-commit fixed this issue, correct?
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.
Some minor nits, but the snippets are LGTM.
Let's assume that we do not want to accept `Enums` as input to our custom derive method. | ||
|
||
This condition can be easily checked with the help of `syn`. | ||
But how to we tell the user, that we do not accept `Enums`. |
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.
how to
->how do
- trailing
.
->?
|
||
This condition can be easily checked with the help of `syn`. | ||
But how to we tell the user, that we do not accept `Enums`. | ||
The idiomatic was to report errors in procedural macros is to panic: |
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.
was
-> way
error: The attribute `HelloWorldName` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642) | ||
``` | ||
|
||
The compiler needs to know that we handle this attribute and to not respond with an error. |
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 handle
-> we're handling
@@ -210,4 +210,74 @@ Hello, World! My name is FrenchToast | |||
Hello, World! My name is Waffles | |||
``` | |||
|
|||
We've done 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.
Removing this for the next section seems to make the previous section truncated, IMO removal is not needed.
## Custom Attributes | ||
|
||
In some cases it might make sense to allow users some kind of configuration. | ||
For our example the user might want to overwrite the name that is printed in the `hello_world()` method. |
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.
For example, ...
is enough for introducing the example.
``` | ||
|
||
The compiler needs to know that we handle this attribute and to not respond with an error. | ||
This is done in the `hello-world-derive`-crate by adding `attributes` to the `proc_macro_derive` attribute: |
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.
The hyphen before crate
should be replaced by a space.
|
||
## Raising Errors | ||
|
||
Let's assume that we do not want to accept `Enums` as input to our custom derive method. |
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.
The use of Enums
is over-emphasis, IMO just enums
or enumerations
without the inline code block is enough.
Thanks for the nits. They should be resolved now. |
@bors: r+ rollup thank you! |
📌 Commit 198208b has been approved by |
Add Documentation for Custom Attributes and Error Reporting in Procedural Macros This fixes rust-lang#39821 . I'm not sure if the process of how to access custom attributes should be documented as well. But I feel, that this should rather be documented in `syn`
Add Documentation for Custom Attributes and Error Reporting in Procedural Macros This fixes rust-lang#39821 . I'm not sure if the process of how to access custom attributes should be documented as well. But I feel, that this should rather be documented in `syn`
Hoping feedback is always welcome: A paragraph pointing to syn with a little example of how to access HelloWorldName = "the best Pancakes" would be really really appreciated on that page. Its not clear from the TokenStream input type - I looked here https://doc.rust-lang.org/proc_macro/struct.TokenStream.html and find no mention of attributes. |
It's still not written how to read |
This fixes #39821 .
I'm not sure if the process of how to access custom attributes should be documented as well.
But I feel, that this should rather be documented in
syn