-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Eagerly expand bang macros within stability attributes #118406
Conversation
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
I'm quite strongly against doing this. |
It seems like an alternative might be some doc(merge) or whatever annotation to teach rustdoc to not preserve the impl block boundaries, if we don't want that at all for these types? |
Could not assign reviewer from: |
(I should get to this on new year holidays.) |
☔ The latest upstream changes (presumably #119146) made this pull request unmergeable. Please resolve the merge conflicts. |
|
☔ The latest upstream changes (presumably #119088) made this pull request unmergeable. Please resolve the merge conflicts. |
error: expected unsuffixed literal or identifier, found `stable_feature` --> tests/ui/stability-attribute/eager-expansion.rs:37:32 | LL | #[stable(feature = stable_feature!($signedness), since = stable_since!($signedness))] | ^^^^^^^^^^^^^^ ... LL | / nonzero_integers! { LL | | unsigned NonZeroU8(u8) LL | | unsigned NonZeroU16(u16) LL | | unsigned NonZeroU32(u32) ... | LL | | signed NonZeroI64(u64) LL | | } | |_- in this macro invocation | = note: this error originates in the macro `nonzero_integers` (in Nightly builds, run with -Z macro-backtrace for more info)
|
In #118665 I discovered that tidy error: library/core/src/num/nonzero.rs:67: malformed stability attribute: missing `feature` key
tidy error: library/core/src/num/nonzero.rs:82: malformed stability attribute: missing `feature` key
tidy error: library/core/src/num/nonzero.rs:98: malformed stability attribute: missing the `since` key
tidy error: library/core/src/num/nonzero.rs:112: malformed stability attribute: missing `feature` key
tidy error: library/core/src/num/nonzero.rs:450: malformed stability attribute: missing `feature` key
some tidy checks failed I am still interesting in seeing something like this PR made to work, but specifically for stability this may not be worth doing now, and #118406 (comment) tells me there isn't a path to extend this to other kinds of inert attrs (like |
This PR implements support for macro invocations in the values of builtin stability attributes:
This is analogous to how macro expansion is already supported in the value part of
doc
attributes since Rust 1.54 (https://blog.rust-lang.org/2021/07/29/Rust-1.54.0.html#attributes-can-invoke-function-like-macros).#[doc = include_str!("README.md")]
All such macro calls must expand to a literal, like for
doc
andTokenStream::expand_expr
.I have limited this feature to just the following 4 builtin attributes, none of which are available in stable Rust: #[stable(...)], #[unstable(...)], #[rustc_const_stable(...)], #[rustc_const_unstable(...)]. Other builtin attributes, for example #[deprecated(...)], will continue to reject the above syntax.
Context
I began looking into refactoring the macros which generate our NonZero* integer types (https://github.com/rust-lang/rust/blob/1.74.0/library/core/src/num/nonzero.rs) in preparation for landing the refactor to
NonZero<T>
(#82363 (comment)).Today there are 7 separate impl blocks per NonZero integer type. You can see this by counting the
impl
sections under "Implementations" in https://doc.rust-lang.org/1.74.0/std/num/struct.NonZeroU32.html. There isn't anything wrong with that today, but after the 12 different NonZero* types all get melded into a NonZero<T>, there end up being 67 impl blocks in #100428 on that type.This is a usability regression compared to today's separate pages per type. With all those blocks expanded, Ctrl+F is obnoxious because you need to skip 12× past every match you don't care about. With all the blocks collapsed, Ctrl+F is useless. Getting to a state in which exactly one type's (e.g.
NonZero<u32>
) impl blocks are expanded while the rest are collapsed is annoying.Ideally there would be one impl block per type.
Today's implementation in the standard library is arranged like this:
The thing that makes it difficult to transpose this into something that produces 1 impl block per type, while remaining maintainable, is that composing stability attributes using a macro is impossible.
rust/library/core/src/num/nonzero.rs
Lines 179 to 192 in c2ec908
The UI test added in this PR (tests/ui/stability-attribute/eager-expansion.rs) is illustrative of the direction I was headed before realizing this was not supported.