Minor refactorings
- use `DefWithBodyId::as_generic_def_id()` - add comments on `InferenceResult` invariant - move local helper function to bottom to comply with style guide
This commit is contained in:
parent
275afd6e79
commit
a3789eabc9
@ -367,6 +367,10 @@ pub enum PointerCast {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The result of type inference: A mapping from expressions and patterns to types.
|
/// The result of type inference: A mapping from expressions and patterns to types.
|
||||||
|
///
|
||||||
|
/// When you add a field that stores types (including `Substitution` and the like), don't forget
|
||||||
|
/// `resolve_completely()`'ing them in `InferenceContext::resolve_all()`. Inference variables must
|
||||||
|
/// not appear in the final inference result.
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Default)]
|
#[derive(Clone, PartialEq, Eq, Debug, Default)]
|
||||||
pub struct InferenceResult {
|
pub struct InferenceResult {
|
||||||
/// For each method call expr, records the function it resolves to.
|
/// For each method call expr, records the function it resolves to.
|
||||||
@ -575,6 +579,8 @@ impl<'a> InferenceContext<'a> {
|
|||||||
// used this function for another workaround, mention it here. If you really need this function and believe that
|
// used this function for another workaround, mention it here. If you really need this function and believe that
|
||||||
// there is no problem in it being `pub(crate)`, remove this comment.
|
// there is no problem in it being `pub(crate)`, remove this comment.
|
||||||
pub(crate) fn resolve_all(self) -> InferenceResult {
|
pub(crate) fn resolve_all(self) -> InferenceResult {
|
||||||
|
// NOTE: `InferenceResult::closure_info` is `resolve_completely()`'d during
|
||||||
|
// `InferenceContext::infer_closures()` (in `HirPlace::ty()` specifically).
|
||||||
let InferenceContext { mut table, mut result, .. } = self;
|
let InferenceContext { mut table, mut result, .. } = self;
|
||||||
|
|
||||||
table.fallback_if_possible();
|
table.fallback_if_possible();
|
||||||
|
@ -236,6 +236,24 @@ pub(crate) struct CapturedItemWithoutTy {
|
|||||||
|
|
||||||
impl CapturedItemWithoutTy {
|
impl CapturedItemWithoutTy {
|
||||||
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
|
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
|
||||||
|
let ty = self.place.ty(ctx).clone();
|
||||||
|
let ty = match &self.kind {
|
||||||
|
CaptureKind::ByValue => ty,
|
||||||
|
CaptureKind::ByRef(bk) => {
|
||||||
|
let m = match bk {
|
||||||
|
BorrowKind::Mut { .. } => Mutability::Mut,
|
||||||
|
_ => Mutability::Not,
|
||||||
|
};
|
||||||
|
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return CapturedItem {
|
||||||
|
place: self.place,
|
||||||
|
kind: self.kind,
|
||||||
|
span: self.span,
|
||||||
|
ty: replace_placeholder_with_binder(ctx.db, ctx.owner, ty),
|
||||||
|
};
|
||||||
|
|
||||||
fn replace_placeholder_with_binder(
|
fn replace_placeholder_with_binder(
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
owner: DefWithBodyId,
|
owner: DefWithBodyId,
|
||||||
@ -281,36 +299,13 @@ impl CapturedItemWithoutTy {
|
|||||||
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))
|
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let g_def = match owner {
|
let Some(generic_def) = owner.as_generic_def_id() else {
|
||||||
DefWithBodyId::FunctionId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::StaticId(_) => None,
|
|
||||||
DefWithBodyId::ConstId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::VariantId(f) => Some(f.into()),
|
|
||||||
};
|
|
||||||
let Some(generics) = g_def.map(|g_def| generics(db.upcast(), g_def)) else {
|
|
||||||
return Binders::empty(Interner, ty);
|
return Binders::empty(Interner, ty);
|
||||||
};
|
};
|
||||||
let filler = &mut Filler { db, generics };
|
let filler = &mut Filler { db, generics: generics(db.upcast(), generic_def) };
|
||||||
let result = ty.clone().try_fold_with(filler, DebruijnIndex::INNERMOST).unwrap_or(ty);
|
let result = ty.clone().try_fold_with(filler, DebruijnIndex::INNERMOST).unwrap_or(ty);
|
||||||
make_binders(db, &filler.generics, result)
|
make_binders(db, &filler.generics, result)
|
||||||
}
|
}
|
||||||
let ty = self.place.ty(ctx).clone();
|
|
||||||
let ty = match &self.kind {
|
|
||||||
CaptureKind::ByValue => ty,
|
|
||||||
CaptureKind::ByRef(bk) => {
|
|
||||||
let m = match bk {
|
|
||||||
BorrowKind::Mut { .. } => Mutability::Mut,
|
|
||||||
_ => Mutability::Not,
|
|
||||||
};
|
|
||||||
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
CapturedItem {
|
|
||||||
place: self.place,
|
|
||||||
kind: self.kind,
|
|
||||||
span: self.span,
|
|
||||||
ty: replace_placeholder_with_binder(ctx.db, ctx.owner, ty),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,13 +303,7 @@ pub fn monomorphized_mir_body_query(
|
|||||||
subst: Substitution,
|
subst: Substitution,
|
||||||
trait_env: Arc<crate::TraitEnvironment>,
|
trait_env: Arc<crate::TraitEnvironment>,
|
||||||
) -> Result<Arc<MirBody>, MirLowerError> {
|
) -> Result<Arc<MirBody>, MirLowerError> {
|
||||||
let g_def = match owner {
|
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
|
||||||
DefWithBodyId::FunctionId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::StaticId(_) => None,
|
|
||||||
DefWithBodyId::ConstId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::VariantId(f) => Some(f.into()),
|
|
||||||
};
|
|
||||||
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
|
|
||||||
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
||||||
let body = db.mir_body(owner)?;
|
let body = db.mir_body(owner)?;
|
||||||
let mut body = (*body).clone();
|
let mut body = (*body).clone();
|
||||||
@ -334,13 +328,7 @@ pub fn monomorphized_mir_body_for_closure_query(
|
|||||||
trait_env: Arc<crate::TraitEnvironment>,
|
trait_env: Arc<crate::TraitEnvironment>,
|
||||||
) -> Result<Arc<MirBody>, MirLowerError> {
|
) -> Result<Arc<MirBody>, MirLowerError> {
|
||||||
let (owner, _) = db.lookup_intern_closure(closure.into());
|
let (owner, _) = db.lookup_intern_closure(closure.into());
|
||||||
let g_def = match owner {
|
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
|
||||||
DefWithBodyId::FunctionId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::StaticId(_) => None,
|
|
||||||
DefWithBodyId::ConstId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::VariantId(f) => Some(f.into()),
|
|
||||||
};
|
|
||||||
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
|
|
||||||
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
||||||
let body = db.mir_body_for_closure(closure)?;
|
let body = db.mir_body_for_closure(closure)?;
|
||||||
let mut body = (*body).clone();
|
let mut body = (*body).clone();
|
||||||
@ -356,13 +344,7 @@ pub fn monomorphize_mir_body_bad(
|
|||||||
trait_env: Arc<crate::TraitEnvironment>,
|
trait_env: Arc<crate::TraitEnvironment>,
|
||||||
) -> Result<MirBody, MirLowerError> {
|
) -> Result<MirBody, MirLowerError> {
|
||||||
let owner = body.owner;
|
let owner = body.owner;
|
||||||
let g_def = match owner {
|
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
|
||||||
DefWithBodyId::FunctionId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::StaticId(_) => None,
|
|
||||||
DefWithBodyId::ConstId(f) => Some(f.into()),
|
|
||||||
DefWithBodyId::VariantId(f) => Some(f.into()),
|
|
||||||
};
|
|
||||||
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
|
|
||||||
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
|
||||||
filler.fill_body(&mut body)?;
|
filler.fill_body(&mut body)?;
|
||||||
Ok(body)
|
Ok(body)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user