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

Disable ref hint for pattern in let and adding ui tests #40402 #41564

Merged
merged 2 commits into from
Apr 29, 2017

Conversation

gaurikholkar-zz
Copy link

@gaurikholkar-zz gaurikholkar-zz commented Apr 26, 2017

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.

@rust-highfive
Copy link
Collaborator

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.

@nikomatsakis
Copy link
Contributor

r? @nikomatsakis

Copy link
Contributor

@nikomatsakis nikomatsakis left a 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.

@@ -95,6 +95,14 @@ enum MapEntry<'hir> {
RootCrate,
}

/// Represents the kind of pattern
#[derive(Debug, Clone, Copy)]
pub enum PatternSource<'hir> {
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 move this struct into gather_move.rs as well?

span_path_opt: Option<MoveSpanAndPath<'tcx>>
}

/// Returns the kind of the Pattern
Copy link
Contributor

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){
Copy link
Contributor

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) {).

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Author

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

@gaurikholkar-zz gaurikholkar-zz changed the title Disable ref hint for pattern in let and adding ui tests. Disable ref hint for pattern in let and adding ui tests #40402 Apr 26, 2017
if let Some(pattern_source) = error.move_to_places.get(0){

match pattern_source.pat_source {
PatternSource::LetDecl(_) => {}
Copy link
Contributor

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.

Copy link
Contributor

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

@nikomatsakis nikomatsakis added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 27, 2017
@nikomatsakis
Copy link
Contributor

@gaurikholkar let me know when you have updates :)

@bors
Copy link
Contributor

bors commented Apr 28, 2017

☔ The latest upstream changes (presumably #41591) made this pull request unmergeable. Please resolve the merge conflicts.

@gaurikholkar-zz
Copy link
Author

@nikomatsakis Updated the PR, @bors resolved the merge conflicts for #41591

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 28, 2017

📌 Commit ab5d16a has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Apr 29, 2017

⌛ Testing commit ab5d16a with merge a618d5b...

@bors
Copy link
Contributor

bors commented Apr 29, 2017

💔 Test failed - status-travis

@Mark-Simulacrum
Copy link
Member

@bors
Copy link
Contributor

bors commented Apr 29, 2017

⌛ Testing commit ab5d16a with merge b9e9a03...

bors added a commit that referenced this pull request Apr 29, 2017
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.
@bors
Copy link
Contributor

bors commented Apr 29, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing b9e9a03 to master...

@bors bors merged commit ab5d16a into rust-lang:master Apr 29, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this pull request May 3, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants