Repository navigation
Reject unexpanded attributes in attributes - #164712
Conversation
|
Some changes occurred in compiler/rustc_attr_parsing |
|
|
70f1a26 to
64d8572
Compare
There was a problem hiding this comment.
My gut instinct here is we should not allow attributes inside other attributes at all. This is not part of the stmt_expr_attributes RFC, so I think that this is possible is probably an accident. I don't think attributes on literals in attributes are useful, and I wouldn't be surprised if there are more bugs to be found here.
How do you feel about that?
|
This did cross my mind. However, for non-early non-inert attributes they should pretty much just work (tm). E.g., From a language design perspective, I don't see why we should forbid non-inert / macro attributes there since we already allow macro calls in such positions: However, most inert attributes we should indeed probably forbid there before stabilizing |
|
Allowing non-inert macros, while forbidding inert ones seems reasonable to me. |
64d8572 to
7c30ce2
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
7c30ce2 to
b845654
Compare
|
Ready for review. Please see the updated PR description for a summary. The diff has grown quite massively because of all the test cases I added, so fear not ^^! |
| [one] this attribute is | ||
| *[other] these attributes are | ||
| } not allowed in this context" | ||
| )] |
There was a problem hiding this comment.
I know, the diagnostic message is quite vague.
If I could determine whether we're "not early", I could specialize this message to say "inert attributes aren't allowed in this context" but then again, I haven't found any preexisting (user-facing) diagnostics that used the term "inert"... but maybe it would be fine to use that jargon...
There was a problem hiding this comment.
I think this is a good compromise between not throwing jargon at users and being clear what is wrong
677552e to
b400fb1
Compare
This comment has been minimized.
This comment has been minimized.
b400fb1 to
88cbcf7
Compare
|
Whoops, |
|
Just a check, probably not significant but attribute parsing has continued to surprise me perf-wise |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Reject unexpanded attributes in attributes
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (9f946e8): comparison URL. Overall result: no relevant changes - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countThis perf run didn't have relevant results for this metric. Max RSS (memory usage)Results (secondary 3.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary 2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 485.883s -> 482.884s (-0.62%) |
…uwer Rollup of 9 pull requests Successful merges: - #165161 (`rustc_builtin_macros` cleanup, part 9) - #165663 (Avoid repeated gated spans snapshots during macro matching) - #165637 (library: std: process: Make Command::status platform specific) - #163571 ([rustdoc] Render `Deref` methods on aliased items) - #164712 (Reject unexpanded attributes in attributes) - #165516 (Point at array length instead of whole array type) - #165674 (Add regression test for issue 118449) - #165676 (Make `Proxy` weak reference as it seems that it is not dropped at all) - #165695 (Add regression test for unsafe diagnostic attributes)
Rollup merge of #164712 - fmease:early-attr-nested-attr, r=JonathanBrouwer Reject unexpanded attributes in attributes Reject "unexpanded" attributes on the "value" of key-value attributes. For "early"[^1] attributes, "unexpanded" means any attribute. For normal attribute, "unexpanded" means inert[^2]. For the early attributes `crate_name`, `crate_type` & `recursion_limit` the motivation for this change is the fact that the result of the expansion wouldn't get used and can't for that matter as the value needs to be known before macro expansion. For the other kind of early attributes, namely attributes placed on macro calls, such contained attributes wouldn't even get name resolved due to the overarching attribute getting dropped early when the macro call, the target, gets expanded. "Non-early", so normal, attributes didn't get parsed by the attribute parsers, so they weren't semantically validated at all. While it shouldn't be that hard to recursively validate nested attributes using the attribute parsers (e.g., `#[doc = #[doc = #[repr(unknown)] ""] ""]`), it's not worth the complexity. Moreover, it's not clear whether we can easily support such attributes in the rest of the compiler. For example, would our lint infra properly support `#[doc = #[deny(rustdoc::invalid_html_tag)] "text"]` out of the box? Let's not make life complicated and just forbid inert attributes in such contexts for now at least. This is not a breaking change since such attributes are gated behind unstable feature `stmt_expr_attributes` (plus `proc_macro_hygiene` if you want to use an attribute macro). Fixes #163330. r? JonathanBrouwer or reassign <sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub> [^1]: Attributes that "need to be known" very early during compilation. Those are `#![crate_name]`, `#![crate_type]`, `#![recursion_limit]` and any attributes places on macro calls. [^2]: Note that since PR #159580 we also reject `cfg` on expressions in *any* context (despite not being inert -- a special case to prevent footguns).
View all comments
Reject "unexpanded" attributes on the "value" of key-value attributes. For "early"1 attributes, "unexpanded" means any attribute. For normal attribute, "unexpanded" means inert2.
For the early attributes
crate_name,crate_type&recursion_limitthe motivation for this change is the fact that the result of the expansion wouldn't get used and can't for that matter as the value needs to be known before macro expansion.For the other kind of early attributes, namely attributes placed on macro calls, such contained attributes wouldn't even get name resolved due to the overarching attribute getting dropped early when the macro call, the target, gets expanded.
"Non-early", so normal, attributes didn't get parsed by the attribute parsers, so they weren't semantically validated at all. While it shouldn't be that hard to recursively validate nested attributes using the attribute parsers (e.g.,
#[doc = #[doc = #[repr(unknown)] ""] ""]), it's not worth the complexity. Moreover, it's not clear whether we can easily support such attributes in the rest of the compiler. For example, would our lint infra properly support#[doc = #[deny(rustdoc::invalid_html_tag)] "text"]out of the box? Let's not make life complicated and just forbid inert attributes in such contexts for now at least.This is not a breaking change since such attributes are gated behind unstable feature
stmt_expr_attributes(plusproc_macro_hygieneif you want to use an attribute macro).Fixes #163330.
r? JonathanBrouwer or reassign
(No LLM was or will be used by me during the entire creation process of this PR)
Footnotes
Attributes that "need to be known" very early during compilation. Those are
#![crate_name],#![crate_type],#![recursion_limit]and any attributes places on macro calls. ↩Note that since PR https://github.com/rust-lang/rust/pull/159580 we also reject
cfgon expressions in any context (despite not being inert -- a special case to prevent footguns). ↩