-
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
Disable ref hint for pattern in let and adding ui tests #40402 #41564
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @arielb1 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @gaurikholkar! I have a few nits and style suggestions to be addressed.
src/librustc/hir/map/mod.rs
Outdated
@@ -95,6 +95,14 @@ enum MapEntry<'hir> { | |||
RootCrate, | |||
} | |||
|
|||
/// Represents the kind of pattern | |||
#[derive(Debug, Clone, Copy)] | |||
pub enum PatternSource<'hir> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this struct into gather_move.rs
as well?
span_path_opt: Option<MoveSpanAndPath<'tcx>> | ||
} | ||
|
||
/// Returns the kind of the Pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a slightly bigger comment would be good. Something like:
/// Analyzes the context where the pattern appears to determine what
/// sort of hint we want to give. In particular, if the pattern is in a `match`
/// or nested within other patterns, we want to suggest a `ref` binding:
///
/// let (a, b) = v[0]; // like the `a` and `b` patterns here
/// match v[0] { a => ... } // or the `a` pattern here
///
/// But if the pattern is the outermost pattern in a `let`, we would rather
/// suggest that the author add a `&` to the initializer:
///
/// let x = v[0]; // suggest `&v[0]` here
///
/// In this latter case, this function will return `PatternSource::LetDecl`
/// with a reference to the let.
move_to.name, is_first_note); | ||
is_first_note = false; | ||
|
||
if let Some(pattern_source) = error.move_to_places.get(0){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the indentation is funny here. The if
should be aligned with the let
above, and I don't think you need the blank lines here. Also, the final brace should have a space (i.e., replace get(0){
with get(0) {
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think I'd just remove the if let
and merge it with the match
. So something like this:
match error.move_to_places.get(0) {
Some(MoveSpanAndPath { pat_source: PatternSource::LetDecl(_), .. }) => {
// ignore patterns that are found at the top-level of a `let`;
// see `get_pattern_source()` for details
}
_ => {
for move_to in &error.move_to_places {
err = note_move_destination(err, move_to.span, move_to.name, is_first_note);
is_first_note = false;
}
}
}
This might read nicer still if we changed the name of the MoveSpanAndPath
struct into something a bit shorter, like Place
or MovePlace
.
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to put a comment at the top of this file explaining the purpose of the test:
// Check that we do not suggest `ref f` here in the `main()` function.
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for this test:
// Check that we do suggest `(ref a, ref b)` here, since `a` and `b`
// are nested within a pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do the necessary changes
if let Some(pattern_source) = error.move_to_places.get(0){ | ||
|
||
match pattern_source.pat_source { | ||
PatternSource::LetDecl(_) => {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you store the expression on the rhs of the assignment in the LetDecl variant, then you can suggest to add an ampersand before the expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk yeah, we are planning for a follow-up PR
@gaurikholkar let me know when you have updates :) |
☔ The latest upstream changes (presumably #41591) made this pull request unmergeable. Please resolve the merge conflicts. |
@nikomatsakis Updated the PR, @bors resolved the merge conflicts for #41591 |
@bors r+ |
📌 Commit ab5d16a has been approved by |
⌛ Testing commit ab5d16a with merge a618d5b... |
💔 Test failed - status-travis |
Disable ref hint for pattern in let and adding ui tests #40402 A fix to #40402 The `to prevent move, use ref e or ref mut e ` has been disabled. ``` fn main() { let v = vec![String::from("oh no")]; let e = v[0]; } ``` now gives ``` error[E0507]: cannot move out of indexed content --> example.rs:4:13 | 4 | let e = v[0]; | ^^^^ cannot move out of indexed content error: aborting due to previous error ``` I have added ui tests for the same and also modified a compile-fail test.
☀️ Test successful - status-appveyor, status-travis |
Consider changing to & for let bindings rust-lang#40402 This is a fix for rust-lang#40402 For the example ``` fn main() { let v = vec![String::from("oh no")]; let e = v[0]; } ``` It gives ``` error[E0507]: cannot move out of indexed content --> ex1.rs:4:13 | 4 | let e = v[0]; | ^^^^ cannot move out of indexed content | = help: consider changing to `&v[0]` error: aborting due to previous error ``` Another alternative is ``` error[E0507]: cannot move out of indexed content --> ex1.rs:4:13 | 4 | let e = v[0]; | ^^^^ consider changing to `&v[0]` error: aborting due to previous error ``` Also refer to rust-lang#41564 for more details. r? @nikomatsakis
A fix to #40402
The
to prevent move, use ref e or ref mut e
has been disabled.now gives
I have added ui tests for the same and also modified a compile-fail test.