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

[NLL] Better move errors #51729

Merged
merged 4 commits into from
Jun 29, 2018
Merged

[NLL] Better move errors #51729

merged 4 commits into from
Jun 29, 2018

Conversation

matthewjasper
Copy link
Contributor

@matthewjasper matthewjasper commented Jun 23, 2018

Make a number of changes to improve the quality of NLL cannot move errors.

  • Group errors that occur in the same match with the same cause.
  • Suggest ref, & or removing * to avoid the move.
  • Show the place being matched on.

Differences from AST borrowck:

  • & is suggested over ref when matching on a place that can't be moved from.
  • Removing * is suggested instead of adding & when applicable.
  • Sub-pattern spans aren't used, this would probably need Spans on Places.

Closes #45699
Closes #46627
Closes #51187
Closes #51189

r? @pnkfelix

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 23, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.

[00:04:40] travis_fold:start:tidy
travis_time:start:tidy
tidy check
[00:04:41] tidy error: /checkout/src/librustc_mir/borrow_check/move_errors.rs:100: line longer than 100 chars
[00:04:41] tidy error: /checkout/src/librustc_mir/borrow_check/move_errors.rs:213: line longer than 100 chars
[00:04:41] tidy error: /checkout/src/librustc_mir/borrow_check/move_errors.rs:225: line longer than 100 chars
[00:04:41] tidy error: /checkout/src/librustc_mir/borrow_check/move_errors.rs: incorrect license
[00:04:42] some tidy checks failed
[00:04:42] 
[00:04:42] 
[00:04:42] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor" "--quiet"
[00:04:42] 
[00:04:42] 
[00:04:42] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:04:42] Build completed unsuccessfully in 0:01:45
[00:04:42] Build completed unsuccessfully in 0:01:45
[00:04:42] make: *** [tidy] Error 1
[00:04:42] Makefile:79: recipe for target 'tidy' failed

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:25a07bb2
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:0c6b1f55:start=1529754252697697590,finish=1529754252705203394,duration=7505804
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:046fc188
$ head -30 ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
head: cannot open ‘./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers’ for reading: No such file or directory
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:04d6a78a
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:56:35] 
[00:56:35] running 2426 tests
[00:56:39] ....................................................................................................
[00:56:44] ....................................................................................................
[00:56:49] .....................................................F..............................................
[00:57:01] ......................................................................................i.............
[00:57:07] ...............................................ii.iii...............................................
[00:57:12] ..............................................................................i.....................
[00:57:17] .......................i............................................................................

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@matthewjasper matthewjasper force-pushed the move-errors branch 2 times, most recently from e6b8f1a to 9da2fa6 Compare June 23, 2018 22:14
@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.

Left a bunch of nits, but this is really quite nice. 💯

// in the case of box expr there is no such check.
if let Place::Projection(..) = destination {
this.local_decls.push(LocalDecl::new_temp(expr.ty, expr.span));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

With #51139, I don't think this should be needed, right?

@@ -44,6 +44,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
arms: Vec<Arm<'tcx>>)
-> BlockAnd<()> {
let tcx = self.hir.tcx();
let discriminant_span = match discriminant {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: seems like we should move this to a helper method on ExprRef

@@ -288,6 +298,20 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
candidate.match_pairs);
}

if let Some(span) = initializer_span {
Copy link
Contributor

Choose a reason for hiding this comment

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

it feels to me like a comment would be nice here

@@ -0,0 +1,364 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for making a module <3

}

#[derive(Debug)]
enum GroupedMoveError<'tcx> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like a meta comment on the enum explaining (in general) what we group by and why would be super useful. e.g.


often when desugaring a pattern match we may have many individual moves in MIR
that are all part of one operation from the user's point-of-view. For example:

let (x, y) = foo()

would move x from the 0 field of some temporary, and y from the 1 field. We group such errors together for cleaner error reporting.

#[derive(Debug)]
enum GroupedMoveError<'tcx> {
// Match place can't be moved from
// e.g. Matching on x[0].
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 make this rust code? I guess this means match x[0] { ... } where x has type &[u32] or something?

Copy link
Contributor

Choose a reason for hiding this comment

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

(Of course the same is true for &Vec...)

binds_to: Vec<Local>,
},
// Part of a pattern can't be moved from,
// e.g. moving from a &x pattern.
Copy link
Contributor

Choose a reason for hiding this comment

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

similarly, I would say e.g. let &x = ... -- here x references borrowed content, so a move would be illegal

opt_ty_info: _,
}))) = local_decl.is_user_variable
{
let (match_place, match_span) = opt_match_place
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see some sort of comment explaining what these represent in terms of Rust source (e.g., give a Rust example, and explain what match_place and match_span would be)

.map(|&(ref x, y)| (x, y))
.unwrap_or((move_from, stmt_source_info.span));

if self
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't quite get why this sub_scope test makes sense... example of where it applies (and maybe where it doesn't?)

@matthewjasper
Copy link
Contributor Author

Nits addressed

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jun 28, 2018

📌 Commit 2cb0a06 has been approved by nikomatsakis

@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 Jun 28, 2018
@Eh2406 Eh2406 mentioned this pull request Jun 28, 2018
@bors
Copy link
Contributor

bors commented Jun 29, 2018

⌛ Testing commit 2cb0a06 with merge ab8a67c...

bors added a commit that referenced this pull request Jun 29, 2018
[NLL] Better move errors

Make a number of changes to improve the quality of NLL cannot move errors.

* Group errors that occur in the same `match` with the same cause.
* Suggest `ref`, `&` or removing `*` to avoid the move.
* Show the place being matched on.

Differences from AST borrowck:

* `&` is suggested over `ref` when matching on a place that can't be moved from.
* Removing `*` is suggested instead of adding `&` when applicable.
* Sub-pattern spans aren't used, this would probably need Spans on Places.

Closes #45699
Closes #46627
Closes #51187
Closes #51189

r? @pnkfelix
@bors
Copy link
Contributor

bors commented Jun 29, 2018

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

@bors bors merged commit 2cb0a06 into rust-lang:master Jun 29, 2018
@matthewjasper matthewjasper deleted the move-errors branch September 3, 2018 21:32
@@ -36,7 +36,7 @@ fn foo<T: Copy>(_t: T, q: i32) -> i32 {
// _5 = (move _6, move _7);
// _8 = move (_5.0: i32);
// _9 = move (_5.1: i32);
// _0 = move _8;
// _0 = _8;
Copy link
Member

Choose a reason for hiding this comment

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

I am very confused as to why this happened. Why was it a move before and not after?

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants