-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Custom attibutes #1755
Custom attibutes #1755
Conversation
This RFC specifies custom attributes, which can be used by both external tools and by compiler plugins of various kinds. It also specifies the interaction of custom attributes, built-in attributes, and attribute-like macros.
I understand why it's being done, but |
To clarify, does this means that all crates which would like to use |
@alexcrichton yes |
Thanks for the clarification! Do you know if it'd be possible to work around that restriction? It seems odd that attributes like |
@alexcrichton in a way the serde one is declared when you import the Serde macro, so they are both declared somehow (although I agree they are somewhat different for the user). I don't think it is possible to work around - the key thing is that you need a list of attributes that the compiler knows are not macros, because otherwise you get inappropriate missing macro errors (or no missing macro errors). |
We could add a pass that explicitly checks all attributes against |
Do you mean even those inside macro-attributed items before those macros are expanded? |
That'd be the idea, yeah. |
Maybeuse something like //crate rustfmt
#![export_attribute(skip)]
//crate example
extern crate rustfmt as abc_rustfmt;
#[abc_rustfmt::skip]
fn main(){
// ...
} This prevents you from having to define the attributes in every crate and allows the attributes to be renamed (in the future?). |
In general though, the tool supplying the attribute will not be linked as an upstream crate, in fact there will be no indication that the tool is used in the code that it is used upon. That is why we need some additional, explicit opt-in rather than relying on |
This seems a reasonable alternative, my reasoning is that we shouldn't require the boilerplate where it is not necessary and that macros should be able to do anything they want to their 'source' code. However, there is benefit in uniformity, so it is not a clear trade-off. |
text/0000-attributes.md
Outdated
|
||
Currently, custom attributes (i.e., those not known to the compiler, e.g., | ||
`rustfmt_skip`) are unstable. There is a future compatibility hazard with custom | ||
attributes: if we add `#[foo]` to the language, than any users using a `foo` |
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.
then*
With apologies for the not-at-all substantive comment, I think the RFC could benefit from some more examples for illustration. At the moment the wall of text is slightly intimidating. (Also the pull request and commit title contain a typo.) |
As an idea, what about #[attribute_namespace]
extern crate rustfmt; which means "crate rustfmt is used for nop attributes only, declare the path without doing anything else"? |
Can you elaborate a bit? I think this might be relevant to my concern rust-lang/rust#35900 (comment) from the 1.1 tracking issue. |
A couple of alternatives from irc (apologies, I forget who I was discussing this with):
It has also been brought up that macros 1.1 custom derives want to use attributes and for the compiler to accept them without trying to do a macro lookup (currently, macros must remove these when processing items, but that is undesirable). This requires some formalisation of the note at https://github.com/nrc/rfcs/blob/fe63acb87f97b8205cad8adb20effb6490d42503/text/0000-attributes.md#a-note-on-macro-attributes Ideally, I think we'd like to use macro name as a prefix, e.g.,
Here An alternative is to permit attributes prefixed with the crate name, e.g., if we have |
|
@steveklabnik wrote:
But So the #![feature(asm)] Still, maybe this is a sign that @liigo has a point about whether the name of the key should change? |
@nrc are you planning to incorporate the alternatives listed here #1755 (comment) into the RFC text in some way? Any other next steps? From skimming the discussion it seems like there is a general request for more examples in the RFC text? |
Ping @nrc, is this something we still want to make progress on in the near future, or should we punt it? |
Closed in favour of #2103 |
This RFC specifies custom attributes, which can be used by both external tools
and by compiler plugins of various kinds. It also specifies the interaction of
custom attributes, built-in attributes, and attribute-like macros.
Rendered