-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Incorrect borrowing suggestion for range literals #54505
Comments
If someone wants to tackle this issue, it should be straightforward to fix. In: This issue affects the other range syntaxes too, so those should also be fixed (although these are not |
Hi @varkor, I'd like to tackle this and take the plunge into the Rust compiler code :) |
@pawroman: great! Let me know if you hit any snags or need any more tips. You'll want to add tests for these (in |
@varkor you've anticipated most of my newbie questions already :) Thanks for that info, very useful! I'm now in the process of stressing my PC with lots of compiling to actually get started. Skimming the test code I'm really impressed by how easy it is to add "UI" tests for these sorts of things (compile this, expect this error on this line). Snazzy! That's all great, but do you anticipate I should also add "deeper" tests? I guess I'm trying to understand if the scope of this is just pure UI or do I need to touch some syntax parsing/immediate representations, since you mentioned code in Because the compiler is emitting a syntax suggestion, we might want to assert that the suggestion is correct syntax in the first place. I don't know if that's how deep the rabbit hole goes and if there's a facility to help out, but my hunch is that since we have all of the compiler at hand, why not use it to validate the suggestion syntax? EDIT: |
The issue here is just a diagnostic issue, so checking that we're getting the updated hint is enough. The code in
It would technically be possible to do something like this, but it would probably be more trouble than it's worth. (I'm not aware of a facility to do this easily already.) I think manually going through the possible
A simple UI test should be sufficient here: |
Thanks for your guidance @varkor and @estebank, much appreciated. I have dug into HIR and built-in ranges a bit more. Decided to check all the built-in range types and concluded they are all indeed affected by this problem. The following test I came up with demonstrates my intended fix (fails with nightly as well as stage 1 compiled from master): https://gist.github.com/pawroman/6ed825d66c0f8d910b99ce3cf22cd4d9 None of the fix suggestions, when applied, constitute valid syntax. My test checks for syntax I believe should be suggested by the compiler. I'm keen to tackle all of the range types at once in the scope of this issue. The problem I see though is just how wildly different each of the range types is represented in HIR. Adding a rust/src/librustc_typeck/check/demand.rs Line 311 in 992d1e4
Revealed the following:
I'm quite bewildered by this non-uniformity and I guess I need some help unifying all of these into a elegant condition to tell "is this a built-in range?". I could probably brute-force it in a Thanks in advance! EDIT realized that this non-uniformity was actually mentioned in the second comment from @varkor , but my plea still stands. Thanks for your patience. |
I think you can match on all of these with something along the lines of (pseudocode):
|
Right, that's what I had in mind, but was also wondering if something like a check for I guess we want to match against built-in range literals though, and explicitly. Here's how I rationalize it: because they are parsed and desugared by the compiler, the check needs to be explicit and hardcode all paths to builtin ranges. Handle a special case with a special case. |
Yes, this is where the lowering code comes in handy, because you can see exactly how the different syntax is desugared differently, to make sure you really are catching all the cases.
Yes, I agree. It'd be nice if there was a more uniform way to handle them, but it's not convenient at the moment. You could include a reference back to |
All clear now, thanks. I have succeeded in matching all range types and getting the test to pass. Needs some cleanups, but hopefully should be ready for PR soon. I have a few more questions before that though 😄 I have noticed that most of my code would actually be irrelevant if I re-used functions and constants defined in Can't deny that I have used these as inspiration / guideline (albeit I did not do a stupid copy-paste). What's the policy on using clippy code (clippy is a submodule of main repo)? This is purely in the interest of pursuing DRY. The diff is not that big (52 new lines added to Another conundrum I have is whether we should address |
Unfortunately, there's not much sharing from clippy to rustc. I wouldn't worry about it. If you can encapsulate it easily, it's possible that clippy could make use of the code in rustc instead.
The |
Thanks for the suggestion for It seems to me they are disjoint -- in rust/src/librustc/hir/lowering.rs Lines 4756 to 4768 in 7e7bc06
The Another problem I encountered while writing tests is that the changes I have made will affect suggestions for code like this: use std::ops::RangeBounds;
fn take_range(_r: &impl RangeBounds<i8>) {}
fn main() {
take_range(::std::ops::Range { start: 0, end: 1 });
} With my changes, the suggestion would be This is due to not differentiating between "de-sugared" form coming from With this in mind, I think current code is not PR worthy just yet. If of interest, here's the branch: https://github.com/pawroman/rust/tree/fix_range_borrowing_suggestion |
Left a couple of comments on possible approaches on that commit. The code looks fine. |
…tion, r=varkor Fix range literals borrowing suggestions Fixes rust-lang#54505. The compiler issued incorrect range borrowing suggestions (missing `()` around borrows of range literals). This was not correct syntax (see the issue for an example). With changes in this PR, this is fixed for all types of `Range` literals. Thanks again to @varkor and @estebank for their invaluable help and guidance. r? @varkor
Fix range literals borrowing suggestions Fixes #54505. The compiler issued incorrect range borrowing suggestions (missing `()` around borrows of range literals). This was not correct syntax (see the issue for an example). With changes in this PR, this is fixed for all types of `Range` literals. Thanks again to @varkor and @estebank for their invaluable help and guidance. r? @varkor
Fix range literals borrowing suggestions Fixes #54505. The compiler issued incorrect range borrowing suggestions (missing `()` around borrows of range literals). This was not correct syntax (see the issue for an example). With changes in this PR, this is fixed for all types of `Range` literals. Thanks again to @varkor and @estebank for their invaluable help and guidance. r? @varkor
&0..=1
is an incorrect suggestion because&
has higher precedence than..=
. It should suggest&(0..=1)
.The text was updated successfully, but these errors were encountered: