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: temps in block tail expression diagnostic #54782

Merged
merged 10 commits into from Oct 7, 2018

Conversation

pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Oct 3, 2018

This change adds a diagnostic that explains when temporaries in a block tail expression live longer than block local variables that they borrow, and attempts to suggest turning the tail expresion into a statement (either by adding a semicolon at the end, when its result value is clearly unused, or by introducing a let-binding for the result value and then returning that).

Fix #54556

@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 Oct 3, 2018
@pnkfelix
Copy link
Member Author

pnkfelix commented Oct 3, 2018

r? @nikomatsakis

@@ -91,8 +91,14 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
let source_info = this.source_info(span);
for stmt in stmts {
let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt);
let kind2 = kind.clone();
Copy link
Member Author

Choose a reason for hiding this comment

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

oops this binding of kind2 (and associated clone) wasn't supposed to be part of the PR. Will fix.

@pnkfelix pnkfelix force-pushed the issue-54556-semi-on-tail-diagnostic branch from 57b9ad5 to eb37f5f Compare October 3, 2018 11:44
@pnkfelix pnkfelix changed the title temps in block tail expression diagnostic NLL: temps in block tail expression diagnostic Oct 3, 2018
@pnkfelix pnkfelix added A-NLL Area: Non Lexical Lifetimes (NLL) NLL-diagnostics Working torwads the "diagnostic parity" goal labels Oct 3, 2018
@pnkfelix
Copy link
Member Author

pnkfelix commented Oct 3, 2018

I just realized that I included the updates to the existing tests but forgot to include the unit tests I made specifically for this.

I'll add a commit with them onto the end of this after I sanity-check their output (very soon).

/// expression is ignored by the block's expression context.
///
/// Examples include `{ ...; tail };` and `let _ = { ...; tail };`
/// but not e.g. `let _x = { ...; tail };`
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to explain briefly what this is used for

@nikomatsakis
Copy link
Contributor

@bors r+

So from a technical perspective, I feel good about this. I'm not sure 100% about the wording but it seems like a solid improvement from what we had. Perhaps we can iterate after landing.

@bors
Copy link
Contributor

bors commented Oct 3, 2018

📌 Commit 3b17f1bfe38f6257ea12c7d5e5d236ffcf70615a 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 Oct 3, 2018
@bors
Copy link
Contributor

bors commented Oct 4, 2018

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

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 4, 2018
…bexpr a block tail expression.

Slightly refactored the `LocalDecl` construction API in the process.
…lanation_to_diagnostic`.

(I found it confusing to have calls to an `emit` method in our
error_reporting module where that `emit` method *wasn't* the
`DiagnosticBuffer::emit` method.)
This is preparation for allowing the `BorrowExplanation` carry things
like `mir::Location` or `mir::Local` and still be able to generate
usable diagnostic text.
… temporary r-values.

Changed `BorrowExplanation::UsedLaterWhenDropped` to handle both named
locals and also unnamed (aka temporaries).

If the dropped temporary does not implement `Drop`, then we print its
full type; but when the dropped temporary is itself an ADT `D` that
implements `Drop`, then diagnostic points the user directly at `D`.
@pnkfelix pnkfelix force-pushed the issue-54556-semi-on-tail-diagnostic branch from 3b17f1b to 056cfff Compare October 5, 2018 10:06
@pnkfelix
Copy link
Member Author

pnkfelix commented Oct 5, 2018

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Oct 5, 2018

📌 Commit 704877f 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 Oct 5, 2018
@bors
Copy link
Contributor

bors commented Oct 6, 2018

⌛ Testing commit 704877f with merge ec3d08835a74038c0f549035b37d8b7ec6f42a99...

@bors
Copy link
Contributor

bors commented Oct 6, 2018

💔 Test failed - status-travis

@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 Oct 6, 2018
@rust-highfive
Copy link
Collaborator

The job arm-android 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.
[01:09:18] test [ui] ui/nll/generator-upvar-mutability.rs ... ok
[01:09:18] test [ui] ui/nll/get_default.rs ... ok
[01:09:18] test [ui] ui/nll/generator-distinct-lifetime.rs ... ok
[01:09:18] test [ui] ui/nll/guarantor-issue-46974.rs ... ok
[01:09:19] test [ui] ui/nll/issue-21114-ebfull.rs ... ok
[01:09:19] test [ui] ui/nll/issue-21114-kixunil.rs ... ok
[01:09:19] test [ui] ui/nll/issue-16223.rs ... ok
[01:09:19] test [ui] ui/nll/issue-27868.rs ... ok
[01:09:19] test [ui] ui/nll/issue-31567.rs ... ok
[01:09:19] test [ui] ui/nll/issue-22323-temp-destruction.rs ... ok
---
[01:09:20] test [ui] ui/nll/issue-52669.rs ... ok
[01:09:20] test [ui] ui/nll/issue-52742.rs ... ok
[01:09:20] test [ui] ui/nll/issue-53807.rs ... ok
[01:09:20] test [ui] ui/nll/issue-53119.rs ... ok
[01:09:20] test [ui] ui/nll/issue-54556-niconii.rs ... ok
[01:09:21] test [ui] ui/nll/issue-53570.rs ... ok
[01:09:21] test [ui] ui/nll/issue-54556-stephaneyfx.rs ... ok
[01:09:21] test [ui] ui/nll/issue-54556-temps-in-tail-diagnostic.rs ... ok
[01:09:21] test [ui] ui/nll/issue-54556-used-vs-unused-tails.rs ... ok
[01:09:21] test [ui] ui/nll/issue-54556-wrap-it-up.rs ... ok
[01:09:21] test [ui] ui/nll/loan_ends_mid_block_vec.rs ... ok
[01:09:21] test [ui] ui/nll/match-guards-always-borrow.rs#ast ... ok
[01:09:21] test [ui] ui/nll/match-guards-always-borrow.rs#mir ... ok
[01:09:21] test [ui] ui/nll/match-guards-partially-borrow.rs ... ok
---
[01:12:14] test [ui (nll)] ui/nll/generator-upvar-mutability.rs ... ok
[01:12:14] test [ui (nll)] ui/nll/get_default.rs ... ok
[01:12:14] test [ui (nll)] ui/nll/generator-distinct-lifetime.rs ... ok
[01:12:14] test [ui (nll)] ui/nll/guarantor-issue-46974.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/issue-21114-ebfull.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/issue-21114-kixunil.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/extra-unused-mut.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/issue-27868.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/issue-31567.rs ... ok
[01:12:15] test [ui (nll)] ui/nll/issue-30104.rs ... ok
---
[01:12:16] test [ui (nll)] ui/nll/issue-52742.rs ... ok
[01:12:16] test [ui (nll)] ui/nll/issue-53807.rs ... ok
[01:12:16] test [ui (nll)] ui/nll/issue-53570.rs ... ok
[01:12:16] test [ui (nll)] ui/nll/issue-53119.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/issue-54556-niconii.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/issue-54556-stephaneyfx.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/issue-54556-wrap-it-up.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/issue-54556-temps-in-tail-diagnostic.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/issue-54556-used-vs-unused-tails.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/loan_ends_mid_block_pair.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/loan_ends_mid_block_vec.rs ... ok
[01:12:17] test [ui (nll)] ui/nll/match-guards-always-borrow.rs#mir ... ok
[01:12:17] test [ui (nll)] ui/nll/maybe-initialized-drop-implicit-fragment-drop.rs ... ok
---
[01:43:33] test time::tests::system_time_elapsed ... ok
[01:43:33] test time::tests::system_time_math ... ok
[01:43:38] test sync::mpsc::tests::stress_recv_timeout_two_threads ... ok
[01:43:40] test collections::hash::map::test_map::test_lots_of_insertions ... ok
[01:44:15] test process::tests::test_process_output_fail_to_start ... test process::tests::test_process_output_fail_to_start has been running for over 60 seconds
No output has been received in the last 30m0s, this potentially indicates a stalled build or something wrong with the build itself.
Check the details on how to adjust your build configuration on: https://docs.travis-ci.com/user/common-build-problems/#Build-times-out-because-no-output-was-received
The build has been terminated

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
Copy link
Member Author

pnkfelix commented Oct 6, 2018

@bors retry

@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 Oct 6, 2018
@bors
Copy link
Contributor

bors commented Oct 7, 2018

⌛ Testing commit 704877f with merge dbecb7a...

bors added a commit that referenced this pull request Oct 7, 2018
…r=nikomatsakis

NLL: temps in block tail expression diagnostic

This change adds a diagnostic that explains when temporaries in a block tail expression live longer than block local variables that they borrow, and attempts to suggest turning the tail expresion into a statement (either by adding a semicolon at the end, when its result value is clearly unused, or by introducing a `let`-binding for the result value and then returning that).

Fix #54556
@bors
Copy link
Contributor

bors commented Oct 7, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non Lexical Lifetimes (NLL) NLL-diagnostics Working torwads the "diagnostic parity" goal 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.

None yet

5 participants