diff options
author | Dawer <[email protected]> | 2021-05-10 12:55:00 +0100 |
---|---|---|
committer | Dawer <[email protected]> | 2021-05-31 20:08:27 +0100 |
commit | 466345ca81c9f8a17347671ca27856eb963858f4 (patch) | |
tree | 8327771c3d67f7c4509b373e8ad7075f5cfd5ac6 /crates/hir_ty/src/diagnostics/pattern.rs | |
parent | 49e016169fc8413e2734a655cbd55ebba2907b76 (diff) |
Clean up, more docs.
Diffstat (limited to 'crates/hir_ty/src/diagnostics/pattern.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics/pattern.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/crates/hir_ty/src/diagnostics/pattern.rs b/crates/hir_ty/src/diagnostics/pattern.rs index 38e4b53b7..85c01b178 100644 --- a/crates/hir_ty/src/diagnostics/pattern.rs +++ b/crates/hir_ty/src/diagnostics/pattern.rs | |||
@@ -1,15 +1,18 @@ | |||
1 | #![deny(elided_lifetimes_in_paths)] | 1 | //! Validation of matches. |
2 | #![allow(unused)] // todo remove | 2 | //! |
3 | //! This module provides lowering from [hir_def::expr::Pat] to [self::Pat] and match | ||
4 | //! checking algorithm. | ||
5 | //! | ||
6 | //! It is a loose port of `rustc_mir_build::thir::pattern` module. | ||
3 | 7 | ||
4 | mod deconstruct_pat; | 8 | mod deconstruct_pat; |
5 | // TODO: find a better place for this? | ||
6 | mod pat_util; | 9 | mod pat_util; |
7 | pub(crate) mod usefulness; | 10 | pub(crate) mod usefulness; |
8 | 11 | ||
9 | use hir_def::{body::Body, EnumVariantId, LocalFieldId, VariantId}; | 12 | use hir_def::{body::Body, EnumVariantId, LocalFieldId, VariantId}; |
10 | use la_arena::Idx; | 13 | use la_arena::Idx; |
11 | 14 | ||
12 | use crate::{db::HirDatabase, AdtId, InferenceResult, Interner, Substitution, Ty, TyKind}; | 15 | use crate::{db::HirDatabase, InferenceResult, Interner, Substitution, Ty, TyKind}; |
13 | 16 | ||
14 | use self::pat_util::EnumerateAndAdjustIterator; | 17 | use self::pat_util::EnumerateAndAdjustIterator; |
15 | 18 | ||
@@ -38,6 +41,7 @@ impl Pat { | |||
38 | } | 41 | } |
39 | } | 42 | } |
40 | 43 | ||
44 | /// Close relative to `rustc_mir_build::thir::pattern::PatKind` | ||
41 | #[derive(Clone, Debug, PartialEq)] | 45 | #[derive(Clone, Debug, PartialEq)] |
42 | pub(crate) enum PatKind { | 46 | pub(crate) enum PatKind { |
43 | Wild, | 47 | Wild, |
@@ -66,7 +70,7 @@ pub(crate) enum PatKind { | |||
66 | subpattern: Pat, | 70 | subpattern: Pat, |
67 | }, | 71 | }, |
68 | 72 | ||
69 | // only bool for now | 73 | // FIXME: for now, only bool literals are implemented |
70 | LiteralBool { | 74 | LiteralBool { |
71 | value: bool, | 75 | value: bool, |
72 | }, | 76 | }, |
@@ -91,7 +95,7 @@ impl<'a> PatCtxt<'a> { | |||
91 | } | 95 | } |
92 | 96 | ||
93 | pub(crate) fn lower_pattern(&mut self, pat: hir_def::expr::PatId) -> Pat { | 97 | pub(crate) fn lower_pattern(&mut self, pat: hir_def::expr::PatId) -> Pat { |
94 | // TODO: pattern adjustments (implicit dereference) | 98 | // FIXME: implement pattern adjustments (implicit pattern dereference; "RFC 2005-match-ergonomics") |
95 | // More info https://github.com/rust-lang/rust/issues/42640#issuecomment-313535089 | 99 | // More info https://github.com/rust-lang/rust/issues/42640#issuecomment-313535089 |
96 | let unadjusted_pat = self.lower_pattern_unadjusted(pat); | 100 | let unadjusted_pat = self.lower_pattern_unadjusted(pat); |
97 | unadjusted_pat | 101 | unadjusted_pat |
@@ -141,7 +145,7 @@ impl<'a> PatCtxt<'a> { | |||
141 | .iter() | 145 | .iter() |
142 | .map(|field| FieldPat { | 146 | .map(|field| FieldPat { |
143 | // XXX(iDawer): field lookup is inefficient | 147 | // XXX(iDawer): field lookup is inefficient |
144 | field: variant_data.field(&field.name).unwrap_or_else(|| todo!()), | 148 | field: variant_data.field(&field.name).unwrap(), |
145 | pattern: self.lower_pattern(field.pat), | 149 | pattern: self.lower_pattern(field.pat), |
146 | }) | 150 | }) |
147 | .collect(); | 151 | .collect(); |
@@ -208,11 +212,10 @@ impl<'a> PatCtxt<'a> { | |||
208 | PatKind::Wild | 212 | PatKind::Wild |
209 | } | 213 | } |
210 | }; | 214 | }; |
211 | // TODO: do we need PatKind::AscribeUserType ? | ||
212 | kind | 215 | kind |
213 | } | 216 | } |
214 | 217 | ||
215 | fn lower_path(&mut self, pat: hir_def::expr::PatId, path: &hir_def::path::Path) -> Pat { | 218 | fn lower_path(&mut self, pat: hir_def::expr::PatId, _path: &hir_def::path::Path) -> Pat { |
216 | let ty = &self.infer[pat]; | 219 | let ty = &self.infer[pat]; |
217 | 220 | ||
218 | let pat_from_kind = |kind| Pat { ty: ty.clone(), kind: Box::new(kind) }; | 221 | let pat_from_kind = |kind| Pat { ty: ty.clone(), kind: Box::new(kind) }; |
@@ -338,8 +341,6 @@ impl PatternFoldable for PatKind { | |||
338 | mod tests { | 341 | mod tests { |
339 | use crate::diagnostics::tests::check_diagnostics; | 342 | use crate::diagnostics::tests::check_diagnostics; |
340 | 343 | ||
341 | use super::*; | ||
342 | |||
343 | #[test] | 344 | #[test] |
344 | fn unit() { | 345 | fn unit() { |
345 | check_diagnostics( | 346 | check_diagnostics( |
@@ -372,8 +373,6 @@ fn main() { | |||
372 | 373 | ||
373 | #[test] | 374 | #[test] |
374 | fn tuple_with_ellipsis() { | 375 | fn tuple_with_ellipsis() { |
375 | // TODO: test non-exhaustive match with ellipsis in the middle | ||
376 | // of a pattern, check reported witness | ||
377 | check_diagnostics( | 376 | check_diagnostics( |
378 | r#" | 377 | r#" |
379 | struct A; struct B; | 378 | struct A; struct B; |