-
Notifications
You must be signed in to change notification settings - Fork 323
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
Update Rust style guide #3172
Update Rust style guide #3172
Conversation
Thank you, I love this changes! I would like to have answers to the following questions:
|
- **Use one import per line.** | ||
Do not use the `use crate::mod_name::{name1, name2, name3}` syntax. For each entity use a separate `use` statement. |
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 enforced by the rustfmt
, likely no need to include this here,
- **Group related submodule and import statements** | ||
Submodule and import statements should be divided into several groups separated by blank lines. Items in the groups should be sorted alphabetically (do not use group comments in your code): |
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 not what I've been already doing nor what I've been requesting in my reviews, so I think it actually warrants some discussion.
Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.
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.
Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.
But the enso-org/ide is archived, right? That strongly suggests, that the guidelines there should not be used anymore.
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.
Ouch, wrong link.
The one I've meant is https://github.com/enso-org/enso/blob/develop/docs/style-guide/rust.md
Issue is that we have two Rust style guide documents.
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.
The idea is to remove the second style guide ASAP and make this the only one.
- **Don't use relative imports.** | ||
Do not use `super::` nor `self::` imports. Use absolute imports or imports from local submodules only. |
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'd add an exception for "inlined" modules, or at least for the "test" module.
// Reexports. | ||
pub use geometry::*; |
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'd put reexports as the last group: sometimes you reexport an imported thing (not necessarily from a submodule). Additionally, we have then a clear place where imports ends, and visible names starts (reexports, and then definitions).
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.
Publicly available things should be next to each other, so, re-exports should ld be next to sub-modules. Otherwise, you need to brwose different parts of the files in order to understand what are the publicly exposed entities in the file.
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.
But still, the definitions are also publicly available. So in the current state you have:
- public submodules
- public reexports
- non-public imports
- public definitions
That's why I opted for moving 2 after 3.
On the other hand... imports might be treated as part of 4 actually, so let's stay with your ordering!
// Prelude-like imports. | ||
use crate::prelude::*; | ||
use sprite_system::traits::*; | ||
|
||
// Local-crate imports. | ||
use crate::closure; | ||
use crate::data::opt_vec::OptVec; | ||
use crate::data::dirty; | ||
use crate::system::web::group; | ||
|
||
// External imports. | ||
use nalgebra::Matrix4; | ||
use nalgebra::Vector3; |
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.
What prelude-like means? Everything with star at the end?
By the way, once we have rust formatter, I wonder if we could keep imports in a single block, sorted alphabetically. Personally, I've never taken an advantage of having imports separated in blocks. But having a single block allows me to make use of Intellij auto-import feature + that makes mass-refactoring easier as well.
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.
Let's chat about it. I think this needs a more interactive discussion.
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.
Did you end up talking about this? If so, what was the result of the discussion?
- **Group related submodule and import statements** | ||
Submodule and import statements should be divided into several groups separated by blank lines. Items in the groups should be sorted alphabetically (do not use group comments in your code): |
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.
Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.
But the enso-org/ide is archived, right? That strongly suggests, that the guidelines there should not be used anymore.
@vitvakatu, good points! I have a few more, based on yesterdays PR review, I plan to add these + answers to your questions somewhat today / tomorrow evening! |
@wdanilo This has been open for a while without change. What is the state of the updates? |
Another thing that I faced - we do not have any explicit requirements for code formatting inside macros (like FRP macros or |
Everyone, I apologise for making this waiting so long. I addressed the comments. @vitvakatu, regarding your points 1-3 (the first comment), let's create separate PRs with the changes there and let's discuss these things on Discord first. |
Do not use `super::` nor `self::` imports in files (you can use them in localy defined modules). Use absolute imports or imports from local submodules only. | ||
|
||
- **Use Enso Formatter to format your imports** | ||
Run the `build/enso-formatter` script to format imports in all files before contributing your PR. |
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 we add a copy-pastable command for running this from the project root?
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.
Yeah, that would be nice. I use cargo run -p enso-formatter
for that. Even nicer would be to integrate it with our ./run lint
tool, but I remember there were some problems with that.
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 the changes in this PR are rather subtle and do not answer a lot of practical questions around our code style, but let's discuss them separately as this PR is here for a few months now.
|
||
## Styling rules | ||
## Code formatting in macros. | ||
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same was as `rustfmt` would do it. |
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.
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same was as `rustfmt` would do it. | |
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same way as `rustfmt` would do it. |
However, it's not clear how "rustfmt
would do it" for non-Rust syntax, like our FRP definitions.
Do not use `super::` nor `self::` imports in files (you can use them in localy defined modules). Use absolute imports or imports from local submodules only. | ||
|
||
- **Use Enso Formatter to format your imports** | ||
Run the `build/enso-formatter` script to format imports in all files before contributing your PR. |
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.
Yeah, that would be nice. I use cargo run -p enso-formatter
for that. Even nicer would be to integrate it with our ./run lint
tool, but I remember there were some problems with that.
I've created a bunch of small updates to the style guide which describe things that we already use in the codebase. Is there anything missing, or is something not clear enough? If so, please write comments about that and I'll fix it.