diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-05-31 22:01:52 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-05-31 22:01:52 +0100 |
commit | 71117e6812f87e014bc8e984e195a75e222ac227 (patch) | |
tree | 8f5a74076cc2802c5de8fdb1b40e3eaf12252d51 /crates/hir_ty/src/diagnostics/match_check/pat_util.rs | |
parent | 42dfdb87cb748e65d2c87687bde4d4712f9a850b (diff) | |
parent | e7c49666be180eba2720cce09d4d2116b1ef4d20 (diff) |
Merge #8717
8717: Update match checking algorithm r=iDawer a=iDawer
I've recently got interest in the match checking to extend the current algo to support reporting witnesses of non-exhaustiveness.
It appears the algo is outdated from rustc's implementation. I decided to rewrite it based on the latest rustc's version. It is a diff-based port to ra codebase. That means you can diff-compare these files to rustc.
I'm striving to keep minimal ra-related changes in the algo to make it easier to backport future changes from the upstream.
Based on upstream algorithm of version rust-lang/rust 1.52.0-nightly (25c15cdbe 2021-04-22)
https://github.com/rust-lang/rust/blob/25c15cdbe/compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
The goal of this PR is to cover the current `missing-match-arm` diagnostic.
What is remaining to do:
- [x] Error handling. The errors that are unrelated to match checking will be handled before the check. Just like how it made in rustc.
- [x] Lowering `hir_def::expr::Pat` to `hir_ty::diagnostics::match_check::Pat`. rustc's match checking works on top of `rustc_mir_build::thir::Pat`, which is lowered from `hir::Pat` and carries some extra semantics used by the check. All unrelated checks are done there. RA could use this to rule out running the check on unimplemented cases (`Pat::ConstBlock`, etc).
- [x] ~~Proper~~Loose typecheck of match arm patterns (https://github.com/rust-analyzer/rust-analyzer/pull/8840, https://github.com/rust-analyzer/rust-analyzer/pull/8875).
- [x] Tests from `hir_ty::diagnostics::match_check::tests`.
- [x] Clean up `todo`s
- [x] Test run on real repos https://github.com/rust-analyzer/rust-analyzer/pull/8717#issuecomment-847120265.
Co-authored-by: Dawer <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/diagnostics/match_check/pat_util.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics/match_check/pat_util.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/crates/hir_ty/src/diagnostics/match_check/pat_util.rs b/crates/hir_ty/src/diagnostics/match_check/pat_util.rs new file mode 100644 index 000000000..b89b4f2bf --- /dev/null +++ b/crates/hir_ty/src/diagnostics/match_check/pat_util.rs | |||
@@ -0,0 +1,56 @@ | |||
1 | //! Pattern untilities. | ||
2 | //! | ||
3 | //! Originates from `rustc_hir::pat_util` | ||
4 | |||
5 | use std::iter::{Enumerate, ExactSizeIterator}; | ||
6 | |||
7 | pub(crate) struct EnumerateAndAdjust<I> { | ||
8 | enumerate: Enumerate<I>, | ||
9 | gap_pos: usize, | ||
10 | gap_len: usize, | ||
11 | } | ||
12 | |||
13 | impl<I> Iterator for EnumerateAndAdjust<I> | ||
14 | where | ||
15 | I: Iterator, | ||
16 | { | ||
17 | type Item = (usize, <I as Iterator>::Item); | ||
18 | |||
19 | fn next(&mut self) -> Option<(usize, <I as Iterator>::Item)> { | ||
20 | self.enumerate | ||
21 | .next() | ||
22 | .map(|(i, elem)| (if i < self.gap_pos { i } else { i + self.gap_len }, elem)) | ||
23 | } | ||
24 | |||
25 | fn size_hint(&self) -> (usize, Option<usize>) { | ||
26 | self.enumerate.size_hint() | ||
27 | } | ||
28 | } | ||
29 | |||
30 | pub(crate) trait EnumerateAndAdjustIterator { | ||
31 | fn enumerate_and_adjust( | ||
32 | self, | ||
33 | expected_len: usize, | ||
34 | gap_pos: Option<usize>, | ||
35 | ) -> EnumerateAndAdjust<Self> | ||
36 | where | ||
37 | Self: Sized; | ||
38 | } | ||
39 | |||
40 | impl<T: ExactSizeIterator> EnumerateAndAdjustIterator for T { | ||
41 | fn enumerate_and_adjust( | ||
42 | self, | ||
43 | expected_len: usize, | ||
44 | gap_pos: Option<usize>, | ||
45 | ) -> EnumerateAndAdjust<Self> | ||
46 | where | ||
47 | Self: Sized, | ||
48 | { | ||
49 | let actual_len = self.len(); | ||
50 | EnumerateAndAdjust { | ||
51 | enumerate: self.enumerate(), | ||
52 | gap_pos: gap_pos.unwrap_or(expected_len), | ||
53 | gap_len: expected_len - actual_len, | ||
54 | } | ||
55 | } | ||
56 | } | ||