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

add note for non-exhaustive matches with guards #113019

Merged

Conversation

ericmarkmartin
Copy link
Contributor

Associated issue: #92197

When a match statement includes guards on every match arm (and is therefore necessarily non-exhaustive), add a note to the error E0004 diagnostic noting this.

@rustbot
Copy link
Collaborator

rustbot commented Jun 25, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @fee1-dead (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 25, 2023
@ericmarkmartin ericmarkmartin marked this pull request as ready for review June 25, 2023 04:38
@rustbot
Copy link
Collaborator

rustbot commented Jun 25, 2023

Some changes might have occurred in exhaustiveness checking

cc @Nadrieril


let all_arms_have_guards = arms.iter().all(|arm_id| thir[*arm_id].guard.is_some());
if !is_empty_match && all_arms_have_guards {
err.note("match arms with guards don't count towards exhaustivity");
Copy link
Member

@fee1-dead fee1-dead Jun 26, 2023

Choose a reason for hiding this comment

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

Could you please use translatable diagnostics?

Copy link
Member

@fee1-dead fee1-dead left a comment

Choose a reason for hiding this comment

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

I think it would be better if the message pointed at the specific match arm that had guards and prevented the match from being exhaustive, but this is a step.

@@ -25,10 +25,14 @@ fn main() {
let ed = ElementData { kind: Box::new(ElementKind::HTMLImageElement(id)) };
let n = NodeData { kind: Box::new(NodeKind::Element(ed)) };

// n.b. span could be better
Copy link
Member

Choose a reason for hiding this comment

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

why did you remove this comment?

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 thought that was in reference to the lack of the comments annotating the expected diagnostic in the compiletest. Is that not right?

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 it was about the span that the diagnostic highlighted?

@fee1-dead
Copy link
Member

Marking as waiting on author, pleasing make this a translatable diagnostic by using a struct and derive(Subdiagnostic). If you have any questions feel free to reach out on zulip.

@rustbot author

@rustbot rustbot 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 Jun 28, 2023
@ericmarkmartin
Copy link
Contributor Author

I think it would be better if the message pointed at the specific match arm that had guards and prevented the match from being exhaustive, but this is a step.

How would you determine which match arm is preventing the match from being exhaustive? For instance in

fn foo(i: i32) {
    match i {
        i if i == 0 => {},
        i if i != 0 => {},
    }
}

removing either guard will cause the match to be exhaustive, and I feel like pointing to one of them in particular might be a bit confusing.

I also have some thoughts about how the error message could be improved---I mostly put this up so I could get some feedback there, though maybe Zulip is the better place to do that.

For instance, the current wording of the note makes it sound like something that would show up on any non-exhaustive match with an unguarded arm. I think this narrow concern could be addressed by explicitly mentioning that all arms have guards, like ocamlc does:

let foo (i : int) =
    match i with
    | i when i == 0 -> ()
    | i when i <> 0 -> ()
$ ocamlc main.ml
File "main.ml", lines 2-4, characters 4-25:
2 | ....match i with
3 |     | i when i == 0 -> ()
4 |     | i when i <> 0 -> ()
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
All clauses in this pattern-matching are guarded.

@fee1-dead
Copy link
Member

How would you determine which match arm is preventing the match from being exhaustive?

Obviously if we have the same pattern then they all don't contribute to exhaustiveness. I was thinking about how for specific cases like such:

    let x = Some(1);
    match x {
        Some(x) if x == 5 => {}
        None => {}
    }

we can then point at Some(x) if x == 5 and how that doesn't contribute to exhaustiveness.

    match x {
        Foo(x) if x == 5 => {}
        Foo(x) => {}
        Bar(x) if y == 42 => {}
        None => {}
    }

For the above we need to point at the Bar arm but not the Foo arm. this is definitely a complex algorithm if we don't have something like that already, but it could be recorded from exhaustiveness checking? I'm not sure. I'm happy to land this once you migrate to translatable diagnostics though.

@ericmarkmartin ericmarkmartin force-pushed the warning-for-guard-non-exhaustion branch from 3f029ab to 2017a17 Compare June 28, 2023 05:54
@fee1-dead
Copy link
Member

Thanks.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 28, 2023

📌 Commit 96bd056 has been approved by fee1-dead

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 28, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 28, 2023
Rollup of 6 pull requests

Successful merges:

 - rust-lang#111571 (Implement proposed API for `proc_macro_span`)
 - rust-lang#112236 (Simplify computation of killed borrows)
 - rust-lang#112867 (More `ImplSource` nits)
 - rust-lang#113019 (add note for non-exhaustive matches with guards)
 - rust-lang#113094 (Fix invalid HTML DIV tag used in HEAD)
 - rust-lang#113111 (add myself to review for t-types stuff)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e4e1a99 into rust-lang:master Jun 28, 2023
11 checks passed
@rustbot rustbot added this to the 1.72.0 milestone Jun 28, 2023
@ericmarkmartin ericmarkmartin deleted the warning-for-guard-non-exhaustion branch June 29, 2023 02:28
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.

None yet

4 participants