Skip to content

Commit f82d230

Browse files
committed
Simplify
1 parent 17691ee commit f82d230

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

crates/hir_ty/src/infer.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -413,39 +413,70 @@ impl<'a> InferenceContext<'a> {
413413
}
414414
}
415415

416-
fn err_ty(&self) -> Ty {
417-
self.result.standard_types.unknown.clone()
418-
}
416+
fn resolve_all(self) -> InferenceResult {
417+
let InferenceContext { mut table, mut result, .. } = self;
419418

420-
fn resolve_all(mut self) -> InferenceResult {
421419
// FIXME resolve obligations as well (use Guidance if necessary)
422-
self.table.resolve_obligations_as_possible();
420+
table.resolve_obligations_as_possible();
423421

424422
// make sure diverging type variables are marked as such
425-
self.table.propagate_diverging_flag();
426-
let mut result = std::mem::take(&mut self.result);
423+
table.propagate_diverging_flag();
427424
for ty in result.type_of_expr.values_mut() {
428-
*ty = self.table.resolve_completely(ty.clone());
425+
*ty = table.resolve_completely(ty.clone());
429426
}
430427
for ty in result.type_of_pat.values_mut() {
431-
*ty = self.table.resolve_completely(ty.clone());
428+
*ty = table.resolve_completely(ty.clone());
432429
}
433430
for mismatch in result.type_mismatches.values_mut() {
434-
mismatch.expected = self.table.resolve_completely(mismatch.expected.clone());
435-
mismatch.actual = self.table.resolve_completely(mismatch.actual.clone());
431+
mismatch.expected = table.resolve_completely(mismatch.expected.clone());
432+
mismatch.actual = table.resolve_completely(mismatch.actual.clone());
436433
}
437434
for (_, subst) in result.method_resolutions.values_mut() {
438-
*subst = self.table.resolve_completely(subst.clone());
435+
*subst = table.resolve_completely(subst.clone());
439436
}
440437
for adjustment in result.expr_adjustments.values_mut().flatten() {
441-
adjustment.target = self.table.resolve_completely(adjustment.target.clone());
438+
adjustment.target = table.resolve_completely(adjustment.target.clone());
442439
}
443440
for adjustment in result.pat_adjustments.values_mut().flatten() {
444-
adjustment.target = self.table.resolve_completely(adjustment.target.clone());
441+
adjustment.target = table.resolve_completely(adjustment.target.clone());
445442
}
446443
result
447444
}
448445

446+
fn collect_const(&mut self, data: &ConstData) {
447+
self.return_ty = self.make_ty(&data.type_ref);
448+
}
449+
450+
fn collect_static(&mut self, data: &StaticData) {
451+
self.return_ty = self.make_ty(&data.type_ref);
452+
}
453+
454+
fn collect_fn(&mut self, data: &FunctionData) {
455+
let body = Arc::clone(&self.body); // avoid borrow checker problem
456+
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
457+
.with_impl_trait_mode(ImplTraitLoweringMode::Param);
458+
let param_tys =
459+
data.params.iter().map(|(_, type_ref)| ctx.lower_ty(type_ref)).collect::<Vec<_>>();
460+
for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) {
461+
let ty = self.insert_type_vars(ty);
462+
let ty = self.normalize_associated_types_in(ty);
463+
464+
self.infer_pat(*pat, &ty, BindingMode::default());
465+
}
466+
let error_ty = &TypeRef::Error;
467+
let return_ty = if data.has_async_kw() {
468+
data.async_ret_type.as_deref().unwrap_or(error_ty)
469+
} else {
470+
&*data.ret_type
471+
};
472+
let return_ty = self.make_ty_with_mode(return_ty, ImplTraitLoweringMode::Disallowed); // FIXME implement RPIT
473+
self.return_ty = return_ty;
474+
}
475+
476+
fn infer_body(&mut self) {
477+
self.infer_expr_coerce(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
478+
}
479+
449480
fn write_expr_ty(&mut self, expr: ExprId, ty: Ty) {
450481
self.result.type_of_expr.insert(expr, ty);
451482
}
@@ -491,6 +522,10 @@ impl<'a> InferenceContext<'a> {
491522
self.make_ty_with_mode(type_ref, ImplTraitLoweringMode::Disallowed)
492523
}
493524

525+
fn err_ty(&self) -> Ty {
526+
self.result.standard_types.unknown.clone()
527+
}
528+
494529
/// Replaces ConstScalar::Unknown by a new type var, so we can maybe still infer it.
495530
fn insert_const_vars_shallow(&mut self, c: Const) -> Const {
496531
let data = c.data(Interner);
@@ -544,6 +579,16 @@ impl<'a> InferenceContext<'a> {
544579
self.table.unify(ty1, ty2)
545580
}
546581

582+
/// Recurses through the given type, normalizing associated types mentioned
583+
/// in it by replacing them by type variables and registering obligations to
584+
/// resolve later. This should be done once for every type we get from some
585+
/// type annotation (e.g. from a let type annotation, field type or function
586+
/// call). `make_ty` handles this already, but e.g. for field types we need
587+
/// to do it as well.
588+
fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
589+
self.table.normalize_associated_types_in(ty)
590+
}
591+
547592
fn resolve_ty_shallow(&mut self, ty: &Ty) -> Ty {
548593
self.resolve_obligations_as_possible();
549594
self.table.resolve_ty_shallow(ty)
@@ -586,16 +631,6 @@ impl<'a> InferenceContext<'a> {
586631
}
587632
}
588633

589-
/// Recurses through the given type, normalizing associated types mentioned
590-
/// in it by replacing them by type variables and registering obligations to
591-
/// resolve later. This should be done once for every type we get from some
592-
/// type annotation (e.g. from a let type annotation, field type or function
593-
/// call). `make_ty` handles this already, but e.g. for field types we need
594-
/// to do it as well.
595-
fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
596-
self.table.normalize_associated_types_in(ty)
597-
}
598-
599634
fn resolve_variant(&mut self, path: Option<&Path>, value_ns: bool) -> (Ty, Option<VariantId>) {
600635
let path = match path {
601636
Some(path) => path,
@@ -727,40 +762,6 @@ impl<'a> InferenceContext<'a> {
727762
}
728763
}
729764

730-
fn collect_const(&mut self, data: &ConstData) {
731-
self.return_ty = self.make_ty(&data.type_ref);
732-
}
733-
734-
fn collect_static(&mut self, data: &StaticData) {
735-
self.return_ty = self.make_ty(&data.type_ref);
736-
}
737-
738-
fn collect_fn(&mut self, data: &FunctionData) {
739-
let body = Arc::clone(&self.body); // avoid borrow checker problem
740-
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
741-
.with_impl_trait_mode(ImplTraitLoweringMode::Param);
742-
let param_tys =
743-
data.params.iter().map(|(_, type_ref)| ctx.lower_ty(type_ref)).collect::<Vec<_>>();
744-
for (ty, pat) in param_tys.into_iter().zip(body.params.iter()) {
745-
let ty = self.insert_type_vars(ty);
746-
let ty = self.normalize_associated_types_in(ty);
747-
748-
self.infer_pat(*pat, &ty, BindingMode::default());
749-
}
750-
let error_ty = &TypeRef::Error;
751-
let return_ty = if data.has_async_kw() {
752-
data.async_ret_type.as_deref().unwrap_or(error_ty)
753-
} else {
754-
&*data.ret_type
755-
};
756-
let return_ty = self.make_ty_with_mode(return_ty, ImplTraitLoweringMode::Disallowed); // FIXME implement RPIT
757-
self.return_ty = return_ty;
758-
}
759-
760-
fn infer_body(&mut self) {
761-
self.infer_expr_coerce(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
762-
}
763-
764765
fn resolve_lang_item(&self, name: Name) -> Option<LangItemTarget> {
765766
let krate = self.resolver.krate();
766767
self.db.lang_item(krate, name.to_smol_str())

0 commit comments

Comments
 (0)