aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-04-29 10:19:01 +0100
committerDawer <[email protected]>2021-05-31 20:03:46 +0100
commit062c7953a12bf849db641d14d8d26af47ddca79e (patch)
treeb65a58bb638cd9f10a567a528d62d1ff04e2751c /crates
parent2880fd23206042cc3564d388d2f991cb91e76c39 (diff)
Add remaining Constructor variants
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs70
1 files changed, 56 insertions, 14 deletions
diff --git a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
index d41b95d2f..c02e783af 100644
--- a/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
+++ b/crates/hir_ty/src/diagnostics/pattern/deconstruct_pat.rs
@@ -31,6 +31,10 @@ impl IntRange {
31 } 31 }
32 } 32 }
33 33
34 fn is_singleton(&self) -> bool {
35 todo!()
36 }
37
34 /// See `Constructor::is_covered_by` 38 /// See `Constructor::is_covered_by`
35 fn is_covered_by(&self, other: &Self) -> bool { 39 fn is_covered_by(&self, other: &Self) -> bool {
36 todo!() 40 todo!()
@@ -66,8 +70,19 @@ pub(super) enum Constructor {
66 Variant(EnumVariantId), 70 Variant(EnumVariantId),
67 /// Ranges of integer literal values (`2`, `2..=5` or `2..5`). 71 /// Ranges of integer literal values (`2`, `2..=5` or `2..5`).
68 IntRange(IntRange), 72 IntRange(IntRange),
73 /// Ranges of floating-point literal values (`2.0..=5.2`).
74 FloatRange(ToDo),
75 /// String literals. Strings are not quite the same as `&[u8]` so we treat them separately.
76 Str(ToDo),
69 /// Array and slice patterns. 77 /// Array and slice patterns.
70 Slice(Slice), 78 Slice(Slice),
79 /// Constants that must not be matched structurally. They are treated as black
80 /// boxes for the purposes of exhaustiveness: we must not inspect them, and they
81 /// don't count towards making a match exhaustive.
82 Opaque,
83 /// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used
84 /// for those types for which we cannot list constructors explicitly, like `f64` and `str`.
85 NonExhaustive,
71 /// Stands for constructors that are not seen in the matrix, as explained in the documentation 86 /// Stands for constructors that are not seen in the matrix, as explained in the documentation
72 /// for [`SplitWildcard`]. 87 /// for [`SplitWildcard`].
73 Missing, 88 Missing,
@@ -154,7 +169,9 @@ impl Constructor {
154 } 169 }
155 // Fast-track if the range is trivial. In particular, we don't do the overlapping 170 // Fast-track if the range is trivial. In particular, we don't do the overlapping
156 // ranges check. 171 // ranges check.
157 IntRange(_) => todo!("Constructor::split IntRange"), 172 IntRange(ctor_range) if !ctor_range.is_singleton() => {
173 todo!("Constructor::split IntRange")
174 }
158 Slice(_) => todo!("Constructor::split Slice"), 175 Slice(_) => todo!("Constructor::split Slice"),
159 // Any other constructor can be used unchanged. 176 // Any other constructor can be used unchanged.
160 _ => smallvec![self.clone()], 177 _ => smallvec![self.clone()],
@@ -177,11 +194,24 @@ impl Constructor {
177 (Single, Single) => true, 194 (Single, Single) => true,
178 (Variant(self_id), Variant(other_id)) => self_id == other_id, 195 (Variant(self_id), Variant(other_id)) => self_id == other_id,
179 196
180 (Constructor::IntRange(_), Constructor::IntRange(_)) => todo!(), 197 (IntRange(self_range), IntRange(other_range)) => self_range.is_covered_by(other_range),
198 (FloatRange(..), FloatRange(..)) => {
199 todo!()
200 }
201 (Str(self_val), Str(other_val)) => {
202 todo!()
203 }
204 (Slice(self_slice), Slice(other_slice)) => self_slice.is_covered_by(*other_slice),
181 205
182 (Constructor::Slice(_), Constructor::Slice(_)) => todo!(), 206 // We are trying to inspect an opaque constant. Thus we skip the row.
207 (Opaque, _) | (_, Opaque) => false,
208 // Only a wildcard pattern can match the special extra constructor.
209 (NonExhaustive, _) => false,
183 210
184 _ => panic!("bug"), 211 _ => panic!(
212 "bug: trying to compare incompatible constructors {:?} and {:?}",
213 self, other
214 ),
185 } 215 }
186 } 216 }
187 217
@@ -206,8 +236,11 @@ impl Constructor {
206 .iter() 236 .iter()
207 .filter_map(|c| c.as_slice()) 237 .filter_map(|c| c.as_slice())
208 .any(|other| slice.is_covered_by(other)), 238 .any(|other| slice.is_covered_by(other)),
209 239 // This constructor is never covered by anything else
210 _ => todo!(), 240 NonExhaustive => false,
241 Str(..) | FloatRange(..) | Opaque | Missing | Wildcard => {
242 panic!("bug: found unexpected ctor in all_ctors: {:?}", self)
243 }
211 } 244 }
212 } 245 }
213} 246}
@@ -425,8 +458,11 @@ impl Fields {
425 } 458 }
426 _ => panic!("Unexpected type for `Single` constructor: {:?}", ty), 459 _ => panic!("Unexpected type for `Single` constructor: {:?}", ty),
427 }, 460 },
428 Missing | Wildcard => Fields::Vec(Default::default()), 461 Slice(slice) => {
429 _ => todo!(), 462 todo!()
463 }
464 Str(..) | FloatRange(..) | IntRange(..) | NonExhaustive | Opaque | Missing
465 | Wildcard => Fields::Vec(Default::default()),
430 }; 466 };
431 ret 467 ret
432 } 468 }
@@ -492,12 +528,18 @@ impl Fields {
492 // TyKind::BoundVar(_) => {} 528 // TyKind::BoundVar(_) => {}
493 // TyKind::InferenceVar(_, _) => {} 529 // TyKind::InferenceVar(_, _) => {}
494 }, 530 },
495 531 Constructor::Slice(slice) => {
496 _ => todo!(), 532 todo!()
497 // Constructor::IntRange(_) => {} 533 }
498 // Constructor::Slice(_) => {} 534 Str(_) => todo!(),
499 // Missing => {} 535 FloatRange(..) => todo!(),
500 // Wildcard => {} 536 Constructor::IntRange(_) => todo!(),
537 NonExhaustive => Pat::Wild,
538 Wildcard => Pat::Wild,
539 Opaque => panic!("bug: we should not try to apply an opaque constructor"),
540 Missing => panic!(
541 "bug: trying to apply the `Missing` constructor; this should have been done in `apply_constructors`"
542 ),
501 } 543 }
502 } 544 }
503 545