aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-04-30 15:12:04 +0100
committerDawer <[email protected]>2021-05-31 20:03:46 +0100
commit5a8a0b62697c01ef881e7e2a0387e3649cab2034 (patch)
treee534317fe74f4cc90e33c6c7fb66a5e5289e6459 /crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
parentb4f41973326a684844ffe23c5816e17d485b4203 (diff)
Check enum patterns
Diffstat (limited to 'crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs')
-rw-r--r--crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs64
1 files changed, 59 insertions, 5 deletions
diff --git a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
index 60323aea3..3c1811f95 100644
--- a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
+++ b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
@@ -135,6 +135,7 @@ impl Constructor {
135 Pat::Bind { .. } | Pat::Wild => Wildcard, 135 Pat::Bind { .. } | Pat::Wild => Wildcard,
136 Pat::Tuple { .. } | Pat::Ref { .. } | Pat::Box { .. } => Single, 136 Pat::Tuple { .. } | Pat::Ref { .. } | Pat::Box { .. } => Single,
137 Pat::Record { .. } | Pat::Path(_) | Pat::TupleStruct { .. } => { 137 Pat::Record { .. } | Pat::Path(_) | Pat::TupleStruct { .. } => {
138 // TODO: path to const
138 let variant_id = 139 let variant_id =
139 cx.infer.variant_resolution_for_pat(pat).unwrap_or_else(|| todo!()); 140 cx.infer.variant_resolution_for_pat(pat).unwrap_or_else(|| todo!());
140 match variant_id { 141 match variant_id {
@@ -144,8 +145,8 @@ impl Constructor {
144 } 145 }
145 146
146 Pat::Or(..) => panic!("bug: Or-pattern should have been expanded earlier on."), 147 Pat::Or(..) => panic!("bug: Or-pattern should have been expanded earlier on."),
148 Pat::Missing => todo!("Fail gracefully when there is an error in a pattern"),
147 pat => todo!("Constructor::from_pat {:?}", pat), 149 pat => todo!("Constructor::from_pat {:?}", pat),
148 // Pat::Missing => {}
149 // Pat::Range { start, end } => {} 150 // Pat::Range { start, end } => {}
150 // Pat::Slice { prefix, slice, suffix } => {} 151 // Pat::Slice { prefix, slice, suffix } => {}
151 // Pat::Lit(_) => {} 152 // Pat::Lit(_) => {}
@@ -280,7 +281,7 @@ pub(super) struct SplitWildcard {
280 281
281impl SplitWildcard { 282impl SplitWildcard {
282 pub(super) fn new(pcx: PatCtxt<'_>) -> Self { 283 pub(super) fn new(pcx: PatCtxt<'_>) -> Self {
283 // let cx = pcx.cx; 284 let cx = pcx.cx;
284 // let make_range = |start, end| IntRange(todo!()); 285 // let make_range = |start, end| IntRange(todo!());
285 286
286 // This determines the set of all possible constructors for the type `pcx.ty`. For numbers, 287 // This determines the set of all possible constructors for the type `pcx.ty`. For numbers,
@@ -292,9 +293,62 @@ impl SplitWildcard {
292 // Invariant: this is empty if and only if the type is uninhabited (as determined by 293 // Invariant: this is empty if and only if the type is uninhabited (as determined by
293 // `cx.is_uninhabited()`). 294 // `cx.is_uninhabited()`).
294 let all_ctors = match pcx.ty.kind(&Interner) { 295 let all_ctors = match pcx.ty.kind(&Interner) {
295 TyKind::Adt(AdtId(hir_def::AdtId::EnumId(_)), _) => todo!(), 296 TyKind::Scalar(Scalar::Bool) => todo!(),
297 // TyKind::Array(..) if ... => todo!(),
298 TyKind::Array(..) | TyKind::Slice(..) => todo!(),
299 &TyKind::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ref _substs) => {
300 let enum_data = cx.db.enum_data(enum_id);
301
302 // If the enum is declared as `#[non_exhaustive]`, we treat it as if it had an
303 // additional "unknown" constructor.
304 // There is no point in enumerating all possible variants, because the user can't
305 // actually match against them all themselves. So we always return only the fictitious
306 // constructor.
307 // E.g., in an example like:
308 //
309 // ```
310 // let err: io::ErrorKind = ...;
311 // match err {
312 // io::ErrorKind::NotFound => {},
313 // }
314 // ```
315 //
316 // we don't want to show every possible IO error, but instead have only `_` as the
317 // witness.
318 let is_declared_nonexhaustive = cx.is_foreign_non_exhaustive_enum(enum_id);
319
320 // If `exhaustive_patterns` is disabled and our scrutinee is an empty enum, we treat it
321 // as though it had an "unknown" constructor to avoid exposing its emptiness. The
322 // exception is if the pattern is at the top level, because we want empty matches to be
323 // considered exhaustive.
324 let is_secretly_empty = enum_data.variants.is_empty()
325 && !cx.feature_exhaustive_patterns()
326 && !pcx.is_top_level;
327
328 if is_secretly_empty || is_declared_nonexhaustive {
329 smallvec![NonExhaustive]
330 } else if cx.feature_exhaustive_patterns() {
331 // If `exhaustive_patterns` is enabled, we exclude variants known to be
332 // uninhabited.
333 todo!()
334 } else {
335 enum_data
336 .variants
337 .iter()
338 .map(|(local_id, ..)| Variant(EnumVariantId { parent: enum_id, local_id }))
339 .collect()
340 }
341 }
342 TyKind::Scalar(Scalar::Char) => todo!(),
343 TyKind::Scalar(Scalar::Int(..)) | TyKind::Scalar(Scalar::Uint(..)) => todo!(),
344 TyKind::Never if !cx.feature_exhaustive_patterns() && !pcx.is_top_level => {
345 smallvec![NonExhaustive]
346 }
347 TyKind::Never => SmallVec::new(),
348 _ if cx.is_uninhabited(&pcx.ty) => SmallVec::new(),
296 TyKind::Adt(..) | TyKind::Tuple(..) | TyKind::Ref(..) => smallvec![Single], 349 TyKind::Adt(..) | TyKind::Tuple(..) | TyKind::Ref(..) => smallvec![Single],
297 _ => todo!(), 350 // This type is one for which we cannot list constructors, like `str` or `f64`.
351 _ => smallvec![NonExhaustive],
298 }; 352 };
299 SplitWildcard { matrix_ctors: Vec::new(), all_ctors } 353 SplitWildcard { matrix_ctors: Vec::new(), all_ctors }
300 } 354 }
@@ -496,7 +550,7 @@ impl Fields {
496 pub(super) fn apply(self, pcx: PatCtxt<'_>, ctor: &Constructor) -> Pat { 550 pub(super) fn apply(self, pcx: PatCtxt<'_>, ctor: &Constructor) -> Pat {
497 let subpatterns_and_indices = self.patterns_and_indices(); 551 let subpatterns_and_indices = self.patterns_and_indices();
498 let mut subpatterns = subpatterns_and_indices.iter().map(|&(_, p)| p); 552 let mut subpatterns = subpatterns_and_indices.iter().map(|&(_, p)| p);
499 // TODO witnesses are not yet used 553 // TODO witnesses are not yet used
500 const TODO: Pat = Pat::Wild; 554 const TODO: Pat = Pat::Wild;
501 555
502 match ctor { 556 match ctor {