Skip to content

Commit a3789ea

Browse files
committed
Minor refactorings
- use `DefWithBodyId::as_generic_def_id()` - add comments on `InferenceResult` invariant - move local helper function to bottom to comply with style guide
1 parent 275afd6 commit a3789ea

File tree

3 files changed

+29
-46
lines changed

3 files changed

+29
-46
lines changed

crates/hir-ty/src/infer.rs

+6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ pub enum PointerCast {
367367
}
368368

369369
/// The result of type inference: A mapping from expressions and patterns to types.
370+
///
371+
/// When you add a field that stores types (including `Substitution` and the like), don't forget
372+
/// `resolve_completely()`'ing them in `InferenceContext::resolve_all()`. Inference variables must
373+
/// not appear in the final inference result.
370374
#[derive(Clone, PartialEq, Eq, Debug, Default)]
371375
pub struct InferenceResult {
372376
/// For each method call expr, records the function it resolves to.
@@ -575,6 +579,8 @@ impl<'a> InferenceContext<'a> {
575579
// used this function for another workaround, mention it here. If you really need this function and believe that
576580
// there is no problem in it being `pub(crate)`, remove this comment.
577581
pub(crate) fn resolve_all(self) -> InferenceResult {
582+
// NOTE: `InferenceResult::closure_info` is `resolve_completely()`'d during
583+
// `InferenceContext::infer_closures()` (in `HirPlace::ty()` specifically).
578584
let InferenceContext { mut table, mut result, .. } = self;
579585

580586
table.fallback_if_possible();

crates/hir-ty/src/infer/closure.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ pub(crate) struct CapturedItemWithoutTy {
236236

237237
impl CapturedItemWithoutTy {
238238
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
239+
let ty = self.place.ty(ctx).clone();
240+
let ty = match &self.kind {
241+
CaptureKind::ByValue => ty,
242+
CaptureKind::ByRef(bk) => {
243+
let m = match bk {
244+
BorrowKind::Mut { .. } => Mutability::Mut,
245+
_ => Mutability::Not,
246+
};
247+
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
248+
}
249+
};
250+
return CapturedItem {
251+
place: self.place,
252+
kind: self.kind,
253+
span: self.span,
254+
ty: replace_placeholder_with_binder(ctx.db, ctx.owner, ty),
255+
};
256+
239257
fn replace_placeholder_with_binder(
240258
db: &dyn HirDatabase,
241259
owner: DefWithBodyId,
@@ -281,36 +299,13 @@ impl CapturedItemWithoutTy {
281299
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))
282300
}
283301
}
284-
let g_def = match owner {
285-
DefWithBodyId::FunctionId(f) => Some(f.into()),
286-
DefWithBodyId::StaticId(_) => None,
287-
DefWithBodyId::ConstId(f) => Some(f.into()),
288-
DefWithBodyId::VariantId(f) => Some(f.into()),
289-
};
290-
let Some(generics) = g_def.map(|g_def| generics(db.upcast(), g_def)) else {
302+
let Some(generic_def) = owner.as_generic_def_id() else {
291303
return Binders::empty(Interner, ty);
292304
};
293-
let filler = &mut Filler { db, generics };
305+
let filler = &mut Filler { db, generics: generics(db.upcast(), generic_def) };
294306
let result = ty.clone().try_fold_with(filler, DebruijnIndex::INNERMOST).unwrap_or(ty);
295307
make_binders(db, &filler.generics, result)
296308
}
297-
let ty = self.place.ty(ctx).clone();
298-
let ty = match &self.kind {
299-
CaptureKind::ByValue => ty,
300-
CaptureKind::ByRef(bk) => {
301-
let m = match bk {
302-
BorrowKind::Mut { .. } => Mutability::Mut,
303-
_ => Mutability::Not,
304-
};
305-
TyKind::Ref(m, static_lifetime(), ty).intern(Interner)
306-
}
307-
};
308-
CapturedItem {
309-
place: self.place,
310-
kind: self.kind,
311-
span: self.span,
312-
ty: replace_placeholder_with_binder(ctx.db, ctx.owner, ty),
313-
}
314309
}
315310
}
316311

crates/hir-ty/src/mir/monomorphization.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,7 @@ pub fn monomorphized_mir_body_query(
303303
subst: Substitution,
304304
trait_env: Arc<crate::TraitEnvironment>,
305305
) -> Result<Arc<MirBody>, MirLowerError> {
306-
let g_def = match owner {
307-
DefWithBodyId::FunctionId(f) => Some(f.into()),
308-
DefWithBodyId::StaticId(_) => None,
309-
DefWithBodyId::ConstId(f) => Some(f.into()),
310-
DefWithBodyId::VariantId(f) => Some(f.into()),
311-
};
312-
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
306+
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
313307
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
314308
let body = db.mir_body(owner)?;
315309
let mut body = (*body).clone();
@@ -334,13 +328,7 @@ pub fn monomorphized_mir_body_for_closure_query(
334328
trait_env: Arc<crate::TraitEnvironment>,
335329
) -> Result<Arc<MirBody>, MirLowerError> {
336330
let (owner, _) = db.lookup_intern_closure(closure.into());
337-
let g_def = match owner {
338-
DefWithBodyId::FunctionId(f) => Some(f.into()),
339-
DefWithBodyId::StaticId(_) => None,
340-
DefWithBodyId::ConstId(f) => Some(f.into()),
341-
DefWithBodyId::VariantId(f) => Some(f.into()),
342-
};
343-
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
331+
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
344332
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
345333
let body = db.mir_body_for_closure(closure)?;
346334
let mut body = (*body).clone();
@@ -356,13 +344,7 @@ pub fn monomorphize_mir_body_bad(
356344
trait_env: Arc<crate::TraitEnvironment>,
357345
) -> Result<MirBody, MirLowerError> {
358346
let owner = body.owner;
359-
let g_def = match owner {
360-
DefWithBodyId::FunctionId(f) => Some(f.into()),
361-
DefWithBodyId::StaticId(_) => None,
362-
DefWithBodyId::ConstId(f) => Some(f.into()),
363-
DefWithBodyId::VariantId(f) => Some(f.into()),
364-
};
365-
let generics = g_def.map(|g_def| generics(db.upcast(), g_def));
347+
let generics = owner.as_generic_def_id().map(|g_def| generics(db.upcast(), g_def));
366348
let filler = &mut Filler { db, subst: &subst, trait_env, generics, owner };
367349
filler.fill_body(&mut body)?;
368350
Ok(body)

0 commit comments

Comments
 (0)