-
Notifications
You must be signed in to change notification settings - Fork 889
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
Add StyleEdition
enum and StyleEditionDefault
trait
#5933
Conversation
3d8a56a
to
84d2d6b
Compare
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"] |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
Lines 417 to 436 in 041f113
/// 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, | |
} |
src/config/style_edition.rs
Outdated
} | ||
} | ||
}; | ||
($ty:ident, $config_ty:ty, $default_2015:expr, $default_2024:expr) => { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
src/config/style_edition.rs
Outdated
#[macro_export] | ||
macro_rules! style_edition_default { | ||
($ty:ident, $config_ty:ty, $default:expr) => { | ||
impl crate::config::style_edition::StyleEditionDefault for $ty { |
There was a problem hiding this comment.
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.
impl crate::config::style_edition::StyleEditionDefault for $ty { | |
impl $crate::config::style_edition::StyleEditionDefault for $ty { |
Also please fix the typo when you next push ("triat" -> "trait") |
StyleEdition
enum and StyleEditionDefault
triatStyleEdition
enum and StyleEditionDefault
trait
84d2d6b
to
10ce986
Compare
@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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
($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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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,
}
10ce986
to
2492f70
Compare
**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.
2492f70
to
02479b9
Compare
@calebcartwright thanks for getting this merged! |
Note This does not add the
style_edition
config option to rustfmt. TheStyleEdition
enum will eventually be used to allow users to configurestyle_edition
, but for now it's added so we can introduce the theStyleEditionDefault
trait.This PR is an alternative to #5854 that doesn't use proc macros.
r? @calebcartwright