-
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
Implement basic input validation for built-in attributes #57321
Conversation
@bors try |
Implement basic input validation for built-in attributes + Stabilize `unrestricted_attribute_tokens` Based on #57272 --- In accordance with the plan in https://internals.rust-lang.org/t/unrestricted-attribute-tokens-feature-status/8561: - The correct top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is enforced for built-in attributes. - For non-built-in non-macro attributes: - The key-value form is restricted to bare minimum required to support what we support on stable - unsuffixed literals (#34981). - Arbitrary token streams in remaining forms (`#[attr(token_stream)]`, `#[attr{token_stream}]`, `#[attr[token_stream]]` ) are now allowed without a feature gate, like in macro attributes. This will close #55208 once completed. Need to go through crater first.
cc @CAD97 |
☀️ Test successful - status-travis |
|
||
#[derive] //~ WARNING empty trait list in `derive` | ||
struct Foo; | ||
// compile-pass | ||
|
||
#[derive()] //~ WARNING empty trait list in `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.
Btw... I tried to look for the reason why this happens instead of unused_attributes but got lost... (cc #54651).
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 is a hardcoded warning in libsyntax/ext/derive.rs
.
derive
is removed during expansion currently (
rust/src/libsyntax/ext/expand.rs
Lines 373 to 374 in d6d32ac
let item = self.fully_configure(item) | |
.map_attrs(|mut attrs| { attrs.retain(|a| a.path != "derive"); attrs }); |
Expansion should degrade #[derive(Macro1, Macro2, ...)]
into #[derive()]
instead with derive attribute itself marked as used when it applies a macro.
src/test/ui/stability-attribute/stability-attribute-sanity-4.rs
Outdated
Show resolved
Hide resolved
I really enjoyed this and the improved error messages as well as the improvements to
My understanding is that the stable grammar of attributes then are: OuterAttr = "#" attr:Attr;
InnerAttr = "#" "!" attr:Attr;
Attr = "[" path:Path input:AttrInput "]";
AttrInput =
| {}
| "(" TOKEN_STREAM ")"
| "[" TOKEN_STREAM "]"
| "{" TOKEN_STREAM "}"
| "=" LITERAL
; Is that correct?
I have some thoughts on this:
Regardless of what I've written here let's do that. |
Assigning to r? @nikomatsakis for technical review |
@craterbot run start=master#c0bbc3927e28c22edefe6a1353b5ecc95ea9a104 end=try#b9139d2caca1db46014a9c302d5c47cfae0d8ae0 mode=check-only |
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
Correct.
In this case the prose is already kind of written actually, in https://internals.rust-lang.org/t/unrestricted-attribute-tokens-feature-status/8561/3.
Hm, I feel exactly oppositely, this is the most trivial part of the proposal.
This also needs some attention and FCP, since this is some change of direction.
I wouldn't want to delay simple and already semi-stable delimited attributes on this.
I think this does requires an RFC and some thoughts about the future, but I wouldn't want to want to work on it myself, since it's not high priority. |
Fair.
Hah; that was implied, sorry... :) (I propose that we do what the PR description suggests; tho I would split off the
It definitely is an improvement; the improved diagnostics are particularly great.
Also fair... I might have some time to work on this, or @CAD97 can do it if they prefer (we can collaborate ofc...). |
🎉 Experiment
|
O_O |
We might want to analyze these to categorize the regressions and see what the roots are before going for plan B... After doing that we might want to allow some new forms (and give them correct semantics rather than just ignoring...) and interpret them correctly or not based on the analysis. From a quick look I saw a lot of |
That's exactly the purpose of the plan B. |
Oh, OK =P |
First of all - no regressions from restricting key-value attributes to literals. Beside that, here's the list of offending attributes with some statistics (statistics include non-root regressions):
Here's the list of root regressions:
|
Conclusions:
It seems quite reasonable to support
|
👍
Agreed; I suggested the same in #56896 (comment).
re. As for deprecation lints, do you mean C-future-compatibility or that we should keep this forever?
😂 |
Yes, didn't notice that. If
What's the difference? Even if the warning ends up being kept forever, it's going to be framed as a deprecation lint anyway. |
@bors r+ |
📌 Commit d3411d3 has been approved by |
@bors p=7 Rollup fairness. |
Implement basic input validation for built-in attributes Correct top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is enforced for built-in attributes, built-in attributes must also fit into the "meta-item" syntax (aka the "classic attribute syntax"). For some subset of attributes (found by crater run), errors are lowered to deprecation warnings. NOTE: This PR previously included #57367 as well.
☀️ Test successful - checks-travis, status-appveyor |
Stabilize `unrestricted_attribute_tokens` In accordance with a plan described in https://internals.rust-lang.org/t/unrestricted-attribute-tokens-feature-status/8561/3. Delimited non-macro non-builtin attributes now support the same syntax as macro attributes: ``` PATH PATH `(` TOKEN_STREAM `)` PATH `[` TOKEN_STREAM `]` PATH `{` TOKEN_STREAM `}` ``` Such attributes mostly serve as inert proc macro helpers or tool attributes. To some extent these attributes are de-facto stable due to a hole in feature gate checking (feature gating is done too late - after macro expansion.) So if macro *removes* such helper attributes during expansion (and it must remove them, unless it's a derive macro), then the code will work on stable. Key-value non-macro non-builtin attributes are now restricted to bare minimum required to support what we support on stable - unsuffixed literals (#34981). ``` PATH `=` LITERAL ``` (Key-value macro attributes are not supported at all right now.) Crater run in #57321 found no regressions for this change. There are multiple possible ways to extend key-value attributes (#57321 (comment)), but I'd expect an RFC for that and it's not a pressing enough issue to block stabilization of delimited attributes. Built-in attributes are still restricted to the "classic" meta-item syntax, nothing changes here. #57321 goes further and adds some additional restrictions (more consistent input checking) to built-in attributes. Closes #55208
@petrochenkov I was curious, was the Also |
Ha, good catch. |
Ah, thanks!. The reason I ask is because I'm looking at improving the reference documentation for attributes, and now all the attributes have nice input validation, except |
I think so, each macro will have to do that individually though, without help from the validation infra introduced in this PR. |
Correct top-level shape (
#[attr]
vs#[attr(...)]
vs#[attr = ...]
) is enforced for built-in attributes, built-in attributes must also fit into the "meta-item" syntax (aka the "classic attribute syntax").For some subset of attributes (found by crater run), errors are lowered to deprecation warnings.
NOTE: This PR previously included #57367 as well.