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: disallow creation of immediately unusable variables #54188

Merged
merged 9 commits into from
Sep 22, 2018

Conversation

lqd
Copy link
Member

@lqd lqd commented Sep 13, 2018

Fix #53695

Original description follows


This WIP PR is for discussing the impact of fixing #53695 by injecting a fake read in let patterns.

(Travis will fail, at least the mir-opt suite is failing in its current state)

@rust-highfive
Copy link
Collaborator

r? @zackmdavis

(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 Sep 13, 2018
@memoryruins memoryruins added the A-NLL Area: Non-lexical lifetimes (NLL) label Sep 13, 2018
@lqd
Copy link
Member Author

lqd commented Sep 13, 2018

r? @nikomatsakis

@rust-highfive

This comment has been minimized.

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.

What do you think @lqd -- up for slightly more invasive edits?

@@ -10,15 +10,16 @@ LL | };
error[E0597]: `a` does not live long enough
--> $DIR/borrowing.rs:24:9
|
LL | let _b = {
| -- borrow later used here
Copy link
Contributor

Choose a reason for hiding this comment

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

So this message is generated here:

Given that we have the location in the MIR in the location variable, it would be quite trivial for us to determine if this is a ReadForMatch statement. The only catch is that we use this same statement both for matches and for this. If we refactor ReadForMatch to be something like this:

FakeRead(FakeReadCause, Place<'tcx>),

with

enum FakeReadCause {
    ForMatch,
    ForLet,
}

then we could match on this particular case and give a different message. Perhaps something like

-- borrowed value is then later stored into this variable

@rust-highfive

This comment has been minimized.

@lqd lqd force-pushed the fallout-53695 branch 2 times, most recently from 08f419b to 9f76655 Compare September 14, 2018 19:27
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 minor nit, but this looks good!

#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum FakeReadCause {
ForMatch,
ForLet,
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add some comments and links here.

I think the best option is to move the comments from the point of insertion to here, and then (in those spots) just refer to

// See comments on `FakeReadCause::ForLet

This is also good because we may insert fake read from multiple places, but this centralizes the comment in one clearly identifiable location.

// Check if the location represents a `FakeRead`, and adapt the error
// message to the `FakeReadCause` it is from: in particular,
// the ones inserted in optimized `let var = <expr>` patterns.
let is_fake_read_for_let = match self.mir.basic_blocks()[location.block]
Copy link
Contributor

Choose a reason for hiding this comment

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

extract to a helper function...somewhere? lots of random code for this.

// but in some cases it can affect the borrow checker, as in #53695.
// Therefore, we insert a "fake read" here to ensure that we get
// appropriate errors.
//
Copy link
Contributor

Choose a reason for hiding this comment

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

this is the comment I would move

@@ -157,7 +157,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
*pre_binding_block,
Statement {
source_info: pattern_source_info,
kind: StatementKind::ReadForMatch(borrow_temp.clone()),
kind: StatementKind::FakeRead(FakeReadCause::ForMatch, borrow_temp.clone()),
Copy link
Contributor

Choose a reason for hiding this comment

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

presumably this has a decent-ish comment attached to it, too

@nikomatsakis nikomatsakis changed the title [WIP] Investigate fallout from NLL allowing creating unusable variables Investigate fallout from NLL allowing creating unusable variables Sep 14, 2018
@rust-highfive

This comment has been minimized.

@lqd
Copy link
Member Author

lqd commented Sep 14, 2018

I've pushed the updated comments, and helper function.

Note: I haven't updated the mir-opt yet so Travis will still fail.

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 good so far!

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@lqd
Copy link
Member Author

lqd commented Sep 15, 2018

(for anyone possibly not on zulip and following at home: mir-opt test suite updated, should be ready)

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 17, 2018

📌 Commit c7587c0aa117894910e9a3cd3c39e9c926ae2a68 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 Sep 17, 2018
@bors
Copy link
Contributor

bors commented Sep 18, 2018

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

@bors bors removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Sep 18, 2018
@bors bors added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 20, 2018
@rust-highfive

This comment has been minimized.

@pnkfelix
Copy link
Member

pnkfelix commented Sep 20, 2018

cc @michaelwoerister : the above travis failure seems to be a linking problem connected to codegen-units; any advice about how to address this?

Update: further investigation indicates that this bug may indeed be due to a combination of the change added by this PR, plus an unfortunate codegen-unit partitioning inhibiting LLVM's ability to optimize the uses of C away? Further discussion of those details should perhaps be put on #54388.

@pnkfelix
Copy link
Member

pnkfelix commented Sep 20, 2018

also nominating to discuss in compiler team meeting how best to address such codegen-units+linker problems when they arise (e.g., how to help contributors deal with such problems, either by mentoring them through the problems, or guiding them in how to delegate to specialists in the area)

Some of my questions:

  • Is it reasonable, in attempting to locally reproduce a bug like this, to manually increase codegen-units to e.g. 99 or 999 units? Or is that just as likely to mask issues?
  • How deterministic is the codegen-unit partitioning system? E.g. would it be a waste of bors-time to just respond to a bug like the above with a retry command?

@pnkfelix pnkfelix added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Sep 20, 2018
@pnkfelix
Copy link
Member

pnkfelix commented Sep 20, 2018

(also, I/we are confused about the expected behavior of running rustc on the extern-const.fixed file... as far as I can tell, there is no external definition of the global static C for it to link to in another file...)

Update: Filed separate issue regarding this question to #54388.

@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 Sep 20, 2018
@pnkfelix
Copy link
Member

(removing I-nominated tag; will raise question of how to deal with these kinds of test failures with compiler team asynchronously.)

@pnkfelix
Copy link
Member

@lqd okay, based on the discussion regarding #54388, I think you should comment out the // run-rustfix, replacing it with a fixme that points to #54388.

After that, I'll just make a PR that fixes the test to make its .fixed output consistent across our targets, and as part of that, I'll put the // run-rustfix back in.

`src/test/ui/extern/extern-const.rs` seems to have an inconsistent behaviour across build configurations, possibly non-deterministically. This is tracked in issue 54388.

For this PR, disable running rustfix on it because it failed on CI, until that issue is fixed.
@lqd
Copy link
Member Author

lqd commented Sep 20, 2018

I've pushed a commit which "unrustfixes" (rustbreaks?) this test

@lqd lqd added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 21, 2018
@pnkfelix
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 21, 2018

📌 Commit 3bdba74 has been approved by pnkfelix

@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 Sep 21, 2018
@pnkfelix
Copy link
Member

@bors r=nikomatsakis

@bors
Copy link
Contributor

bors commented Sep 21, 2018

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Sep 21, 2018

📌 Commit 3bdba74 has been approved by nikomatsakis

@bors
Copy link
Contributor

bors commented Sep 22, 2018

⌛ Testing commit 3bdba74 with merge 4591a24...

bors added a commit that referenced this pull request Sep 22, 2018
NLL: disallow creation of immediately unusable variables

Fix #53695

Original description follows

----

This WIP PR is for discussing the impact of fixing #53695 by injecting a fake read in let patterns.

(Travis will fail, at least the `mir-opt` suite is failing in its current state)
@bors
Copy link
Contributor

bors commented Sep 22, 2018

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

@bors bors merged commit 3bdba74 into rust-lang:master Sep 22, 2018
@lqd lqd deleted the fallout-53695 branch September 23, 2018 12:48
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) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NLL allows creating variables that are immediately unusable.
9 participants