Skip to content

Commit 83bc44b

Browse files
committed
Switch to #[forbid(unused_attribute)] idea
1 parent 0634e83 commit 83bc44b

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

text/0000-rustc-attribute.md

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Summary
66

7-
Feature gate attributes of the type `#[rustc_*]` and `#[rustc]` to allow for backwards compatibility of builtin attributes.
7+
Feature gate unused attributes for backwards compatibility.
88

99
# Motivation
1010

@@ -17,53 +17,32 @@ contains uses of the `#[awesome_deriving]` attribute might be broken. While such
1717

1818
# Detailed design
1919

20-
We deny the usage of any attributes with a name of `rustc` or a name starting with `rustc_` (unless a feature gate, `rustc_attributes` is enabled).
20+
We add a feature gate, `custom_attribute`, that disallows the use of any attributes not defined by the compiler or consumed in any other way.
2121

22-
Whenever we define a new attribute that can be used outside of rustc (`#[must_use]` or `#[repr(..)]` are examples of preexisting attributes that do this),
23-
we can give it a name starting with `rustc_`, eg `#[rustc_attr_name]` (an alternative is `#[rustc(attr_name)]`, it's best to reserve both for now).
24-
We then whitelist the attribute from being denied; so that one will not need to enable the feature gate to use it in external crates.
25-
26-
27-
This is fairly simple to achieve.
28-
29-
- Add a feature gate for mentioning attributes that match the given patterns. Disallow its usage in release builds.
30-
- Have a builtin check for such attributes that will error when they are used, _unless_ they are in a whitelist or if the gate is opened
31-
- The whitelist will contain a list of language-exported custom attributes that are allowed to be used in release code.
32-
We can add more complicated per-attribute feature-gate checking as needed (eg for `#[rustc_on_unimplemented]`,
33-
which is builtin and exported but behind a feature gate).
34-
35-
Note that this RfC does not impose any rules on future attributes defined by the compiler (except that they must be backwards compatible).
36-
One is free to use `#[rustc_attr_name]`, `#[rustc(attr_name)]`, or perhaps something like `#[rustc::attr_name]` if in the future we get
37-
arbitrary token trees in attributes (or at least some form of namespacing).
22+
This is achieved by elevating the `unused_attribute` lint to a feature gate check (with the gate open, it reverts to being a lint). We'd also need to ensure that it runs after all the other lints (currently it runs as part of the main lint check and might warn about attributes which are actually consumed by other lints later on).
3823

3924
# Drawbacks
4025

41-
I don't see much of a drawback (except that the alternatives below might be more lucrative)
26+
I don't see much of a drawback (except that the alternatives below might be more lucrative). This might make it harder for people who wish to use custom attributes for static analysis in 1.0 code.
4227

4328
# Alternatives
4429

45-
## Forbid `unused_attributes`
30+
## Forbid `#[rustc_*]` and `#[rustc(...)]` attributes
4631

47-
This is an alternative that is quite feasible. We simply make unused attributes a hard (unsilencable, perhaps feature gate silencing) error for release,
48-
and the problem is solved. Compiler-defined attributes can be whitelisted for `unused_attributes` if necessary, but a random
49-
attribute floating around in 1.0-release code will not be allowed (there's no reason to have it anyway, except perhaps for static analysis code)
32+
(This was the original proposal in the RfC)
5033

51-
For this, we may have to move the unused attribute check to somewhere post-lints; currently other lints may run after it and an attribute that is actually used
52-
isn't marked as such.
53-
54-
This might be more work to implement than the main RfC (and is more drastic); but I don't see any major issues with this a priori except for the aforementioned
55-
hurdle for static analysis. If the community is okay with it I'd love to make this the main proposal.
34+
This is less restrictive for the user, but it restricts us to a form of namespacing for any future attributes which we may wish to introduce. This is suboptimal, since by the time plugins stabilize (which is when user-defined attributes become useful for release code) we may add many more attributes to the compiler and they will all have cumbersome names.
5635

5736
## Do nothing
5837

5938
If we do nothing we can still manage to add new attributes, however we will need to invent new syntax for it. This will probably be in the form of basic namespacing support
6039
(`#[rustc::awesome_deriving]`) or arbitrary token tree support (the use case will probably still end up looking something like `#[rustc::awesome_deriving]`)
6140

41+
This has the drawback that the attribute parsing and representation will need to be overhauled before being able to add any new attributes to the compiler.
42+
6243

6344
# Unresolved questions
6445

6546
Which proposal to use — disallowing `#[rustc_*]` and `#[rustc]` attributes, or just `#[forbid(unused_attribute)]`ing everything.
6647

67-
The main proposal can be tweaked to just disallow the `#[rustc()]` attribute (or just the `rustc_` prefix).
68-
69-
The names of the prefix and the feature gate could peraps be improved.
48+
The name of the feature gate could peraps be improved.

0 commit comments

Comments
 (0)