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

chore: multipart_suggestions for manual_assert #13787

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

scottgerring
Copy link
Contributor

This should address #13099 for the let_unit test.

changelog: [manual_assert]: Updated manual_assert to use multipart_suggestions where appropriate

@rustbot
Copy link
Collaborator

rustbot commented Dec 6, 2024

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 6, 2024
@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from a2618b9 to 09589ff Compare December 6, 2024 09:57
@scottgerring
Copy link
Contributor Author

scottgerring commented Dec 6, 2024

I could use a pointer on this one. We're trying to avoid displaying any comments that are captured within the span including the if/assert block, and only retain them in the tool-applied suggestion:

span_lint_and_then(
cx,
MANUAL_ASSERT,
expr.span,
"only a `panic!` in `if`-then statement",
|diag| {
// comments can be noisy, do not show them to the user
if !comments.is_empty() {
diag.tool_only_span_suggestion(
expr.span.shrink_to_lo(),
"add comments back",
comments,
applicability,
);
}
diag.span_suggestion(expr.span, "try instead", sugg, applicability);
},

I've preserved this here by keeping the tool-only suggestion, and replacing only the span_suggestion with multipart. I believe this serves the intended purpose, but the kind of "undesired upshot" is that we end up with two different outputs from the test framework:

  • manual_assert.editionX.1.fixed - user-facing suggestion only - replaces the if/assert, does not add comment back
  • manual_assert.edition.X.2.fixed - in cases where there is a comment, runs the machine-suggestion only - essentially duplicating the comment, but not replacing the if/assert.

The second case fails the testing, because the if/assert has not been removed.

I think this is the correct behaviour from the runtime perspective, but because the testing framework automatically diverges when there are multiple suggestions, i'm stuck. Options I see:

  • Stop trying to hide the comments from the user
  • Leverage some mechanism to group both machine-only and regular changes together into one multipart suggestion
  • Leverage some mechanism to tell the testing framework to ignore the .2. outputs

Any tips @y21 ?

@y21
Copy link
Member

y21 commented Dec 10, 2024

I suppose there would also be the option to have a single multipart suggestion that removes the if cond { + } and changes the panic! to assert!(cond. Then the comments in between the condition and the assert are naturally preserved and the lint wouldn't need to bother extracting them at all, but maybe that would look confusing. Just including the comments in the same suggestion also sounds fine to me though.

@scottgerring
Copy link
Contributor Author

Cheers @y21 - i'll add them back in and see where we land 🙌

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch 3 times, most recently from bc9e5a6 to e146ead Compare December 11, 2024 10:16
@@ -77,19 +77,25 @@ LL | | panic!("panic with comment") // comment after `panic!`
LL | | }
| |_____^
|
help: try instead
help: replace `if`-then-`panic!` with `assert!`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@y21 here's the change discussed in action with the comment preservation in the UI output

@scottgerring scottgerring marked this pull request as ready for review December 11, 2024 10:48
@scottgerring
Copy link
Contributor Author

Hey @y21 , do you have a chance to look at this? We are inches away from finishing the whole issue 🤣

@y21
Copy link
Member

y21 commented Dec 17, 2024

Sorry for the delay, will try to get to this soon (probably this weekend). I have a bunch of other PRs + other stuff going on at the same time

@scottgerring
Copy link
Contributor Author

Sorry for the delay, will try to get to this soon (probably this weekend). I have a bunch of other PRs + other stuff going on at the same time

Sorry I figured it might’ve fallen through the cracks! It’s probably not a high priority so no stress. Hope you can have a good break!

let suggestions = if comments.is_empty() {
vec![(expr.span, base_sugg.clone())]
} else {
vec![(expr.span.shrink_to_lo(), comments), (expr.span, base_sugg.clone())]
Copy link
Member

Choose a reason for hiding this comment

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

The current suggestion looks a little weird in the diagnostic with the colors
image

I suspect this is because the two replacements are at the same span, but I think it would be nice for this to also be properly indented.

We could probably combine these two replacement strings comments + base_sugg into one, and pass that combined string to the util function reindent_multiline, with the indent obtained from the other util function indent_of(expr.span).

From some local testing that looks like it should be enough
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @y21 good catch! i'm running on low capacity until after the break but i'll polish this up and ping you in the coming weeks 💪

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 31, 2025
@rustbot

This comment has been minimized.

@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from 7a68957 to a805a6f Compare January 31, 2025 14:24
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jan 31, 2025
@scottgerring scottgerring force-pushed the chore/fix-manual-assert branch from a805a6f to 5e448ee Compare January 31, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants