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

Expand potential inner Or pattern for THIR #98200

Merged
merged 3 commits into from
Aug 22, 2022
Merged

Conversation

ouz-a
Copy link
Contributor

@ouz-a ouz-a commented Jun 17, 2022

Code assumed there wouldn't be a deeper Or pattern inside expanded PatStack this fixes it by looking for the Or pattern inside expanded PatStack.

A more ideal solution would be recursively doing this but I haven't found a good way to do that.
fixes #97898

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 17, 2022
@rust-highfive
Copy link
Collaborator

r? @estebank

(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 Jun 17, 2022
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

This could use a test, preferably also other negative test(s) that check that this logic is sound as well.

@estebank
Copy link
Contributor

please squash your commits, r=me

@@ -453,7 +453,17 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
/// expands it.
fn push(&mut self, row: PatStack<'p, 'tcx>) {
if !row.is_empty() && row.head().is_or_pat() {
self.patterns.extend(row.expand_or_pat());
let pats = row.expand_or_pat();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to make expand_or_pat recursively expand or patterns instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't make that one recursive but made a new function that is recursive.

Copy link
Contributor

Choose a reason for hiding this comment

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

Either the other use of expand_or_Pat is dead code now or it's still got that issue. Check whether it's dead by panicking inside of it instead of doing any logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can't be dead code because line 835, it's the only reason I couldn't replace the function altogether

Copy link
Contributor

Choose a reason for hiding this comment

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

That call may be dead after your change is what I'm thinking. Since you now expand all the nested Or patterns, line 835 may not encounter Or patterns anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did as you said, it panicked. Newly added function is only called via fn push and that is called via line 846.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like that we now have one function that does the recursive work and one that doesn't. On what kind of matches did it panic? Should we call the new function at that site to reduce duplication and make it more general?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other function just works like iterator right now, I tried to merge these two functions together but problem is, it's quite tricky because of line 838 if I remove expand_or_pat altogether and just call expand_and_extend compiler gives a warning unreachable pattern even though both of them should do the same thing at this point.

Copy link
Contributor

Choose a reason for hiding this comment

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

that is suspicious indeed and makes me confident that we should definitely make them work the same and figure out why they are problematic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I understand now why the compiler gives "unreachable pattern" err, to reach those places I need to access is_under_guard for each recursive call, but also push to the matrix has to happen after the call is_useful to trigger if v.is_empty() where the matrix has to be empty. Even if I throw all those out of the window I need to call is_useful for each v 🤷‍♂️

@estebank
Copy link
Contributor

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned estebank Jun 24, 2022
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 24, 2022
@LeSeulArtichaut
Copy link
Contributor

Here's a very similar ICE:

enum Foo {
    A,
    B,
    C,
}

fn main() {
    match Foo::A {
        | _foo @ (Foo::A | Foo::B) => {}
//      ^ note the trailing `|`
        Foo::C => {}
    };
}

The trailing | makes the pattern in the first arm to be an Or pattern with a single subpattern. I assume it should be fixed by this PR? Is it worth yet another testcase?

@ouz-a
Copy link
Contributor Author

ouz-a commented Aug 17, 2022

Here's a very similar ICE:

enum Foo {
    A,
    B,
    C,
}

fn main() {
    match Foo::A {
        | _foo @ (Foo::A | Foo::B) => {}
//      ^ note the trailing `|`
        Foo::C => {}
    };
}

The trailing | makes the pattern in the first arm to be an Or pattern with a single subpattern. I assume it should be fixed by this PR? Is it worth yet another testcase?

Yeah this PR fixes this, added it as test case, thanks!

@oli-obk
Copy link
Contributor

oli-obk commented Aug 22, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Aug 22, 2022

📌 Commit ddf23cb has been approved by oli-obk

It is now in the queue for this repository.

@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 Aug 22, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Aug 22, 2022
Expand potential inner `Or` pattern for THIR

Code assumed there wouldn't be a deeper `Or` pattern inside expanded `PatStack` this fixes it by looking for the `Or` pattern inside expanded `PatStack`.

A more ideal solution would be recursively doing this but I haven't found a good way to do that.
_fixes #97898_
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 22, 2022
Rollup of 8 pull requests

Successful merges:

 - rust-lang#98200 (Expand potential inner `Or` pattern for THIR)
 - rust-lang#99770 (Make some const prop mir-opt tests `unit-test`s)
 - rust-lang#99957 (Rework Ipv6Addr::is_global to check for global reachability rather than global scope - rebase)
 - rust-lang#100331 (Guarantee `try_reserve` preserves the contents on error)
 - rust-lang#100336 (Fix two const_trait_impl issues)
 - rust-lang#100713 (Convert diagnostics in parser/expr to SessionDiagnostic)
 - rust-lang#100820 (Use pointer `is_aligned*` methods)
 - rust-lang#100872 (Add guarantee that Vec::default() does not alloc)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 56ba13a into rust-lang:master Aug 22, 2022
@rustbot rustbot added this to the 1.65.0 milestone Aug 22, 2022
@ouz-a ouz-a deleted the issue-98177 branch November 13, 2022 09:56
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. 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.

ICE: deconstructing a pattern with multiple variable bindings of OR'ed strings.
10 participants