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
@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!

@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
Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for pushing this through!

@y21 y21 added this pull request to the merge queue Feb 11, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 11, 2025
@y21
Copy link
Member

y21 commented Feb 11, 2025

Looks like CI is failing with a compile error. The signature of reindent_multiline was changed recently so most likely you'll have to rebase it so see/fix this locally.

Comment on lines +66 to +69
let base_sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");

let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(format!("{comments}{base_sugg}").into(), true, indent);
Copy link
Member

Choose a reason for hiding this comment

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

While we're already here and have to change something anyway: since this base_sugg is only used once we could inline it into the format macro directly to get rid of an allocation

Suggested change
let base_sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(format!("{comments}{base_sugg}").into(), true, indent);
let indent = indent_of(cx, expr.span);
let full_sugg = reindent_multiline(&*format!("{comments}assert!({not}{cond_sugg}, {format_args_snip}){semicolon}"), true, indent);

(may need some formatting changes if the line is too long)


Also, can you move all of this code that prepares/formats the suggestion into the span_lint_and_then closure? The lint is in pedantic, so often the lint isn't enabled and we don't need to emit/prepare any suggestion as the closure won't be called.

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