Skip to content

Add Documentation for Custom Attributes and Error Reporting in Procedural Macros - #39845

Merged
bors merged 4 commits into
rust-lang:masterfrom
JDemler:master
Feb 25, 2017
Merged

Add Documentation for Custom Attributes and Error Reporting in Procedural Macros#39845
bors merged 4 commits into
rust-lang:masterfrom
JDemler:master

Conversation

@JDemler

Copy link
Copy Markdown
Contributor

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

@rust-highfive

Copy link
Copy Markdown
Contributor

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.

@steveklabniksteveklabnik left a comment

Copy link
Copy Markdown
Contributor

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!

Hello, World! My name is Waffles
```
## Custom Attributes
In some cases it might make sense to allow users some kind of configuration.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
ContributorAuthor

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?

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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)

Copy link
Copy Markdown
ContributorAuthor

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?

@xen0nxen0n left a comment

Copy link
Copy Markdown
Contributor

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`.

Copy link
Copy Markdown
Contributor

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:

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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

Hello, World! My name is Waffles
```

We've done it!

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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:

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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.

@JDemler

Copy link
Copy Markdown
ContributorAuthor

Thanks for the nits. They should be resolved now.

@steveklabnik

Copy link
Copy Markdown
Contributor

@bors: r+ rollup

thank you!

@bors

bors commented Feb 24, 2017

Copy link
Copy Markdown
Collaborator

📌 Commit 198208b has been approved by steveklabnik

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 24, 2017
Add Documentation for Custom Attributes and Error Reporting in Procedural Macros
This fixesrust-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`
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 24, 2017
Add Documentation for Custom Attributes and Error Reporting in Procedural Macros
This fixesrust-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`
@frewsxcvfrewsxcv mentioned this pull request Feb 24, 2017
bors added a commit that referenced this pull request Feb 24, 2017
bors added a commit that referenced this pull request Feb 25, 2017
@bors
bors merged commit 198208b into rust-lang:masterFeb 25, 2017
@gilescope

Copy link
Copy Markdown
Contributor

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.

@trsh

trsh commented Jul 24, 2018

Copy link
Copy Markdown

It's still not written how to read HelloWorldName in macro fn

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Procedural macros: attributes are not documented

7 participants

@JDemler@rust-highfive@steveklabnik@bors@gilescope@trsh@xen0n