-
Notifications
You must be signed in to change notification settings - Fork 898
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
5801 enum variant doc comments are wrapped before comment_width
#6000
Conversation
@IVIURRAY would you mind holding off on creating PRs until the bugfix is ready. You can verify that your code changes work by running the test suite locally with |
@ytmimi yeah that is fine, will avoid raising until finished in the future. |
@IVIURRAY thanks! To clarify, I don't think there's anything wrong with opening draft PRs. However, In this particular case I don't think it's necessary if you're just adding failing test cases. Let me know if you need any pointers for this one. |
Hey @ytmimi - yes, that is totally fine, I can keep the failing tests local until I'm ready to raise an MR 😄 Also, I managed to get this MR passing with the test case from #5801 !! - Let me know if you've comments, I'm not going to say I know exactly how everything is hanging together here as this is my first MR for rustfmt. Please let me know if you want changes... |
@IVIURRAY great! I should have some time later today to review this and provide feedback |
src/items.rs
Outdated
// 1 = ',' | ||
let shape = self.shape().sub_width(1)?; | ||
let shape = self.shape(); |
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 sub_width(1)
is still important for rewriting each enum variant as we need to take the trailing comma into account. Thats what the comment // 1 = ','
was telling us. The bug here is that we're apply sub_width(1)
prematurely. We still need to reduce the width by 1 after we rewrite the attributes as the trailing comma doesn't impact the attributes.
Additionally, I think it would be best if these changes were version gated.
probably best to change this to something like:
let attrs_str = if context.config.version() == Version::Two {
field.attrs.rewrite(&context, shape)?
} else {
// Version::One formatting that was off by 1. See issue #5801
field.attrs.rewrite(&context, shape.sub_width(1)?)?
};
// sub_width(1) to take the trailing comma into account
let shape = shape.sub_width(1)?;
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.
wait, this is only an issue when wrap_comments=true
is set, right? In that case we don't need the version gate since wrap_comments=true
is an unstable option. I'd then expect the code change to look something like:
Edit: Disregard this comment.
let attrs_str = field.attrs.rewrite(&context, shape)?;
// sub_width(1) to take the trailing comma into account
let shape = shape.sub_width(1)?;
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.
As I was thinking about this more I'm now 100% convinced this change needs to be version gated as attribute formatting is also influenced by stable configuration options like attr_fn_like_width
and use_small_heuristics
.
Consider this extended example from what you have in the test case.
pub enum Severity {
/// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines still.
Error,
/// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
Warning,
#[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttsWontWrap,
}
Using a recent nightly rustfmt 1.7.0-nightly (89e2160c 2023-12-27)
with the input above and these configuration options:
comment_width = 120
max_width = 120
wrap_comments = true
version = "One"
attr_fn_like_width = 120
produces this formatting:
pub enum Severity {
/// But here, this comment is 120 columns wide and the formatter wants to split it up onto two separate lines
/// still.
Error,
/// This comment is 119 columns wide and works perfectly. Lorem ipsum. lorem ipsum. lorem ipsum. lorem ipsum lorem.
Warning,
#[something(
AAAAAAAAAAAAA,
BBBBBBBBBBBBBB,
CCCCCCCCCCCCCCCC,
DDDDDDDDDDDDD,
EEEEEEEEEEEE,
FFFFFFFFFFF,
GGGGGGGGGGG
)]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttsWontWrap,
}
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've updated the code to include the version gate. Would you also like me to update the test cases to match what you've as an example here?
c96ad5f
to
ad652ca
Compare
@IVIURRAY great work on this so far. I hope you're getting a better sense of what it's like to work on rustfmt! I'd like you to add two additional test cases, and move the test cases around.
|
Also, probably best to squash all of these commits down into a single commit. Feel free to do that on your end, otherwise I'll handle that when merging. |
c222cfd
to
fb36c14
Compare
@ytmimi ok, I'm a little confused here. My test for /attribute_does_not_wrap_within_max_width.rs is unstable and I don't know why. Running this test in my IDE passes and then fails and then passes. You need to just keep running it a few times. When I pushed from my IDE it passed then running on github actions failed. This caused me to notice the same was happening locally. I'm verey confused as to why this would be. Do you've any insight? Below is the output I get upon a failed Mismatch at tests/source/issue-5801/attribute_does_not_wrap_within_max_width.rs:3:
// rustfmt-attr_fn_like_width: 120
pub enum Severity {
- #[something(AAAAAAAAAAAAA, BBBBBBBBBBBBBB, CCCCCCCCCCCCCCCC, DDDDDDDDDDDDD, EEEEEEEEEEEE, FFFFFFFFFFF, GGGGGGGGGGG)]
+ #[something(
+ AAAAAAAAAAAAA,
+ BBBBBBBBBBBBBB,
+ CCCCCCCCCCCCCCCC,
+ DDDDDDDDDDDDD,
+ EEEEEEEEEEEE,
+ FFFFFFFFFFF,
+ GGGGGGGGGGG
+ )]
AttrsWillWrap,
#[something_else(hhhhhhhhhhhhhhhh, iiiiiiiiiiiiiiii, jjjjjjjjjjjjjjj, kkkkkkkkkkkkk, llllllllllll, mmmmmmmmmmmmmm)]
AttsWontWrap, |
Also, just to let you know I squashed the commits, rebased master and created an |
tests/target/issue-5801/comment_unexpectedly_wraps_before_max_width.rs
Outdated
Show resolved
Hide resolved
I'm just as confused. That shouldn't happen. I wrote a little script to run cargo test over and over again: #!/bin/bash
set -e
for i in {1..20};
do
cargo test -q &>/dev/null
if [ $? -eq 0 ]; then
echo "PASS $i"
else
echo "FAIL $i"
fi
done I'm unable to reproduce the failure locally when running Also, I find it odd that this is failing in CI for the |
tests/target/issue-5801/attribute_unexpectedly_wraps_before_max_width.rs
Outdated
Show resolved
Hide resolved
I've recorded a video of me just running Honestly, I think if we re-run the pipeline it won't be consistent. I think I just got lucky there with some build systems passing. |
@IVIURRAY thanks for the video. I was able to reproduce the error when running the Just to be totally clear, this isn't an issue I want to address in this PR, and I think I need to do some more investigating on my end. To explain further, In most cases setting the configuration values inline with the rustfmt comments isn't an issue because the configuration values don't depend on each other, but in this case all the width configurations depend on the I think the workaround here is to use the I think we can create two new files, let's call them Then in each test file you can replace the inline configs with |
do not change shape as not formatting doc comments correctly sub_width has no impact on tests, also removing add version gate and review comments remove unused variable test unexpected wrapping in version one test version 2 does not wrap remove unneeded config remove unneeded config params adding source test file for version one fix typo ensure ordering of config by using .toml files
8278e4d
to
ef7e05b
Compare
Ah, you genius @ytmimi! - That makes total sense to me, I can totally understand how that is is causing the issue. I thought I had broken something even worse! I've moved the config into a .toml file now and checks are passing 🎉 Would you like me to write this up as an issue?...maybe this could be the next thing I can work on. You might've something else that would be easier for me though, wait to hear. |
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.
Thank you for the kind words @IVIURRAY! I thought this one would be simple, but I couldn't have anticipated all the issues that would have popped up. I'm really glad that you continued to work through them all.
Thank you for your first contribution to rustfmt 🎉
It would be great if you could create a new issue to document the testing problem we ran into. I don't think you need to continue looking into it right now. It's a bit of an edge case that I don't think we'll run into soon. I've got a rough idea on how to correct this problem, but as I mentioned, there are a few things I want to double check first.
I don't have another issue for you off the top of my head, but I'm happy to look through the backlog to see if there's another one that could ease you into the codebase.
Wahoo! - Thanks for the approval and merge @ytmimi, glad I could contribute! I learnt a lot even if most of that was understanding the Rust toolchain 😆 I've created an issue to capture unstable test #6011 I would like another issue to investigate if you've on that you think would be suitable for me 😄 I'll wait to hear from you. |
As long as you learned something then it was all worth it! I appreciate you following up and creating the issue. I'll checkout the backlog over the next day or so and tag you on an issue that I think would be another good one for you to take on! |
Fixes #5801