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 diagnostics: revise fn check_access_permissions #51275

Merged

Conversation

pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Jun 1, 2018

NLL: revise fn check_access_permissions so that its (still branchy) shares more code paths between the different cases, and also provide more diagnostics in more cases (though the added diagnostics still do not always meet the quality bar established by AST-borrowck)


Transcribing "checklist" suggested by Niko, except I am rendering it as a table to make it clear that I do not regard every item in the list to be a "must have" for landing this PR.

goal does this PR do it?
no suggestions for ref mut yes
suggestions for direct local assignment ({ let x = 3; x = 4; }) yes (see commits at end)
suggestions for direct field assignment ({ let x = (3, 4); x.0 = 5; } yes (see commits at end)
suggestions for upvars (let x = 3; let c = || { &mut x; }) yes

Note that I added support for a couple of rows via changes that are not strictly part of fn check_access_permissions. If desired I can remove those commits from this PR and leave them for a later PR.

Fix #51031
Fix #51032
(bug #51191 needs a little more investigation before closing.)
Fix #51578

@rust-highfive
Copy link
Collaborator

r? @cramertj

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 1, 2018
@pnkfelix pnkfelix added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 1, 2018
@pnkfelix pnkfelix assigned pnkfelix and unassigned cramertj Jun 1, 2018
}
}

if note_the_place_err && place != the_place_err {
Copy link
Contributor

Choose a reason for hiding this comment

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

I personally would remove this message. If we can identify such a place, we should suggest how to change it to mutable. Otherwise, I suspect it will confuse users as much as anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

My plan is indeed to remove this message once we’ve added all the diagnostic cases of interest

// ... but it doesn't make sense to suggest it on
// variables that are already `ref x` or `ref mut x`
// (such variables are simply not mutable).
if let Some(ty::BindingMode::BindByValue(_)) =
Copy link
Contributor

Choose a reason for hiding this comment

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

In such cases, often the user should have written &mut *foo instead of &mut foo — maybe we can think how to suggest that. (I've seen this before...)

// (such variables are simply not mutable).
if let Some(ty::BindingMode::BindByValue(_)) =
self.mir.local_decls[*local].is_user_variable {
true
Copy link
Contributor

Choose a reason for hiding this comment

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

What about upvars? We should test this example:

let x = (3, );
let c = || x.0 += 1;

and see that we issue a suggestion here to make it let mut x

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, if #51031 does not currently include any examples of upvars, then we should make sure that those cases are being tested... I know I haven't done anything special to account for them ... yet!

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Jun 1, 2018

This check-list is from #51260 — some of these cases I think you handle, but not all...

  • no suggestions for ref mut
  • check on suggestions for other cases ({ let x = 3; x = 4; })
    • oh, that seems to generate a slightly different message...maybe that's ok?
    • { let x = (3, 4); x.0 = 5; } generates a sort-of strange message maybe? (playground — compare NLL and not NLL)
  • suggestions for upvars (let x = 3; let c = || { &mut x; }) (playground)

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 4, 2018

I want to preserve this comment from @nikomatsakis somewhere that won't become outdated by updated to the commit series:

https://github.com/pnkfelix/rust/blob/79e9c3d3fa5ad3b22141968c3a88e44475df75f3/src/librustc_mir/borrow_check/mod.rs#L1795-L1798

In such cases, often the user should have written &mut *foo instead of &mut foo — maybe we can think how to suggest that. (I've seen this before...)

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 4, 2018

Current state is closer but still WIP.

In particular:

  1. I want to try to expand the scope of the PR just a little bit further to cover more diagnostics (mostly so other people know that they should focus on other NLL diagnostic issues not related to this function). I have tried to write notes in all such potential target issues.
  2. The changes to the NLL diagnostics are not quite complete yet and might be acceptable with some further tweaks. For example, in borrowck-borrow-overloaded-deref-mut.rs, AST-borrowck preserves the explicitly-written lifetime in its suggested output, while NLL leaves it out (which is almost certainly going to them yield erroneous code if the developer follows the suggestion literally).

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 4, 2018

(Oh and clearly I will need to rebase too. Which is just as well because I also want to refactor the commit series before actually submitting them for formal review.)

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 4, 2018

So I have some prototype commits that attempt to thread information about explicitly provided types, when present, down into the mir::LocalDecl.

This does seem to be workable in terms of improving the diagnostics for NLL.

But something is wrong with my approach because I think something I did to the compiler data structure has caused the dep-graph tests in compile-fail to start failing.

I'm going to push the commits in case @nikomatsakis has a chance to peek at them. But I also assume that he won't really have any time before I get a chance to start fresh on this tomorrow.

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 4, 2018

Ah i just realized: I bet the problem in the commits I pushed is that I added NodeId’s to the state of the HIR and MIR, and those won’t be stable in the face of other changes to the crate, which messes up DepInfo (right?)

Luckily I don’t need the NodeId; I just had it there in case I wanted to try to use it for something.

(Still, I am curious about what I should be inserting here to capture this state. Or maybe I should just deliberately omit the NodeId from the implementations of things like hash_stable?

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 6, 2018

Okay I've figured out how to use ClearCrossCrate to hide the threaded HirId. Things seem to work.

@pnkfelix
Copy link
Member Author

pnkfelix commented Jun 6, 2018

I think I'm going to make one last push to see if I can thread down the information about the initializer expression span in something like let x = <expr>; (we use that to do things like suggest "maybe use &mut <expr>)

Once that experiment is done (whether it works or not), I'll switch to focusing on cleaning up this PR and getting it ready for review.

@pnkfelix pnkfelix force-pushed the nll-diagnostics-revise-check-access-permissions branch 2 times, most recently from 45ec3c0 to 557f946 Compare June 12, 2018 17:25
@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:09:02]    Compiling proc_macro v0.0.0 (file:///checkout/src/libproc_macro)
[00:09:21]    Compiling syntax_ext v0.0.0 (file:///checkout/src/libsyntax_ext)
[00:15:39]    Compiling rustc_mir v0.0.0 (file:///checkout/src/librustc_mir)
[00:15:39]    Compiling rustc_typeck v0.0.0 (file:///checkout/src/librustc_typeck)
[00:15:41] error[E0425]: cannot find value `syntactic_scope` in this scope
[00:15:41]     --> librustc_mir/build/matches/mod.rs:1160:17
[00:15:41]      |
[00:15:41] 1160 |                 syntactic_scope,
[00:15:41] 
[00:15:41] 
[00:15:45] error[E0560]: struct `rustc::mir::LocalDecl<'tcx>` has no field named `syntactic_scope`
[00:15:45]     --> librustc_mir/build/matches/mod.rs:1160:17
[00:15:45]      |
[00:15:45] 1160 |                 syntactic_scope,
[00:15:45]      |                 ^^^^^^^^^^^^^^^ `rustc::mir::LocalDecl<'tcx>` does not have this field
[00:15:45]      |
[00:15:45]      = note: available fields are: `mutability`, `is_user_variable`, `internal`, `ty`, `name` ... and 2 others
[00:15:52] error: aborting due to 2 previous errors
[00:15:52] 
[00:15:52] Some errors occurred: E0425, E0560.
[00:15:52] For more information about an error, try `rustc --explain E0425`.
[00:15:52] For more information about an error, try `rustc --explain E0425`.
[00:15:52] error: Could not compile `rustc_mir`.
[00:15:52] 
[00:15:52] Caused by:
[00:15:52]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name rustc_mir librustc_mir/lib.rs --color always --error-format json --crate-type dylib --emit=dep-info,link -C prefer-dynamic -C opt-level=2 -C metadata=1f35ea53eb60b9cb -C extra-filename=-1f35ea53eb60b9cb --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/release/deps --extern bitflags=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libbitflags-99b4534960f92449.rlib --extern byteorder=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libbyteorder-78ebc049d4f53f46.rlib --extern serialize=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libserialize-5d0a8a65bb9fe29f.so --extern serialize=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libserialize-5d0a8a65bb9fe29f.rlib --extern log_settings=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/liblog_settings-f16e9eee09cda644.rlib --extern arena=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libarena-ccde2368d50449de.so --extern log=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/liblog-5073f1296cd24b67.rlib --extern rustc_data_structures=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_data_structures-a7df2fc298b4fb06.so --extern rustc=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc-01e98699f9876faa.so --extern graphviz=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libgraphviz-be994de8b3050feb.so --extern syntax_pos=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libsyntax_pos-670f6bac60d99b34.so --extern rustc_errors=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_errors-c9d6678b1c0f0b46.so --extern polonius_engine=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libpolonius_engine-6e0be5cf77966185.rlib --extern rustc_apfloat=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_apfloat-642a7efe79e24835.rlib --extern syntax=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libsyntax-6a2f0731783c2bd3.so --extern either=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/libeither-11fcd35bf55f48b3.rlib --extern rustc_target=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/deps/librustc_target-58741ed9de9aae4f.so -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/build/backtrace-sys-88673787176f9d86/out -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release/build/miniz-sys-62d80197b9ec531e/out` (exit code: 101)
rustlib/x86_64-unknown-linux-gnu
315008 ./src/llvm
249872 ./obj/build/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib
241180 ./src/llvm-emscripten
---
147592 ./.git/modules/src
137992 ./obj/build/bootstrap/debug/incremental
132184 ./obj/build/x86_64-unknown-linux-gnu/stage0-rustc
123420 ./obj/build/bootstrap/debug/incremental/bootstrap-1r3bppl29tbrj
123416 ./obj/build/bootstrap/debug/incremental/bootstrap-1r3bppl29tbrj/s-f1x8fd6lok-6wniw6-bfrkfwrvh0tv
89804 ./src/llvm/test/CodeGen
81432 ./obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu
81428 ./obj/build/x86_64-unknown-linux-gnu/stage0-rustc/x86_64-unknown-linux-gnu/release
72472 ./.git/modules/src/tools
---
travis_time:end:0589127d:start=1528825746652078033,finish=1528825746658101010,duration=6022977
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:12589860
$ 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:0bb7026b
$ 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)

@pnkfelix pnkfelix force-pushed the nll-diagnostics-revise-check-access-permissions branch from 2a1ceb8 to ce9988a Compare June 12, 2018 17:53
@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.
travis_time:start:test_ui_nll
Check compiletest suite=ui mode=ui compare_mode=nll (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
[00:50:29] 
[00:50:29] running 1502 tests
[00:50:34] ...............................................F............................................i.......
[00:50:40] ......................................................i................F............................
[00:50:48] ....................................................................................................
[00:50:52] ....................................................................................................
[00:50:55] ....................................................................................................
[00:51:00] ....................................................................................................
[00:51:00] ....................................................................................................
[00:51:05] ....................................................................................................
[00:51:10] ....................................................................................................
[00:51:16] ....................................................................................................
[00:51:21] ....i..............................................................................i................
[00:51:27] ....................................................................................................
[00:51:32] ....................................................................................................
[00:51:37] ....................................................................................................
[00:51:43] .................i.....................F............................................................
[00:51:44] failures:
[00:51:44] 
[00:51:44] ---- [ui (nll)] ui/borrowck/mut-borrow-of-mut-ref.rs stdout ----
[00:51:44] diff of stderr:
[00:51:44] diff of stderr:
[00:51:44] 
[00:51:44] 1 error[E0596]: cannot borrow immutable item `b` as mutable
[00:51:44] 3    |
[00:51:44] 3    |
[00:51:44] + LL | fn f(b: &mut i32) {
[00:51:44] +    |      - help: consider changing this to be mutable: `mut b`
[00:51:44] 4 LL |     g(&mut b) //~ ERROR cannot borrow
[00:51:44] 5    |       ^^^^^^ cannot borrow as mutable
[00:51:44] 
[00:51:44] 
[00:51:44] The actual stderr differed from the expected stderr.
[00:51:44] The actual stderr differed from the expected stderr.
[00:51:44] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/borrowck/mut-borrow-of-mut-ref.nll/mut-borrow-of-mut-ref.nll.stderr
[00:51:44] To update references, rerun the tests and pass the `--bless` flag
[00:51:44] To only update this specific test, also pass `--test-args borrowck/mut-borrow-of-mut-ref.rs`
[00:51:44] error: 1 errors occurred comparing output.
[00:51:44] status: exit code: 101
[00:51:44] status: exit code: 101
[00:51:44] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/borrowck/mut-borrow-of-mut-ref.nll/a" "-Zborrowck=mir" "-Ztwo-phase-borrows" "-Crpath" "-O" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linuxd","expansion":null}],"children":[],"rendered":null}],"rendered":"error[E0596]: cannot borrow immutable item `b` as mutable\n  --> /checkout/src/test/ui/borrowck/mut-borrow-of-mut-ref.rs:18:7\n   |\nLL | fn f(b: &mut i32) {\n   |      - help: consider changing this to be mutable: `mut b`\nLL |     g(&mut b) //~ ERROR cannot borrow\n   |       ^^^^^^ cannot borrow as mutable\n\n"}
[00:51:44] {"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to previous error\n\n"}
[00:51:44] {"message":"For more information about this error, try `rustc --explain E0596`.","code":null,"level":"","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0596`.\n"}
[00:51:44] ------------------------------------------
[00:51:44] 
[00:51:44] thread '[ui (nll)] ui/borrowck/mut-borrow-of-mut-ref.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3139:9
[00:51:44] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[00:51:44] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[00:51:44] 
[00:51:44] ---- [ui (nll)] ui/did_you_mean/issue-31424.rs stdout ----
[00:51:44] diff of stderr:
[00:51:44] 
[00:51:44] 8   --> $DIR/issue-31424.rs:23:9
[00:51:44] 9    |
[00:51:44] 10 LL |     fn bar(self: &mut Self) {
[00:51:44] -    |            --------------- help: consider changing this to be mutable: `mut self`
[00:51:44] +    |            ---- help: consider changing this to be mutable: `mut self`
[00:51:44] 12 LL |         (&mut self).bar(); //~ ERROR cannot borrow
[00:51:44] 13    |         ^^^^^^^^^^^ cannot borrow as mutable
[00:51:44] 
[00:51:44] 
[00:51:44] The actual stderr differed from the expected stderr.
[00:51:44] The actual stderr differed from the expected stderr.
[00:51:44] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/did_you_mean/issue-31424.nll/issue-31424.nll.stderr
[00:51:44] To update references, rerun the tests and pass the `--bless` flag
[00:51:44] To only update this specific test, also pass `--test-args did_you_mean/issue-31424.rs`
[00:51:44] error: 1 errors occurred comparing output.
[00:51:44] status: exit code: 101
[00:51:44] status: exit code: 101
[00:51:44] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/did_you_mean/issue-31424.rs" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/did_you_mean/issue-31424.nll/a" "-Zborrowck=mir" "-Ztwo-phase-borrows" "-Crpath" "-O" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/did_you_mean/issue-31424.nll/auxiliary" "-A" "unused"
[00:51:44] ------------------------------------------
[00:51:44] 
[00:51:44] ------------------------------------------
[00:51:44] stderr:
[00:51:44] stderr:
[00:51:44] ------------------------------------------
[00:51:44] {"message":"cannot borrow immutable item `self` as mutable","code":{"code":"E0596","explanation":"\nThis error occurs because you tried to mutably borrow a non-mutable variable.\n\nExample of erroneous code:\n\n```compile_fail,E0596\nlet x = 1;\nlet y = &mut x; // error: cannot borrow mutably\n```\n\nIn here, `x` isn't mutable, so light_end":20}],"label":"cannot borrow as mutable","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider changing this to be mutable","code":null,"level":"help","spans":[{"file_name":"/checkout/src/test/ui/did_you_mean/issue-31424.rs","byte_start":758,"byte_end":762,"line_start":22,"line_end":22,"column_start":12,"column_end":16,"is_primary":true,"text":[{"text":"    fn bar(self: &mut Self) {","highlight_start":12,"highlight_end":16}],"label":null,"suggested_replacement":"mut self","suggestion_applicability":"Unspecified","expansion":null}],"children":[],"rendered":null}],"rendered":"error[E0596]: cannot borrow immutable item `self` as mutable\n  --> /checkout/src/test/ui/did_you_mean/issue-31424.rs:23:9\n   |\nLL |     fn bar(self: &mut Self) {\n   |            ---- help: consider changing this to be mutable: `mut self`\nLL |         (&mut self).bar(); //~ ERROR cannot borrow\n   |         ^^^^^^^^^^^ cannot borrow as mutable\n\n"}
[00:51:44] {"message":"aborting due to 2 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 2 previous errors\n\n"}
[00:51:44] {"message":"For more information about this error, try `rustc --explain E0596`.","code":null,"level":"","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0596`.\n"}
[00:51:44] ------------------------------------------
[00:51:44] 
[00:51:44] thread '[ui (nll)] ui/did_you_mean/issue-31424.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3139:9
[00:51:44] 
[00:51:44] 
[00:51:44] ---- [ui (nll)] ui/trivial-bounds-inconsistent-copy-reborrow.rs stdout ----
[00:51:44] diff of stderr:
[00:51:44] 
[00:51:44] 1 error[E0596]: cannot borrow immutable item `**t` as mutable
[00:51:44] 3    |
[00:51:44] 3    |
[00:51:44] + LL | fn reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
[00:51:44] +    |                        --------------- help: consider changing this to be a mutable reference: `&mut &mut i32`
[00:51:44] 4 LL |     *t //~ ERROR
[00:51:44] 5    |     ^^ cannot borrow as mutable
[00:51:44] -    |
[00:51:44] -    = note: the value which is causing this path not to be mutable is...: `*t`
[00:51:44] 8 
[00:51:44] 9 error[E0596]: cannot borrow immutable item `**t` as mutable
[00:51:44] 
[00:51:44] 11    |
[00:51:44] 11    |
[00:51:44] + LL | fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
[00:51:44] +    |                             --------------- help: consider changing this to be a mutable reference: `&mut &mut i32`
[00:51:44] 12 LL |     {*t} //~ ERROR
[00:51:44] 13    |      ^^ cannot borrow as mutable
[00:51:44] -    |
[00:51:44] -    = note: the value which is causing this path not to be mutable is...: `*t`
[00:51:44] 17 error: aborting due to 2 previous errors
[00:51:44] 18 
[00:51:44] 
[00:51:44] 
[00:51:44] 
[00:51:44] The actual stderr differed from the expected stderr.
[00:51:44] Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/trivial-bounds-inconsistent-copy-reborrow.nll/trivial-bounds-inconsistent-copy-reborrow.nll.stderr
[00:51:44] To update references, rerun the tests and pass the `--bless` flag
[00:51:44] To only update this specific test, also pass `--test-args trivial-bounds-inconsistent-copy-reborrow.rs`
[00:51:44] error: 1 errors occurred comparing output.
[00:51:44] status: exit code: 101
[00:51:44] status: exit code: 101
[00:51:44] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/trivial-bounds-inconsistent-copy-reborrow.rs" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/trivial-bounds-inconsistent-copy-reborrow.nll/a" "-Zborrowck=mir" "-Ztwo-phase-borrows" "-Crpath" "-O" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/trivial-bounds-inconsistent-copy-reborrow.nll/auxiliary" "-A" "unused"
[00:51:44] ------------------------------------------
[00:51:44] 
[00:51:44] ------------------------------------------
[00:51:44] stderr:
[00:51:44] stderr:
[00:51:44] ------------------------------------------
[00:51:44] {"message":"cannot borrow immutable item `**t` as mutable","code":{"code":"E0596","explanation":"\nThis error occurs because you tried to mutably borrow a non-mutable variable.\n\nExample of erroneous code:\n\n```compile_fail,E0596\nlet x = 1;\nlet y = &mut x; // error: cannot borrow mutably\n```\n\nIn here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it\nfails. To fix this error, you need to make `x` murow a non-mutable variable.\n\nExample of erroneous code:\n\n```compile_fail,E0596\nlet x = 1;\nlet y = &mut x; // error: cannot borrow mutably\n```\n\nIn here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it\nfails. To fix this error, you need to make `x` mutable:\n\n```\nlet mut x = 1;\nlet y = &mut x; // ok!\n```\n"},"level":"error","spans":[{"file_name":"/checkout/src/test/ui/trivial-bounds-inconsistent-copy-reborrow.rs","byte_start":777,"byte_end":779,"line_start":20,"line_end":20,"column_start":6,"column_end":8,"is_primary":true,"text":[{"text":"    {*t} //~ ERROR","highlight_start":6,"highlight_end":8}],"label":"cannot borrow as mutable","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider changing this to be a mutable reference","code":null,"level":"help","spans":[{"file_name":"/checkout/src/test/ui/trivial-bounds-inconsistent-copy-reborrow.rs","byte_start":714,"byte_end":729,"line_start":19,"line_end":19,"column_start":29,"column_end":44,"is_primary":true,"text":[{"text":"fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {","highlight_start":29,"highlight_end":44}],"label":null,"suggested_replacement":"&mut &mut i32","suggestion_applicability":"Unspecified","expansion":null}],"children":[],"rendered":null}],"rendered":"error[E0596]: cannot borrow immutable item `**t` as mutable\n  --> /checkout/src/test/ui/trivial-bounds-inconsistent-copy-reborrow.rs:20:6\n   |\nLL | fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {\n   |                             --------------- help: consider changing this to be a mutable reference: `&mut &mut i32`\nLL |     {*t} //~ ERROR\n   |      ^^ cannot borrow as mutable\n\n"}
[00:51:44] {"message":"aborting due to 2 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 2 previous errors\n\n"}
[00:51:44] {"message":"For more information about this error, try `rustc --explain E0596`.","code":null,"level":"","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0596`.\n"}
[00:51:44] ------------------------------------------
[00:51:44] 
[00:51:44] thread '[ui (nll)] ui/trivial-bounds-inconsistent-copy-reborrow.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3139:9
[00:51:44] 
---
[00:51:44] 
[00:51:44] thread 'main' panicked at 'Some tests failed', tools/compiletest/src/main.rs:498:22
[00:51:44] 
[00:51:44] 
[00:51:44] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/compiletest" "--compile-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" "--run-lib-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "--rustc-path" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rusd https://google.com | grep ^Date: | sed 's/Date: //g' || true)
travis_fold:start:after_failure.1
travis_time:start:00b4d0ab

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)

@pnkfelix pnkfelix force-pushed the nll-diagnostics-revise-check-access-permissions branch from ce9988a to cb65ef1 Compare June 12, 2018 23:14
@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.
travis_time:start:test_incremental
Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
[01:00:07] 
[01:00:07] running 89 tests
highlight_end":8},{"text":"}","highlight_start":1,"highlight_end":2}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error: `MirOptimized(add_else_branch)` should be clean but is not\n  --> /checkout/src/test/incremental/hashes/if_expressions.rs:110:1\n   |\nLL | / pub fn add_else_branch(x: bool) -> u32 {\nLL | |     let mut ret = 1;\nLL | |\nLL | |     if x {\n...  |\nLL | |     ret\nLL | | }\n   | |_^\n\n"}
[01:00:29] {"message":"`MirValidated(add_else_branch)` should be clean but is not","code":null,"level":"error","spans":[{"file_name":"/checkout/src/test/incremental/hashes/if_expressions.rs","byte_start":2526,"byte_end":2646,"line_start":110,"line_end":119,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"pub fn add_else_branch(x: bool) -> u32 {","highlight_start":1,"highlight_end":41},{"text":"    let mut ret = 1;","highlight_start":1,"highlight_end":21},{"text":"","highlight_start":1,"highlight_end":1},{"text":"    if x {","highlight_start":1,"highlight_end":11},{"text":"        ret = 2;","highlight_start":1,"highlight_end":17},{"text":"    } else {","highlight_start":1,"highlight_end":13},{"text":"    }","highlight_start":1,"highlight_end":6},{"text":"","highlight_start":1,"highlight_end":1},{"text":"    ret","highlight_start":1,"highlight_end":8},{"text":"}","highlight_start":1,"highlight_end":2}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error: `MirValidated(add_else_branch)` should be clean but is not\n  --> /checkout/src/test/incremental/hashes/if_exf_let)` should be clean but is not","code":null,"level":"error","spans":[{"file_name":"/checkout/src/test/incremental/hashes/if_expressions.rs","byte_start":4393,"byte_end":4541,"line_start":207,"line_end":216,"column_start":1,"column_end":2,"is_primary":true,"text":[{"text":"pub fn add_else_branch_if_let(x: Option<u32>) -> u32 {","highlight_start":1,"highlight_end":55},{"text":"    let mut ret = 1;","highlight_start":1,"highlight_end":21},{"text":"","highlight_start":1,"highlight_end":1},{"text":"    if let Some(x) = x {","highlight_start":1,"highlight_end":25},{"text":"        ret = x;","highlight_start":1,"highlight_end":17},{"text":"    } else {","highlight_start":1,"highlight_end":13},{"text":"    }","highlight_start":1,"highlight_end":6},{"text":"","highlight_start":1,"highlight_end":1},{"text":"    ret","highlight_start":1,"highlight_end":8},{"text":"}","highlight_start":1,"highlight_end":2}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error: `MirOptimized(add_else_branch_if_let)` should be clean but is not\n  --> /checkout/src/test/incremental/hashes/if_expressions.rs:207:1\n   |\nLL | / pub fn add_else_branch_if_let(x: Option<u32>) -> u32 {\nLL | |     let mut ret = 1;\nLL | |\nLL | |     if let Some(x) = x {\n...  |\nLL | |     ret\nLL | | }\n   | |_^\n\n"}
[01:00:29] {"message":"aborting due to 4 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 4 previous errors\n\n"}
[01:00:29] ------------------------------------------
[01:00:29] 
[01:00:29] thread '[incremental] incremental/hashes/if_expressions.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:3139:9
[01:00:29] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:00:29] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:00:29] 
[01:00:29] ---- [incremental] incremental/hashes/trait_defs.rs stdout ----
[01:00:29] 
[01:00:29] error in revision `cfail2`: test compilation failed although it shouldn't!
[01:00:29] status: exit code: 101
[01:00:29] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/incremental/hashes/trait_defs.rs" "--target=x86_64-unknown-linux-gnu" "--cfg" "cfail2" "-C" "incremental=/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/hashes/trait_defs/trait_defs.inc" "-Z" "incremental-verify-ich" "-Z" "incremental-queries" "--error-format" "json" "-Zui-testing" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/hashes/trait_defs/a" "-Crpath" "-O" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-Z" "query-dep-graph" "-Zincremental-ignore-spans" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/incremental/hashes/trait_defs/auxiliary"
[01:00:29] ------------------------------------------
[01:00:29] 
[01:00:29] ------------------------------------------
[01:00:29] stderr:
[01:00:29] stderr:
[01:00:29] ------------------------------------------
[01:00:29] {"message":"`Hir(TraitChangeModeSelfOwnToMut::method)` should be clean but is not","code":null,"level":"error","spans":[{"file_name":"/checkout/src/test/incremental/hashes/trait_defs.rs","byte_start":7313,"byte_end":7335,"line_start":277,"line_end":277,6_64-unknown-linux-gnu/stage1-rustc/release/build
34840 ./obj/build/x86_64-unknown-linux-gnu/test/run-pass
34588 ./obj/build/x86_64-unknown-linux-gnu/native/jemalloc/lib
34372 ./obj/build/x86_64-unknown-linux-gnu/doc/core/arch
33884 ./src/llvm-emscripten/lib/Target
---
travis_time:end:26529e85:start=1528849055005141390,finish=1528849055012084695,duration=6943305
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:132a6624
$ 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:0cf47ccd
$ 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)

@bors
Copy link
Contributor

bors commented Jun 19, 2018

💔 Test failed - status-appveyor

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 19, 2018
@kennytm
Copy link
Member

kennytm commented Jun 19, 2018

Legit. Needs to update a UI test result.

---- [ui (nll)] ui\asm-out-assign-imm.rs stdout ----
diff of stderr:
1	error[E0384]: cannot assign twice to immutable variable `x`
-	  --> $DIR/asm-out-assign-imm.rs:31:9
+	  --> $DIR/asm-out-assign-imm.rs:33:9
3	   |
4	LL |     let x: isize;
5	   |         - consider changing this to `mut x`

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 19, 2018
…ypes, down into `mir::LocalDecls`.

As a drive-by: the ref_for_guards created by `fn declare_binding`
should not have been tagged as user_variables in the first
place. These secret internal locals are *pointers* to user variables,
but themselves are not such (IMO. For now at least.)
Namely, we thread down the `HirId` of the explicit type of the
argument.  In the case of the special `self` variable with an implicit
type, we also thread down a description of its structure (`self`/`mut
self`/`&self`/`&mut self`).
… control flow.

As a drive-by, removed some dead-code.
Tried to unify various common code paths and also vaguely approximate
the AST-borrowck diagnostics.

The change in (subjective) quality of diagnostics is not a universal
improvement. But I think this is a better code base to work from
for future fixes.
(Follow-on commits updating the test suite show the resulting changes
to diagnostic output.)
…tests

(since I made this mistake at first but the tests didn't catch it):
we should not suggest adding `mut` to a reassigned `ref` or `ref mut` binding.

(The Rust language, since at least 1.0, does not have `mut ref mut` or
`ref mut mut` etc.)
@pnkfelix pnkfelix force-pushed the nll-diagnostics-revise-check-access-permissions branch from 032eee1 to 4684649 Compare June 19, 2018 17:42
@pnkfelix
Copy link
Member Author

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Jun 19, 2018

📌 Commit 4684649 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 19, 2018
@bors
Copy link
Contributor

bors commented Jun 19, 2018

⌛ Testing commit 4684649 with merge f28c7ae...

bors added a commit that referenced this pull request Jun 19, 2018
…ermissions, r=nikomatsakis

NLL diagnostics: revise `fn check_access_permissions`

NLL: revise `fn check_access_permissions` so that its (still branchy) shares more code paths between the different cases, and also provide more diagnostics in more cases (though the added diagnostics still do not always meet the quality bar established by AST-borrowck)

----

Transcribing "checklist" suggested by Niko, except I am rendering it as a table to make it clear that I do not regard every item in the list to be a "must have" for landing this PR.

goal | does this PR do it?
-----|------------------------------
no suggestions for `ref mut` |  yes
suggestions for direct local assignment (`{ let x = 3; x = 4; }`) | yes (see commits at end)
suggestions for direct field assignment (`{ let x = (3, 4); x.0 = 5; }` | yes (see commits at end)
suggestions for upvars (`let x = 3; let c = \|\| { &mut x; }`) | yes

Note that I added support for a couple of rows via changes that are not strictly part of `fn check_access_permissions`. If desired I can remove those commits from this PR and leave them for a later PR.

Fix #51031
Fix #51032
(bug #51191 needs a little more investigation before closing.)
Fix #51578
@bors
Copy link
Contributor

bors commented Jun 19, 2018

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

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