Skip to content

[MIR] Fix equality checks in matching code #30651

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 1 commit into from
Jan 4, 2016

Conversation

nagisa
Copy link
Member

@nagisa nagisa commented Dec 30, 2015

This is not a fix to checks themselves per se (though we still use Eq MIR test instead of calling PartialEq::eq), but rather how we handle items we encounter in pattern position.

Previously we would just call PartialEq with the constant and the matchee, but now we essentially inline the constant instead. E.g. these two snippets are functionally equivalent at MIR level:

match val { Some(42) => true, _ => false }

and

const SECRET: Option<u8> = Some(42);
match val { SECRET => true, _ => false }

This approach also allows for more optimizations of matches. I.e. It can now exploit SwitchInt to switch on number inside a Some regardless of whether the value being an item or not.

This is based on @tsion’s already approved PR so I could reuse the file for more tests.

r? @eddyb
cc @nikomatsakis @tsion

};
PatternKind::Constant { value: literal }
let pat = const_eval::const_expr_to_pat(self.cx.tcx, const_expr,
pat.span);
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice if we can use HAIR to avoid allocating here.

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, that would overcomplicate too many things.

@nagisa nagisa force-pushed the mir-fix-equality-checks branch from 39f74b0 to 209d0a6 Compare December 30, 2015 23:32
@nagisa nagisa force-pushed the mir-fix-equality-checks branch from 209d0a6 to add7410 Compare January 1, 2016 12:56
@eddyb
Copy link
Member

eddyb commented Jan 3, 2016

Given that this is how we implement matching on constants currently, I don't see any problems with this.
@bors r+

@bors
Copy link
Collaborator

bors commented Jan 3, 2016

📌 Commit add7410 has been approved by eddyb

@bors
Copy link
Collaborator

bors commented Jan 3, 2016

⌛ Testing commit add7410 with merge a9a42a0...

@bors
Copy link
Collaborator

bors commented Jan 4, 2016

💔 Test failed - auto-win-gnu-64-nopt-t

@eddyb
Copy link
Member

eddyb commented Jan 4, 2016

@bors retry

bors added a commit that referenced this pull request Jan 4, 2016
This is not a fix to checks themselves per se (though we still use `Eq` MIR test instead of calling `PartialEq::eq`), but rather how we handle items we encounter in pattern position.

Previously we would just call `PartialEq` with the constant and the matchee, but now we essentially inline the constant instead. E.g. these two snippets are functionally equivalent at MIR level:

```
match val { Some(42) => true, _ => false }
```
and
```
const SECRET: Option<u8> = Some(42);
match val { SECRET => true, _ => false }
```

This approach also allows for more optimizations of matches. I.e. It can now exploit `SwitchInt` to switch on number inside a `Some` regardless of whether the value being an item or not.

This is based on @tsion’s already approved PR so I could reuse the file for more tests.

r? @eddyb
cc @nikomatsakis @tsion
@bors
Copy link
Collaborator

bors commented Jan 4, 2016

⌛ Testing commit add7410 with merge 191ff2d...

@bors
Copy link
Collaborator

bors commented Jan 4, 2016

💔 Test failed - auto-linux-64-nopt-t

@eddyb
Copy link
Member

eddyb commented Jan 4, 2016

@bors retry

@bors bors merged commit add7410 into rust-lang:master Jan 4, 2016
@nikomatsakis
Copy link
Contributor

On Sun, Jan 03, 2016 at 01:21:16PM -0800, Eduard-Mihai Burtescu wrote:

Given that this is how we implement matching on constants currently, I don't see any problems with this.

Agreed (though I didn't yet look at the patch). I'd prefer to change
how we implement constants, but it's a separate thing.

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.

4 participants