

config_type: add unstable_variant attribute by tommilligan · Pull Request #5379...
source link: https://github.com/rust-lang/rustfmt/pull/5379
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Contributor
tommilligan
commented
28 days ago
First pass at #5378
Adds new functionality to the existing config_proc_macro
crate, such that enums used for configuration can have their variants annotated as unstable_variant
:
#[config_type]
/// How to merge imports.
pub enum ImportGranularity {
/// Do not merge imports.
Preserve,
/// Use one `use` statement per crate.
Crate,
...
/// Adapt import granularity, based on internal heuristics.
#[unstable_variant]
Adaptive
}
This will be checked upon loading config, and a warning emitted similar to using current unstable options on stable toolchain:
Warning: can't set `imports_granularity = Adaptive`, unstable variants are only available in nightly channel.
Contributor
Author
tommilligan
left a comment
I have some questions for someone with more project experience
@@ -57,7 +57,7 @@ unicode-segmentation = "1.9" | ||
unicode-width = "0.1" |
||
unicode_categories = "0.1" |
||
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" } |
||
rustfmt-config_proc_macro = { version = "0.3", path = "config_proc_macro" } |
Contributor
Author
tommilligan
28 days ago
I'm pretty sure this needs a version bump, however I don't know if this has knock on impacts anywhere else? AFAIK this is an internal only crate that we can break without fear?
Outdated
@@ -1,68 +1,68 @@ | ||
# This file is automatically @generated by Cargo. |
||
# It is not intended for manual editing. |
||
version = 3 |
Contributor
Author
tommilligan
28 days ago
I noted the lockfile versions have been upgraded by my local toolchain - happy to somehow keep to the original version if this is an issue for compatibility?
Outdated
}, |
||
// Nightly: everything allowed |
||
// Stable with stable option and variant: allowed |
||
(true, _, _) | (false, true, true) => { |
Contributor
Author
tommilligan
28 days ago
This could be _ => ...
, but I preferred to write the explicit cases to check they made semantic sense, and guard against future changes.
Contributor
Author
tommilligan commented 28 days ago
I would also ask - how should I go about testing this? I couldn't find any tests for the existing unstable options, or the proc macro code internals. In fact, I get test failures when I run |
Haven't really had a chance to look at this yet, but just wanted to say I love the idea! Attributes seem like a really clean and simple way of managing this and I'm excited about the possibilities |
@tommilligan as well as updating |
Contributor
ytmimi commented 24 days ago
Do we need to define a new
Also I believe the tests are failing because the doc tests for |
I can see how testing this could be a little tricky since we don't currently have any options that are stable with
And then we could write a few tests in Not 100% sure if this approach will work. This might cause some issues with the |
//! |
||
//! - `doc_hint`: name-value pair whose value is string literal |
||
//! - `value`: name-value pair whose value is string literal |
||
//! - `unstable_variant`: name only |
Contributor
ytmimi
24 days ago
An idea for a follow up PR: I wonder if we could optionally include the tracking issue number in the unstable_variant
annotation. It might be nice to include a link to the tracking issue in the warning message. Thoughts?
Contributor
Author
tommilligan
20 days ago
I like this idea, but agree it should be in followup
Contributor
Author
tommilligan commented 20 days ago
We don't - I've verified that the attribute works as intended locally. The reason is that the
Thanks for this - I've opened #5389 with a fix for this plus CI to enforce it. |
Thanks for pointing this out. Updated to factor out the common logic of updating with an unstable option/value from I think this points out that you could set unstable options using |
Contributor
Author
tommilligan commented 20 days ago
Thanks for the hints on this. I actually found we already have a |
Contributor
Author
tommilligan commented 20 days ago
@ytmimi Thanks for all your feedback - I think I've addressed everything you raised. CI appears to be failing with a lot of unrelated errors that I don't think are to do with this PR? |
If I had to guess I'd say it's this behavior that's causing the CI to fail, since it's only the "stable" tests that are failing. To reiterate, the "stable" tests don't actually run on a stable version of rustfmt they just run with In theory I agree that this would be a bugfix for unintended behaviour, but I think there's probably a larger discussion that needs to be had around whether we want to continue to let users set unstable options via the command line. #5025 looked to solve this by giving users control of that decision, and allowing them to decide whether rustfmt exits with an abort message or warns and continues formatting when using unstable options. For now, can you simply issue the warning in |
Contributor
Author
tommilligan commented 19 days ago
I think it would be counterintuitive to warn about unstable options/variants, while at the same time allowing them to be set from If this isn't desired, I can look into changing the behaviour of Added unit tests that confirm the behaviour of both |
Contributor
ytmimi commented 18 days ago
Oh don't worry about it. I've definitely done the same thing before
That's fine. I think restoring the original behaviour for now makes sense and we can decide what the best way forward is after we've had some more time to think on it.
Fantastic! I took a look at the tests and everything looks good to me! |
Contributor
ytmimi
left a comment
Thanks for all your work on this! I feel pretty good about what we've got here. @calebcartwright I'm ready to move forward, but I definitely want to give you a chance to take a look before merging
Yes I want to review this one for sure Apologies as I'm not fully up to date with the entirety of the conversation, but given the CLI discussion there's a few things I wanted to go ahead and note:
|
Contributor
ytmimi commented 16 days ago
Yup, that's the consensus. We won't look to deal with that here
The only difference being made right now is that a warning will be issued if you set an unstable value on stable via the config file, but if you set it from the command line no warning will be issued. I think that's fine for now, but I'm personally in favor of issuing the warning even if we end up setting the value. Because of the way #5025 is implemented a warning will be issued regardless of how the option was set once I rebase and integrate these changes.
I'm almost positive that it is. Probably a few minor tweaks need to be made when I rebase to pull these changes in, but should be relatively painless. |
This has unfortunately acquired a merge conflict, could you rebase and resolve when you get a chance? Overall I'm good with the approach, though may have a couple nits (e.g. function names) we can revisit at a later date.
We can continue that discussion on your PR to identify target behavior. The historical thinking, aiui, was to simply warn when the user asked rustfmt for an option that it wasn't going to apply. That's why the current behavior when taking advantage of the |
Contributor
Author
tommilligan commented 8 days ago
Squashed and rebased.
Happy to, I tried to stick with existing convention as far as I could, even though I don't think they're the most intuitive right now.
Thanks for confirming this - happy this PR maintains the original intent to warn only when an option will not be respected despite being specified. |
Member
calebcartwright
left a comment
Thank you so much for the excellent work on this!
It's a capability I've had in mind for a while but hadn't been able to make time for. I realize and appreciate your impetus was largely driven by imports_granularity
, but I just want to stress this will have far greater and broader import around our ability to get more options out (at least partially) on stable
Contributor
Author
tommilligan commented 7 days ago
Thanks, that's great to hear. Fingers crossed, look forward to seeing it in some good use cases in future! And thanks for the great iteration process from both of you, it's really made working on this feature a pleasure. |
</div
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK