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

never patterns: suggest ! patterns on non-exhaustive matches #121823

Merged
merged 5 commits into from
Mar 18, 2024

Conversation

Nadrieril
Copy link
Member

When a match is non-exhaustive we now suggest never patterns whenever it makes sense.

r? @compiler-errors

@Nadrieril Nadrieril added the F-never_patterns `#![feature(never_patterns)]` label Mar 1, 2024
@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 Mar 1, 2024
@@ -1020,6 +1020,14 @@ fn report_non_exhaustive_match<'p, 'tcx>(

let mut suggestion = None;
let sm = cx.tcx.sess.source_map();
let suggested_arm = if witnesses.len() < 4
Copy link
Member

Choose a reason for hiding this comment

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

why < 4?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because that's also the limit used above to choose whether to report patterns or _. You're right this should be made clear, let me clean this

Copy link
Member

Choose a reason for hiding this comment

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

I just find it strange that this applies to only the never branch, since the other half of the if could suffer from the same problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

There were actually three places using this magic threshold ^^' Fixed in the last commit

Copy link
Member Author

@Nadrieril Nadrieril Mar 12, 2024

Choose a reason for hiding this comment

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

If there are more than 4 witnesses we suggest _ => todo!(), it doesn't make sense to remove the todo!() then. And I can't just suggest ! either because the type might not be empty.

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 18, 2024

📌 Commit 1b31e14 has been approved by compiler-errors

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 Mar 18, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 18, 2024
…ler-errors

never patterns: suggest `!` patterns on non-exhaustive matches

When a match is non-exhaustive we now suggest never patterns whenever it makes sense.

r? `@compiler-errors`
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 18, 2024
…iaskrgr

Rollup of 12 pull requests

Successful merges:

 - rust-lang#121258 (Reject overly generic assoc const binding types)
 - rust-lang#121823 (never patterns: suggest `!` patterns on non-exhaustive matches)
 - rust-lang#122060 (Stabilize `imported_main`)
 - rust-lang#122158 (Provide structured suggestion for `#![feature(foo)]`)
 - rust-lang#122642 (Improve wording of `Vec::swap_remove`)
 - rust-lang#122675 (core: document default attribute stabilization)
 - rust-lang#122687 (`NormalizesTo`: return nested goals to caller)
 - rust-lang#122691 (Fix ICE: `global_asm!()` Don't Panic When Unable to Evaluate Constant)
 - rust-lang#122693 (Fix heading anchors in doc pages.)
 - rust-lang#122699 (Fix a typo in the 1.77.0 relnotes)
 - rust-lang#122700 (Remove redundant files, rename base riscv32 file)
 - rust-lang#122701 (Detect allocator for box in `must_not_suspend` lint)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 18, 2024
…iaskrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#121258 (Reject overly generic assoc const binding types)
 - rust-lang#121823 (never patterns: suggest `!` patterns on non-exhaustive matches)
 - rust-lang#122060 (Stabilize `imported_main`)
 - rust-lang#122158 (Provide structured suggestion for `#![feature(foo)]`)
 - rust-lang#122642 (Improve wording of `Vec::swap_remove`)
 - rust-lang#122675 (core: document default attribute stabilization)
 - rust-lang#122687 (`NormalizesTo`: return nested goals to caller)
 - rust-lang#122693 (Fix heading anchors in doc pages.)
 - rust-lang#122699 (Fix a typo in the 1.77.0 relnotes)
 - rust-lang#122700 (Remove redundant files, rename base riscv32 file)
 - rust-lang#122701 (Detect allocator for box in `must_not_suspend` lint)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2d3dcfa into rust-lang:master Mar 18, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 18, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 18, 2024
Rollup merge of rust-lang#121823 - Nadrieril:never-witnesses, r=compiler-errors

never patterns: suggest `!` patterns on non-exhaustive matches

When a match is non-exhaustive we now suggest never patterns whenever it makes sense.

r? ``@compiler-errors``
@Nadrieril Nadrieril deleted the never-witnesses branch March 18, 2024 23:34
@Nadrieril
Copy link
Member Author

This caused a small regression. I'm surprised because a far as I can tell this only touches error paths (except when non_exhaustive_omitted_patterns is enabled, which it isn't for that particular benchmark). It's small enough that I'm tempted to shrug it off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-never_patterns `#![feature(never_patterns)]` 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.

4 participants