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.rs160
1 files changed, 74 insertions, 86 deletions
diff --git a/crates/hir-ty/src/mir/lower/pattern_matching.rs b/crates/hir-ty/src/mir/lower/pattern_matching.rs
index 359c4fbb2e..b1b86ab2c6 100644
--- a/crates/hir-ty/src/mir/lower/pattern_matching.rs
+++ b/crates/hir-ty/src/mir/lower/pattern_matching.rs
@@ -1,18 +1,18 @@
//! MIR lowering for patterns
use hir_def::{AssocItemId, hir::ExprId, signatures::VariantFields};
+use rustc_type_ir::inherent::{IntoKind, SliceLike, Ty as _};
-use crate::next_solver::mapping::NextSolverToChalk;
+use crate::next_solver::GenericArgs;
use crate::{
BindingMode,
mir::{
LocalId, MutBorrowKind, Operand, OperandKind,
lower::{
- BasicBlockId, BinOp, BindingId, BorrowKind, Either, Expr, FieldId, Idx, Interner,
- MemoryMap, MirLowerCtx, MirLowerError, MirSpan, Pat, PatId, Place, PlaceElem,
- ProjectionElem, RecordFieldPat, ResolveValueResult, Result, Rvalue, Substitution,
- SwitchTargets, TerminatorKind, TupleFieldId, TupleId, TyBuilder, TyKind, ValueNs,
- VariantId,
+ BasicBlockId, BinOp, BindingId, BorrowKind, Either, Expr, FieldId, Idx, MemoryMap,
+ MirLowerCtx, MirLowerError, MirSpan, Pat, PatId, Place, PlaceElem, ProjectionElem,
+ RecordFieldPat, ResolveValueResult, Result, Rvalue, SwitchTargets, TerminatorKind,
+ TupleFieldId, TupleId, Ty, TyKind, ValueNs, VariantId,
},
},
};
@@ -63,11 +63,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
/// so it should be an empty block.
pub(super) fn pattern_match(
&mut self,
- current: BasicBlockId,
- current_else: Option<BasicBlockId>,
- cond_place: Place,
+ current: BasicBlockId<'db>,
+ current_else: Option<BasicBlockId<'db>>,
+ cond_place: Place<'db>,
pattern: PatId,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
let (current, current_else) = self.pattern_match_inner(
current,
current_else,
@@ -87,10 +87,10 @@ impl<'db> MirLowerCtx<'_, 'db> {
pub(super) fn pattern_match_assignment(
&mut self,
- current: BasicBlockId,
- value: Place,
+ current: BasicBlockId<'db>,
+ value: Place<'db>,
pattern: PatId,
- ) -> Result<'db, BasicBlockId> {
+ ) -> Result<'db, BasicBlockId<'db>> {
let (current, _) =
self.pattern_match_inner(current, None, value, pattern, MatchingMode::Assign)?;
Ok(current)
@@ -99,9 +99,9 @@ impl<'db> MirLowerCtx<'_, 'db> {
pub(super) fn match_self_param(
&mut self,
id: BindingId,
- current: BasicBlockId,
- local: LocalId,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ current: BasicBlockId<'db>,
+ local: LocalId<'db>,
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
self.pattern_match_binding(
id,
BindingMode::Move,
@@ -114,12 +114,12 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn pattern_match_inner(
&mut self,
- mut current: BasicBlockId,
- mut current_else: Option<BasicBlockId>,
- mut cond_place: Place,
+ mut current: BasicBlockId<'db>,
+ mut current_else: Option<BasicBlockId<'db>>,
+ mut cond_place: Place<'db>,
pattern: PatId,
mode: MatchingMode,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
let cnt = self.infer.pat_adjustments.get(&pattern).map(|x| x.len()).unwrap_or_default();
cond_place.projection = self.result.projection_store.intern(
cond_place
@@ -135,8 +135,8 @@ impl<'db> MirLowerCtx<'_, 'db> {
Pat::Missing => return Err(MirLowerError::IncompletePattern),
Pat::Wild => (current, current_else),
Pat::Tuple { args, ellipsis } => {
- let subst = match self.infer[pattern].to_chalk(self.interner).kind(Interner) {
- TyKind::Tuple(_, s) => s.clone(),
+ let subst = match self.infer[pattern].kind() {
+ TyKind::Tuple(s) => s,
_ => {
return Err(MirLowerError::TypeError(
"non tuple type matched with tuple pattern",
@@ -148,7 +148,7 @@ impl<'db> MirLowerCtx<'_, 'db> {
current_else,
args,
*ellipsis,
- (0..subst.len(Interner)).map(|i| {
+ (0..subst.len()).map(|i| {
PlaceElem::Field(Either::Right(TupleFieldId {
tuple: TupleId(!0), // Dummy as it is unused
index: i as u32,
@@ -209,14 +209,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
}
Pat::Range { start, end } => {
let mut add_check = |l: &ExprId, binop| -> Result<'db, ()> {
- let lv = self.lower_literal_or_const_to_operand(
- self.infer[pattern].to_chalk(self.interner),
- l,
- )?;
+ let lv = self.lower_literal_or_const_to_operand(self.infer[pattern], l)?;
let else_target = *current_else.get_or_insert_with(|| self.new_basic_block());
let next = self.new_basic_block();
- let discr: Place =
- self.temp(TyBuilder::bool(), current, pattern.into())?.into();
+ let discr: Place<'db> =
+ self.temp(Ty::new_bool(self.interner()), current, pattern.into())?.into();
self.push_assignment(
current,
discr,
@@ -252,12 +249,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
Pat::Slice { prefix, slice, suffix } => {
if mode == MatchingMode::Check {
// emit runtime length check for slice
- if let TyKind::Slice(_) =
- self.infer[pattern].to_chalk(self.interner).kind(Interner)
- {
+ if let TyKind::Slice(_) = self.infer[pattern].kind() {
let pattern_len = prefix.len() + suffix.len();
- let place_len: Place =
- self.temp(TyBuilder::usize(), current, pattern.into())?.into();
+ let place_len: Place<'db> = self
+ .temp(Ty::new_usize(self.interner()), current, pattern.into())?
+ .into();
self.push_assignment(
current,
place_len,
@@ -287,10 +283,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
let c = Operand::from_concrete_const(
pattern_len.to_le_bytes().into(),
MemoryMap::default(),
- TyBuilder::usize(),
+ Ty::new_usize(self.interner()),
);
- let discr: Place =
- self.temp(TyBuilder::bool(), current, pattern.into())?.into();
+ let discr: Place<'db> = self
+ .temp(Ty::new_bool(self.interner()), current, pattern.into())?
+ .into();
self.push_assignment(
current,
discr,
@@ -398,26 +395,19 @@ impl<'db> MirLowerCtx<'_, 'db> {
if let Some(x) = self.infer.assoc_resolutions_for_pat(pattern)
&& let AssocItemId::ConstId(c) = x.0
{
- break 'b (c, x.1.to_chalk(self.interner));
+ break 'b (c, x.1);
}
if let ResolveValueResult::ValueNs(ValueNs::ConstId(c), _) = pr {
- break 'b (c, Substitution::empty(Interner));
+ break 'b (c, GenericArgs::new_from_iter(self.interner(), []));
}
not_supported!("path in pattern position that is not const or variant")
};
- let tmp: Place = self
- .temp(self.infer[pattern].to_chalk(self.interner), current, pattern.into())?
- .into();
+ let tmp: Place<'db> =
+ self.temp(self.infer[pattern], current, pattern.into())?.into();
let span = pattern.into();
- self.lower_const(
- c.into(),
- current,
- tmp,
- subst,
- span,
- self.infer[pattern].to_chalk(self.interner),
- )?;
- let tmp2: Place = self.temp(TyBuilder::bool(), current, pattern.into())?.into();
+ self.lower_const(c.into(), current, tmp, subst, span)?;
+ let tmp2: Place<'db> =
+ self.temp(Ty::new_bool(self.interner()), current, pattern.into())?.into();
self.push_assignment(
current,
tmp2,
@@ -444,10 +434,7 @@ impl<'db> MirLowerCtx<'_, 'db> {
Pat::Lit(l) => match &self.body[*l] {
Expr::Literal(l) => {
if mode == MatchingMode::Check {
- let c = self.lower_literal_to_operand(
- self.infer[pattern].to_chalk(self.interner),
- l,
- )?;
+ let c = self.lower_literal_to_operand(self.infer[pattern], l)?;
self.pattern_match_const(current_else, current, c, cond_place, pattern)?
} else {
(current, current_else)
@@ -519,11 +506,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
&mut self,
id: BindingId,
mode: BindingMode,
- cond_place: Place,
+ cond_place: Place<'db>,
span: MirSpan,
- current: BasicBlockId,
- current_else: Option<BasicBlockId>,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ current: BasicBlockId<'db>,
+ current_else: Option<BasicBlockId<'db>>,
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
let target_place = self.binding_local(id)?;
self.push_storage_live(id, current)?;
self.push_match_assignment(current, target_place, mode, cond_place, span);
@@ -532,10 +519,10 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn push_match_assignment(
&mut self,
- current: BasicBlockId,
- target_place: LocalId,
+ current: BasicBlockId<'db>,
+ target_place: LocalId<'db>,
mode: BindingMode,
- cond_place: Place,
+ cond_place: Place<'db>,
span: MirSpan,
) {
self.push_assignment(
@@ -558,15 +545,16 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn pattern_match_const(
&mut self,
- current_else: Option<BasicBlockId>,
- current: BasicBlockId,
- c: Operand,
- cond_place: Place,
+ current_else: Option<BasicBlockId<'db>>,
+ current: BasicBlockId<'db>,
+ c: Operand<'db>,
+ cond_place: Place<'db>,
pattern: Idx<Pat>,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
let then_target = self.new_basic_block();
let else_target = current_else.unwrap_or_else(|| self.new_basic_block());
- let discr: Place = self.temp(TyBuilder::bool(), current, pattern.into())?.into();
+ let discr: Place<'db> =
+ self.temp(Ty::new_bool(self.interner()), current, pattern.into())?.into();
self.push_assignment(
current,
discr,
@@ -591,14 +579,14 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn pattern_matching_variant(
&mut self,
- cond_place: Place,
+ cond_place: Place<'db>,
variant: VariantId,
- mut current: BasicBlockId,
+ mut current: BasicBlockId<'db>,
span: MirSpan,
- mut current_else: Option<BasicBlockId>,
+ mut current_else: Option<BasicBlockId<'db>>,
shape: AdtPatternShape<'_>,
mode: MatchingMode,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
Ok(match variant {
VariantId::EnumVariantId(v) => {
if mode == MatchingMode::Check {
@@ -647,11 +635,11 @@ impl<'db> MirLowerCtx<'_, 'db> {
shape: AdtPatternShape<'_>,
variant_data: &VariantFields,
v: VariantId,
- current: BasicBlockId,
- current_else: Option<BasicBlockId>,
- cond_place: &Place,
+ current: BasicBlockId<'db>,
+ current_else: Option<BasicBlockId<'db>>,
+ cond_place: &Place<'db>,
mode: MatchingMode,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
Ok(match shape {
AdtPatternShape::Record { args } => {
let it = args
@@ -690,12 +678,12 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn pattern_match_adt(
&mut self,
- mut current: BasicBlockId,
- mut current_else: Option<BasicBlockId>,
- args: impl Iterator<Item = (PlaceElem, PatId)>,
- cond_place: &Place,
+ mut current: BasicBlockId<'db>,
+ mut current_else: Option<BasicBlockId<'db>>,
+ args: impl Iterator<Item = (PlaceElem<'db>, PatId)>,
+ cond_place: &Place<'db>,
mode: MatchingMode,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
for (proj, arg) in args {
let cond_place = cond_place.project(proj, &mut self.result.projection_store);
(current, current_else) =
@@ -706,14 +694,14 @@ impl<'db> MirLowerCtx<'_, 'db> {
fn pattern_match_tuple_like(
&mut self,
- current: BasicBlockId,
- current_else: Option<BasicBlockId>,
+ current: BasicBlockId<'db>,
+ current_else: Option<BasicBlockId<'db>>,
args: &[PatId],
ellipsis: Option<u32>,
- fields: impl DoubleEndedIterator<Item = PlaceElem> + Clone,
- cond_place: &Place,
+ fields: impl DoubleEndedIterator<Item = PlaceElem<'db>> + Clone,
+ cond_place: &Place<'db>,
mode: MatchingMode,
- ) -> Result<'db, (BasicBlockId, Option<BasicBlockId>)> {
+ ) -> Result<'db, (BasicBlockId<'db>, Option<BasicBlockId<'db>>)> {
let (al, ar) = args.split_at(ellipsis.map_or(args.len(), |it| it as usize));
let it = al
.iter()