Skip to content
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

Impl StyleEditionDefault trait for all configs #5937

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Oct 8, 2023

This PR is a follow up to #5933, but currently includes all the changes from that PR since it hasn't been merged yet.

This PR makes internal changes to define each configuration in terms of the StyleEditionDefault trait, and hard codes StyleEdition::Edition2015 anywhere the new style_edition is needed.

Note users are still unable to configure style edition.

r? @calebcartwright

Edit: #5933 was merged and I rebased this PR.

Comment on lines +38 to +39
max_width: MaxWidth, true, "Maximum width of each line";
hard_tabs: HardTabs, true, "Use tab characters for indentation, spaces for alignment";
tab_spaces: TabSpaces, true, "Number of spaces per tab";
newline_style: NewlineStyleConfig, true, "Unix or Windows line endings";
indent_style: IndentStyleConfig, false, "How do we indent expressions or items";
Copy link
Contributor Author

@ytmimi ytmimi Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a change we need to make now (or ever), but as I was working on this I wondered if we could generalize StyleEditionDefault and rename it to something like RustfmtConfigOption (happy to bikeshed on the name). That trait could also be used to define each configuration option's description, stability, and default value for different style editions.

For example:

use crate::config::StyleEdition;
use crate::config::config_type::ConfigType;

pub trait RustfmtConfigOption {
    type ConfigType: ConfigType;

    fn default(style_edition: StyleEdition) -> Self::ConfigType;
    fn description() -> &'static str;
    fn stability() -> bool;
}

That way, create_config! could focus on setting field names and tys that impl RustfmtConfigOption.

For example if we implemented RustfmtConfigOption for MaxWidth it might look something like this:

impl RustfmtConfigOption for MaxWidth {
    type ConfigType = usize;

    fn default(_: StyleEdition) -> Self::ConfigType { 100 }
    fn description() -> &'static str { "Maximum width of each line" }
    fn stability() -> bool { true }
}

And then the call to create_config! would be simplified to:

create_config! {
    max_width: MaxWidth,
}

This would also eliminate some of the duplication for the test config we create in mod test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the sound of this, but agree it's something worth deferring

Comment on lines 519 to 525
config_option_with_style_edition_default!(
// Fundamental stuff
MaxWidth, usize, 100;
HardTabs, bool, false;
TabSpaces, usize, 4;
NewlineStyleConfig, NewlineStyle, NewlineStyle::Auto;
IndentStyleConfig, IndentStyle, IndentStyle::Block;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defines a unit struct for each config option that implements StyleEditionDefault

@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch from 09eb6ff to 37b0505 Compare December 16, 2023 18:10
pub use crate::config::lists::*;
#[allow(unreachable_pub)]
pub use crate::config::macro_names::{MacroSelector, MacroSelectors};
pub use crate::config::macro_names::MacroSelector;
Copy link
Contributor Author

@ytmimi ytmimi Dec 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unused import lint was triggering for crate::config::lists::*; and MacroSelectors so I removed the import for crate::config::lists::*;, and I moved the MacroSelectors import down to the test module.

Comment on lines -421 to +428
use crate::config::macro_names::MacroName;
use crate::config::macro_names::{MacroName, MacroSelectors};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calebcartwright
Copy link
Member

@ytmimi do you think this would be better to merge before or after the next sync? (no promises but I'm going to try to do one today)

@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 7, 2024

@calebcartwright If we're going to do a sync we should update the changelog. I've started that on #6004, but there are a few other fixes that I merged that don't have entries yet.

@ytmimi
Copy link
Contributor Author

ytmimi commented Apr 7, 2024

That said, we can probably wait to merge this until after the sync

@calebcartwright calebcartwright self-assigned this Apr 7, 2024
@ytmimi ytmimi added the a-2024-edition Style Edition 2024 label Jun 27, 2024
@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch 2 times, most recently from d1d1f41 to 3e03c8d Compare June 27, 2024 05:05
@calebcartwright calebcartwright force-pushed the impl_style_edition_default_trait_for_all_configs branch from 3e03c8d to 2adf879 Compare July 1, 2024 04:39
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still have a bit more to get through but leaving some initial thoughts now

src/config/config_type.rs Outdated Show resolved Hide resolved
src/config/config_type.rs Outdated Show resolved Hide resolved
src/config/config_type.rs Outdated Show resolved Hide resolved
Comment on lines +38 to +39
max_width: MaxWidth, true, "Maximum width of each line";
hard_tabs: HardTabs, true, "Use tab characters for indentation, spaces for alignment";
tab_spaces: TabSpaces, true, "Number of spaces per tab";
newline_style: NewlineStyleConfig, true, "Unix or Windows line endings";
indent_style: IndentStyleConfig, false, "How do we indent expressions or items";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the sound of this, but agree it's something worth deferring

verbose: Verbosity, Verbosity::Normal, false,
"How much to information to emit to the user";
file_lines: FileLines, FileLines::all(), false,
max_width: MaxWidth, true, "Maximum width of each line";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just leaving this as a thought, not requesting/suggesting a pivot, but I found myself having to dart back and forth between the two sites to get a "full picture" of a given option.

that's fine for moving ahead with 2024 edition support, but it's definitely something I want to address sooner rather than later, as it's going to be prone for errors IMO (perhaps you're earlier suggestion on future refactoring would help as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that there's plenty of room for improvement, and I'm open to suggestions on how to make this better

@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch from 2adf879 to 0e612d5 Compare July 6, 2024 00:23
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go ahead with this as this is the big domino we need to fall to start knocking out the other work for 2024 edition.

Couple minor items left inline for future follow up

src/config/mod.rs Show resolved Hide resolved
src/config/options.rs Outdated Show resolved Hide resolved
Makes internal changes to define each configuration in terms of the
`StyleEditionDefault` trait, and hard codes `StyleEdition::Edition2015`
anywhere the new `style_edition` is needed.

**Note** users are still unable to configure `style edition`.
@ytmimi ytmimi force-pushed the impl_style_edition_default_trait_for_all_configs branch from 0e612d5 to 312be6a Compare July 15, 2024 20:43
@ytmimi
Copy link
Contributor Author

ytmimi commented Jul 15, 2024

@ytmimi ytmimi merged commit 7cdb426 into rust-lang:master Jul 15, 2024
26 checks passed
@ytmimi ytmimi deleted the impl_style_edition_default_trait_for_all_configs branch July 15, 2024 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-2024-edition Style Edition 2024 p-high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants