Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/mir/lower/pattern_matching.rs')
| -rw-r--r-- | crates/hir-ty/src/mir/lower/pattern_matching.rs | 86 |
1 files changed, 33 insertions, 53 deletions
diff --git a/crates/hir-ty/src/mir/lower/pattern_matching.rs b/crates/hir-ty/src/mir/lower/pattern_matching.rs index 98c2e7c63b..65ab12929d 100644 --- a/crates/hir-ty/src/mir/lower/pattern_matching.rs +++ b/crates/hir-ty/src/mir/lower/pattern_matching.rs @@ -58,7 +58,7 @@ impl MirLowerCtx<'_> { let (current, current_else) = self.pattern_match_inner( current, current_else, - cond_place.clone(), + cond_place, pattern, MatchingMode::Check, )?; @@ -114,7 +114,7 @@ impl MirLowerCtx<'_> { index: i as u32, })) }), - &(&mut cond_place), + &mut cond_place, mode, )? } @@ -125,7 +125,7 @@ impl MirLowerCtx<'_> { let (mut next, next_else) = self.pattern_match_inner( current, None, - (&mut cond_place).clone(), + cond_place, *pat, MatchingMode::Check, )?; @@ -133,7 +133,7 @@ impl MirLowerCtx<'_> { (next, _) = self.pattern_match_inner( next, None, - (&mut cond_place).clone(), + cond_place, *pat, MatchingMode::Bind, )?; @@ -169,7 +169,7 @@ impl MirLowerCtx<'_> { current, pattern.into(), current_else, - AdtPatternShape::Record { args: &*args }, + AdtPatternShape::Record { args }, mode, )? } @@ -183,12 +183,8 @@ impl MirLowerCtx<'_> { self.temp(TyBuilder::bool(), current, pattern.into())?.into(); self.push_assignment( current, - discr.clone(), - Rvalue::CheckedBinaryOp( - binop, - lv, - Operand::Copy((&mut cond_place).clone()), - ), + discr, + Rvalue::CheckedBinaryOp(binop, lv, Operand::Copy(cond_place)), pattern.into(), ); let discr = Operand::Copy(discr); @@ -222,8 +218,8 @@ impl MirLowerCtx<'_> { self.temp(TyBuilder::usize(), current, pattern.into())?.into(); self.push_assignment( current, - place_len.clone(), - Rvalue::Len((&mut cond_place).clone()), + place_len, + Rvalue::Len(cond_place), pattern.into(), ); let else_target = @@ -252,7 +248,7 @@ impl MirLowerCtx<'_> { self.temp(TyBuilder::bool(), current, pattern.into())?.into(); self.push_assignment( current, - discr.clone(), + discr, Rvalue::CheckedBinaryOp(BinOp::Le, c, Operand::Copy(place_len)), pattern.into(), ); @@ -270,7 +266,7 @@ impl MirLowerCtx<'_> { } } for (i, &pat) in prefix.iter().enumerate() { - let next_place = (&mut cond_place).project( + let next_place = cond_place.project( ProjectionElem::ConstantIndex { offset: i as u64, from_end: false }, &mut self.result.projection_store, ); @@ -280,7 +276,7 @@ impl MirLowerCtx<'_> { if let Some(slice) = slice { if mode == MatchingMode::Bind { if let Pat::Bind { id, subpat: _ } = self.body[*slice] { - let next_place = (&mut cond_place).project( + let next_place = cond_place.project( ProjectionElem::Subslice { from: prefix.len() as u64, to: suffix.len() as u64, @@ -299,7 +295,7 @@ impl MirLowerCtx<'_> { } } for (i, &pat) in suffix.iter().enumerate() { - let next_place = (&mut cond_place).project( + let next_place = cond_place.project( ProjectionElem::ConstantIndex { offset: i as u64, from_end: true }, &mut self.result.projection_store, ); @@ -335,10 +331,8 @@ impl MirLowerCtx<'_> { break 'b (c, x.1); } } - if let ResolveValueResult::ValueNs(v, _) = pr { - if let ValueNs::ConstId(c) = v { - break 'b (c, Substitution::empty(Interner)); - } + if let ResolveValueResult::ValueNs(ValueNs::ConstId(c), _) = pr { + break 'b (c, Substitution::empty(Interner)); } not_supported!("path in pattern position that is not const or variant") }; @@ -348,7 +342,7 @@ impl MirLowerCtx<'_> { self.lower_const( c.into(), current, - tmp.clone(), + tmp, subst, span, self.infer[pattern].clone(), @@ -356,7 +350,7 @@ impl MirLowerCtx<'_> { let tmp2: Place = self.temp(TyBuilder::bool(), current, pattern.into())?.into(); self.push_assignment( current, - tmp2.clone(), + tmp2, Rvalue::CheckedBinaryOp( BinOp::Eq, Operand::Copy(tmp), @@ -390,13 +384,8 @@ impl MirLowerCtx<'_> { }, Pat::Bind { id, subpat } => { if let Some(subpat) = subpat { - (current, current_else) = self.pattern_match_inner( - current, - current_else, - (&mut cond_place).clone(), - *subpat, - mode, - )? + (current, current_else) = + self.pattern_match_inner(current, current_else, cond_place, *subpat, mode)? } if mode == MatchingMode::Bind { self.pattern_match_binding( @@ -475,7 +464,7 @@ impl MirLowerCtx<'_> { let discr: Place = self.temp(TyBuilder::bool(), current, pattern.into())?.into(); self.push_assignment( current, - discr.clone(), + discr, Rvalue::CheckedBinaryOp(BinOp::Eq, c, Operand::Copy(cond_place)), pattern.into(), ); @@ -506,12 +495,7 @@ impl MirLowerCtx<'_> { if mode == MatchingMode::Check { let e = self.const_eval_discriminant(v)? as u128; let tmp = self.discr_temp_place(current); - self.push_assignment( - current, - tmp.clone(), - Rvalue::Discriminant(cond_place.clone()), - span, - ); + self.push_assignment(current, tmp, Rvalue::Discriminant(cond_place), span); let next = self.new_basic_block(); let else_target = current_else.get_or_insert_with(|| self.new_basic_block()); self.set_terminator( @@ -524,22 +508,9 @@ impl MirLowerCtx<'_> { ); current = next; } - let enum_data = self.db.enum_data(v.parent); - self.pattern_matching_variant_fields( - shape, - &enum_data.variants[v.local_id].variant_data, - variant, - current, - current_else, - &cond_place, - mode, - )? - } - VariantId::StructId(s) => { - let struct_data = self.db.struct_data(s); self.pattern_matching_variant_fields( shape, - &struct_data.variant_data, + &self.db.enum_variant_data(v).variant_data, variant, current, current_else, @@ -547,6 +518,15 @@ impl MirLowerCtx<'_> { mode, )? } + VariantId::StructId(s) => self.pattern_matching_variant_fields( + shape, + &self.db.struct_data(s).variant_data, + variant, + current, + current_else, + &cond_place, + mode, + )?, VariantId::UnionId(_) => { return Err(MirLowerError::TypeError("pattern matching on union")) } @@ -572,7 +552,7 @@ impl MirLowerCtx<'_> { variant_data.field(&x.name).ok_or(MirLowerError::UnresolvedField)?; Ok(( PlaceElem::Field(Either::Left(FieldId { - parent: v.into(), + parent: v, local_id: field_id, })), x.pat, @@ -583,7 +563,7 @@ impl MirLowerCtx<'_> { } AdtPatternShape::Tuple { args, ellipsis } => { let fields = variant_data.fields().iter().map(|(x, _)| { - PlaceElem::Field(Either::Left(FieldId { parent: v.into(), local_id: x })) + PlaceElem::Field(Either::Left(FieldId { parent: v, local_id: x })) }); self.pattern_match_tuple_like( current, |