Skip to content

[MIR] Make sure candidates are reversed before match_candidates. #30553

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

Merged
merged 3 commits into from
Jan 4, 2016

Conversation

luqmana
Copy link
Member

@luqmana luqmana commented Dec 25, 2015

Fixes #30527.

#![feature(rustc_attrs)]

#[rustc_mir(graphviz="match.dot")]
fn main() {
    let _abc = match Some(101i8) {
        Some(xyz) if xyz > 100 => xyz,
        Some(_) => -1,
        None => -2
    };
}

Resulting MIR now includes the Some(xyz) arm, guard and all:
match.dot

Not quite sure how to write a test for this. Thinking too hard, just tested the end result.

r? @nikomatsakis

.flat_map(|(target_block, mut target_candidates)| {
// We need to preserve the fact that the candidates
// are in the reversed order compared to the source.
target_candidates.reverse();
self.match_candidates(span,
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps match_candidates could be refactored to consume an iterator, thus allowing us to avoid a potentially expensive operation that is reversal of elements in a Vector?

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively you could just reverse the target_candidates iterator from which the vector is built from just above.

Copy link
Member

Choose a reason for hiding this comment

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

I think I would prefer if sort_candidate above already produced list in the correct order.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried reversing just the creation of the target_candidates Vec but that turned out to be a lot more annoying. So, instead I just changed all the code to deal with the candidates in the same order they're presented in the source rather than reversed.

@michaelwoerister
Copy link
Member

@luqmana 😍

@@ -361,15 +358,17 @@ impl<'a,'tcx> Builder<'a,'tcx> {

// If all candidates were sorted into `target_candidates` somewhere, then
// the initial set was inexhaustive.
let untested_candidates = candidates.len() - tested_candidates;
let untested_candidates = unmatched_candidates.len() - tested_candidates;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe do a let untestd_candidates = unmatched_candidates.split_off(tested_candidates) here and then next check becomes if untested_candidates.len() == 0.

@nagisa
Copy link
Member

nagisa commented Dec 29, 2015

Looks really good to me now.

@luqmana luqmana force-pushed the mir-match-arm-guards branch from 3c937bc to f88c808 Compare December 29, 2015 22:04
@nikomatsakis
Copy link
Contributor

@bors r+

Seems good.

@bors
Copy link
Collaborator

bors commented Jan 4, 2016

📌 Commit f88c808 has been approved by nikomatsakis

@bors
Copy link
Collaborator

bors commented Jan 4, 2016

⌛ Testing commit f88c808 with merge b622891...

bors added a commit that referenced this pull request Jan 4, 2016
Fixes #30527.

```Rust

fn main() {
    let _abc = match Some(101i8) {
        Some(xyz) if xyz > 100 => xyz,
        Some(_) => -1,
        None => -2
    };
}
```

Resulting MIR now includes the `Some(xyz)` arm, guard and all:
![match.dot](https://cloud.githubusercontent.com/assets/287063/11999413/066f7610-aa8b-11e5-927b-24215af57fc4.png)

~~Not quite sure how to write a test for this.~~ Thinking too hard, just tested the end result.

r? @nikomatsakis
@bors bors merged commit f88c808 into rust-lang:master Jan 4, 2016
@luqmana luqmana deleted the mir-match-arm-guards branch May 9, 2016 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants