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

More accurate spans for arg removal suggestion #106347

Merged
merged 7 commits into from
Feb 16, 2023

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jan 1, 2023

Partially address #106304.

@rustbot
Copy link
Collaborator

rustbot commented Jan 1, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 1, 2023
@rust-log-analyzer

This comment was marked as resolved.

@petrochenkov
Copy link
Contributor

r? rust-lang/compiler
I'd like to take some break from reviewing.

@rustbot rustbot assigned TaKO8Ki and unassigned petrochenkov Jan 2, 2023
@estebank
Copy link
Contributor Author

estebank commented Jan 2, 2023

@petrochenkov Consider removing yourself from the review rotation and readding yourself once you are ready to take them on, just to avoid spam on your end :)

Happy new year.

@Ezrashaw
Copy link
Contributor

Ezrashaw commented Jan 6, 2023

@estebank I have some code for the case where arguments must be added, but that code relies on this code to be merged first.

Also I think I have found a bug with this PR:

fn foo(x: &str) {}

fn main() {
    foo(1, 2, "hello");
}

emits this:

error[E0061]: this function takes 1 argument but 3 arguments were supplied
 --> <anon>:7:5
  |
7 |     foo(1, 2, "hello");
  |     ^^^ -  - argument of type `{integer}` unexpected
  |         |
  |         argument of type `{integer}` unexpected
  |
note: function defined here
 --> <anon>:2:4
  |
2 | fn foo(x: &str) {
  |    ^^^ -------
help: remove the extra arguments
  |
7 -     foo(1, 2, "hello");
7 +     foo(, "hello");
  |

Note the leading comma.

@estebank
Copy link
Contributor Author

estebank commented Jan 6, 2023

Yeah, you're right :-/

Would you have time to try and fix the bug in a subsequent PR?

@Ezrashaw
Copy link
Contributor

Ezrashaw commented Jan 6, 2023

Yeah, I can have a look at this. Honestly the whole error printing bit of that function needs a rewrite, it's all based around the whole-span idea. Ideally we can get this merged soon so that I can create my PRs for the additional suggestion and to fix that bug.

Comment on lines 1124 to 1175
// Incorporate the argument changes in the removal suggestion.
let mut prev = -1;
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
}
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let None = provided_idx
&& let Some((_, arg_span)) = provided_arg_tys.get(idx)
{
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain what this does?

It's breaking my code :(

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by "breaking your code"? Do you have an example of a broken suggestion that we can put as a test?

estebank: I don't really understand what this is doing either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's been a while since I wrote this and now I don't recall exactly what this does, but this weird code was so that I could also incorporate the "change this argument for this other one" suggestions into the removal one, but it is quite messy :-/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The last commit now shows the effect of this block taken out on the tests.

@Ezrashaw
Copy link
Contributor

Ezrashaw commented Jan 9, 2023

@estebank What's happening with this PR? I intend to write some more changes but obviously they rely on this PR. Also could you look at my review comment, thanks!

@estebank
Copy link
Contributor Author

@Ezrashaw this PR is awaiting review. Different reviewers have different schedules when they are available.

| ~~~~~~
LL - 1,
LL + 1
|
Copy link
Contributor

Choose a reason for hiding this comment

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

This suggestion is missing a bit of context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would like to address this in a separate PR. This is a consequence of how the suggestion machinery works. I find it unreasonable to address that as part of an "unrelated" change.

| ~~~~~~~
LL - 1,
LL + 1,
|
Copy link
Contributor

Choose a reason for hiding this comment

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

This one too.

@@ -1098,7 +1153,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some(format!("provide the argument{}", if plural { "s" } else { "" }))
}
SuggestionText::Remove(plural) => {
Some(format!("remove the extra argument{}", if plural { "s" } else { "" }))
err.multipart_suggestion_verbose(
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are just suggesting a removal, is a verbose suggestion the best choice?
From the ui diff when arguments span multiple lines, the terse version may be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to prefer the verbose output in general, because I feel showing the resulting code ends up being clearer, but I can change this to terse, it shouldn't be too much of an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. It's on its own commit so that you can review it.

Comment on lines 1124 to 1175
// Incorporate the argument changes in the removal suggestion.
let mut prev = -1;
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
}
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let None = provided_idx
&& let Some((_, arg_span)) = provided_arg_tys.get(idx)
{
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by "breaking your code"? Do you have an example of a broken suggestion that we can put as a test?

estebank: I don't really understand what this is doing either.

Comment on lines 1127 to 1174
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
}
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let None = provided_idx
&& let Some((_, arg_span)) = provided_arg_tys.get(idx)
{
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
}
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let None = provided_idx
&& let Some((_, arg_span)) = provided_arg_tys.get(idx)
{
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
} else {
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let Some((_, arg_span)) = provided_arg_tys.get(idx) {
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
}

?

| ++
LL - fn oom() -> ! {
LL - loop {}
LL - }
Copy link
Contributor

Choose a reason for hiding this comment

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

This suggestion is weird too. Any idea why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is caused by the macro spans :-/

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs Outdated Show resolved Hide resolved
@estebank estebank force-pushed the removal-suggestion branch 2 times, most recently from 4d85fb2 to 134a305 Compare February 2, 2023 22:23
@TaKO8Ki
Copy link
Member

TaKO8Ki commented Feb 14, 2023

Cloud you rebase the commits?

@estebank
Copy link
Contributor Author

@TaKO8Ki I just rebased and pushed, but the changes were merging cleanly.

@TaKO8Ki
Copy link
Member

TaKO8Ki commented Feb 16, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Feb 16, 2023

📌 Commit dff10d0 has been approved by TaKO8Ki

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 16, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 16, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#106347 (More accurate spans for arg removal suggestion)
 - rust-lang#108057 (Prevent some attributes from being merged with others on reexports)
 - rust-lang#108090 (`if $c:expr { Some($r:expr) } else { None }` =>> `$c.then(|| $r)`)
 - rust-lang#108092 (note issue for feature(packed_bundled_libs))
 - rust-lang#108099 (use chars instead of strings where applicable)
 - rust-lang#108115 (Do not ICE on unmet trait alias bounds)
 - rust-lang#108125 (Add new people to the compiletest review rotation)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f65c6e4 into rust-lang:master Feb 16, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 16, 2023
@estebank estebank deleted the removal-suggestion branch November 9, 2023 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants