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

Mark block exits as reachable if the block can break. #45316

Merged
merged 1 commit into from
Oct 20, 2017

Conversation

goffrie
Copy link
Contributor

@goffrie goffrie commented Oct 16, 2017

This only happens when desugaring catch expressions for now, but regular blocks (in HIR) can be broken from - respect that when doing reachability analysis.

Fixes #45124.

@rust-highfive
Copy link
Collaborator

r? @eddyb

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

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 16, 2017
@eddyb
Copy link
Member

eddyb commented Oct 16, 2017

r? @nikomatsakis

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Oct 16, 2017
if ctxt.may_break {
// If we can break from the block, then the block's exit is always reachable
// (... as long as the entry is reachable) - regardless of the tail of the block.
self.diverges.set(prev_diverges);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, this doesn't seem quite right because of the possibility of dead code. Consider something like this:

let x = 'a: {
    panic!(); break 'a 22;
};

or perhaps

let x = do catch {
    panic!("wtf");
    Err(22)?;
    Ok(())
};

That said, I think that ExprLoop is comparably imprecise. I'm trying to remember all the implications of this and make sure there's not some kind of soundness issue here.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Thoughts?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it unsound to mark code as potentially reachable (erm, Diverges::Maybe) when it in fact is never reachable? Seems like it shouldn't be - from my understanding, it should only be unsound to make the wrong call in the opposite direction.

Also maybe we could skip marking .may_break if the break itself is unreachable, though I wouldn't personally advocate for it.

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 currently the case for loop (is there a test for it? there ought to be):

fn main() {
    let x: u32 = { //~ ERROR mismatched types
        loop {
            panic!();
            break;
        };
    };
}

From a soundness perspective, IIRC if an arm is never reachable, then it is potentially reachable, so regarding something that always diverges as something that potentially reaches its exit should always be sound.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for delay. Busy couple of days. Anyway, I agree that ExprLoop is treated the same way, and I also agreed it ought not to be unsound to approximate in this way. (In particular, I think we only care when things definitely diverge -- this makes sense since clearly we can't tell that things definitely don't diverge (damn halting problem).)

I was thinking more about what behavior we actually want. I actually think it's consistent with our general strategy around typing dead code to allow break -- even in dead code -- to make a loop or block be considered non-diverging. For example, the example that @arielb1 gave here] seems consistent with the rules we adopted for dead-code, which mean that e.g. the following code doesn't build:

#![allow(unreachable_code)]

fn main() {
    let x: u32 = { //~ ERROR mismatched types
        panic!();
        'a'
    };
}

In particular, I would consider break 'a' to be equivalent to using 'a' as a tail expression.

Essentially -- to the extent possible -- we are aiming to type-check dead-code as if it could become live. This includes not only the types of values produced by dead-code but also the control-flow that the dead-code would have introduced.

We might want to tweak that last bit but, regardless, this PR is consistent with what we do now.

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 19, 2017

📌 Commit 57f03ea has been approved by nikomatsakis

@kennytm kennytm 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 19, 2017
@bors
Copy link
Contributor

bors commented Oct 20, 2017

⌛ Testing commit 57f03ea with merge c0e0a38...

bors added a commit that referenced this pull request Oct 20, 2017
Mark block exits as reachable if the block can break.

This only happens when desugaring `catch` expressions for now, but regular blocks (in HIR) can be broken from - respect that when doing reachability analysis.

Fixes #45124.
@bors
Copy link
Contributor

bors commented Oct 20, 2017

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

@bors bors merged commit 57f03ea into rust-lang:master Oct 20, 2017
@goffrie goffrie deleted the exitable-breakable-block branch October 25, 2017 22:43
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.

7 participants