-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Newlines make diagnostic fix suggestions render strangely #92741
Comments
cc @estebank -- I think you wrote the code that generates these diffs? |
I did. I guess what we could do is detect this case in particular and show only the |
@rustbot claim Perhaps I'll try this out :) Which part of the code would need to be changed to only show the |
@camelid I bet the code is somewhere around here https://github.com/rust-lang/rust/blob/master/compiler/rustc_errors/src/emitter.rs#L1671 haven't looked much into the rendering, but a theory: Otherwise maybe we just need to suppress emitting |
@compiler-errors Thanks for the suggestion! I tried trimming newlines before calling Some results from investigating: The span for the substitution with |
@rustbot claim |
Hey! I took a look at this and I have found some things, but I don't see a clear path to fix it. @camelid was right that the span seems to be wrong. It takes the line with the ampersand, but also the indentation of the next line. The code that produces that span is in rustc_typeck. After experimenting a bit, I understood why the span is like that. It tries to remove the part of a The problem is, that even using that span, the suggestion we are getting is wrong. I would expect something like this:
Maybe that isn't ideal, but it makes sense (at least to me), as the new line takes the whitespace from the first one. However, this doesn't happen and I'm having difficulties finding where the error is in the code. I have found that in this line So, to sum up:
|
I think that fixing the rendering of the span is the appropriate solution. We could infer for example that the only changes that are additions (rendered with a + next to the line number) are purely whitespace, and then not render it at all. I think that would convey the "delete this line" meaning well enough. |
I'm not sure I understand. The addition that appears in the diagnostic isn't only whitespace. If we remove the whole span, the line 2 is another almost entirely different line (in fact, the only thing that doesn't change from the compiler point of view is precisely the whitespace). I think the root of the problem is that right now the compiler has trouble when rendering removals that span more than one line. The problem is even worse if there are more lines involved:
I don't think this can be fixed without restructuring at least part of the rendering, but I've found the code to be really difficult to understand (I guess there are lots of edge cases for things like this). |
@CastilloDel, the patch rendering of the suggested change would have been - &
- mut
- if true { true } else { false }
+ if true { true } else { false } because the span that is being removed (and hence the lines that are being modified) is
which puts the start of the Does this make sense? |
Yes! That makes sense. The problem is that what is actually happening in the code is that it is inferring that it should substitute the |
Given the following code: playground
The current output is:
What's wrong
The suggestion looks weird, seeming to suggest removing
&
and replacing it with a duplicate of the line that follows it.The actual fixup'd code (via
cargo fix
) isn't actually broken.cargo fix
does the change as expected, removing just the&
. So this leads me to believe this is instead just a bug in the code that renders suggestions.@camelid spotted this in #91545 but this is not related to that PR.
The text was updated successfully, but these errors were encountered: