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

Add StyleEdition enum and StyleEditionDefault trait #5933

Conversation

ytmimi
Copy link
Contributor

@ytmimi ytmimi commented Oct 6, 2023

Note This does not add the style_edition config option to rustfmt. The StyleEdition enum will eventually be used to allow users to configure style_edition, but for now it's added so we can introduce the the StyleEditionDefault trait.

This PR is an alternative to #5854 that doesn't use proc macros.

r? @calebcartwright

@ytmimi ytmimi force-pushed the alternative_style_edition_default_trait branch from 3d8a56a to 84d2d6b Compare October 6, 2023 21:18
@calebcartwright
Copy link
Member

Thanks @ytmimi!

I am still planning to try to review this over the weekend. However, also wanted to ensure you'd seen this @fee1-dead given the thoughts you shared on #5854 (though I know you do not have much review capacity currently either)

/// [the style guide]: https://doc.rust-lang.org/nightly/style-guide/
#[config_type]
pub enum StyleEdition {
#[value = "2015"]
Copy link
Member

Choose a reason for hiding this comment

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

Can this config value be a number? would be nice if it did, but it would probably be inconsistent with other tools. (IIRC I saw a discussion around whether cargo can also have edition option as a number)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see why not, but if I'm missing something please let me know. If you happen to find the link for that discussion in cargo I'll be sure to give it a read. I also want to note that we already allow edition to be a number.

/// The edition of the syntax and semntics of code (RFC 2052).
#[config_type]
pub enum Edition {
#[value = "2015"]
#[doc_hint = "2015"]
/// Edition 2015.
Edition2015,
#[value = "2018"]
#[doc_hint = "2018"]
/// Edition 2018.
Edition2018,
#[value = "2021"]
#[doc_hint = "2021"]
/// Edition 2021.
Edition2021,
#[value = "2024"]
#[doc_hint = "2024"]
/// Edition 2024.
Edition2024,
}

}
}
};
($ty:ident, $config_ty:ty, $default_2015:expr, $default_2024:expr) => {
Copy link
Member

Choose a reason for hiding this comment

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

This seems a bit arbitrary and hard to understand just given the macro call and parameters without reading the macro source. Can we change it so that it is more like match arms? E.g. something like

style_edition_default!(Foo, usize, Edition2024 => 1000, _ => 100)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is a good suggestion! I'll update the macro so that it's easier to understand what's going on by looking at the call.

#[macro_export]
macro_rules! style_edition_default {
($ty:ident, $config_ty:ty, $default:expr) => {
impl crate::config::style_edition::StyleEditionDefault for $ty {
Copy link
Member

Choose a reason for hiding this comment

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

This is very minor but in case we want to have multiple crates changing all the crate::* to $crate:* would save some time when moving it later.

Suggested change
impl crate::config::style_edition::StyleEditionDefault for $ty {
impl $crate::config::style_edition::StyleEditionDefault for $ty {

@fee1-dead
Copy link
Member

Also please fix the typo when you next push ("triat" -> "trait")

@ytmimi ytmimi changed the title Add StyleEdition enum and StyleEditionDefault triat Add StyleEdition enum and StyleEditionDefault trait Oct 28, 2023
@ytmimi ytmimi force-pushed the alternative_style_edition_default_trait branch from 84d2d6b to 10ce986 Compare October 28, 2023 20:19
@ytmimi ytmimi requested a review from fee1-dead October 28, 2023 20:21
@ytmimi
Copy link
Contributor Author

ytmimi commented Oct 28, 2023

@fee1-dead thanks again for your initial review. I incorporated your suggestions, and fixed the typo that you caught! Please let me know if there's anything else that you'd like me to change and I'll get on that as soon as I can!

Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +21 to +31
($ty:ident, $config_ty:ty, Edition2024 => $default_2024:expr, _ => $default_2015:expr) => {
impl $crate::config::style_edition::StyleEditionDefault for $ty {
type ConfigType = $config_ty;

fn style_edition_default(
style_edition: $crate::config::StyleEdition,
) -> Self::ConfigType {
match style_edition {
$crate::config::StyleEdition::Edition2015
| $crate::config::StyleEdition::Edition2018
| $crate::config::StyleEdition::Edition2021 => $default_2015,
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should make the first three editions roll up to 2021 as the default 🤔 ?

The style edition RFC and subsequent style team policies have established that in cases where an older edition supports syntax that did not exist/did not have rules in the corresponding version of the style guide/edition then the rules from the oldest style edition that does prescribe rules should be used.

Functionally I do not believe it makes a difference here one way or the other, it's almost a code readability thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we should make the first three editions roll up to 2021 as the default 🤔 ?

Are you suggesting we roll up 2015, 2018, and 2021 enum variants like this:

enum StyleEdition {
    /// There is no difference between the style in the 2015, 2018, and 2021 editions
    /// so they're rolled up to 2021
    Edition2021,
    Edition2024,
}

@ytmimi ytmimi force-pushed the alternative_style_edition_default_trait branch from 10ce986 to 2492f70 Compare December 5, 2023 00:18
**Note** This does not add the `style_edition` config option to rustfmt.
The `StyleEdition` enum will eventually be used to allow users to
configure the `style_edition`, but for now it's added so we can
introduce the the `StyleEditionDefault` trait.
@ytmimi ytmimi force-pushed the alternative_style_edition_default_trait branch from 2492f70 to 02479b9 Compare December 5, 2023 00:20
@calebcartwright calebcartwright merged commit 2174e60 into rust-lang:master Dec 9, 2023
27 checks passed
@ytmimi ytmimi deleted the alternative_style_edition_default_trait branch December 9, 2023 22:20
@ytmimi
Copy link
Contributor Author

ytmimi commented Dec 9, 2023

@calebcartwright thanks for getting this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants