From 0e8c4503bf4754f4437d8bd45a110b9d0ec671e0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 1 Apr 2021 21:45:44 +0200 Subject: Don't recheck obligations if we have learned nothing new This is just the most trivial check: If no inference variables have been updated, and there are no new obligations, we can just skip trying to solve them again. We could be smarter about it, but this already helps quite a bit, and I don't want to touch this too much before we replace the inference table by Chalk's. Fixes #8263 (well, improves it quite a bit). --- crates/hir_ty/src/infer/expr.rs | 6 +++--- crates/hir_ty/src/infer/path.rs | 2 +- crates/hir_ty/src/infer/unify.rs | 8 +++++++- 3 files changed, 11 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 6279aa572..25ab3ea4c 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -99,7 +99,7 @@ impl<'a> InferenceContext<'a> { environment: trait_env, }); if self.db.trait_solve(krate, goal.value).is_some() { - self.obligations.push(implements_fn_trait); + self.push_obligation(implements_fn_trait); let output_proj_ty = crate::ProjectionTy { associated_ty_id: to_assoc_type_id(output_assoc_type), substitution: substs, @@ -964,7 +964,7 @@ impl<'a> InferenceContext<'a> { let (predicate, binders) = predicate.clone().subst(parameters).into_value_and_skipped_binders(); always!(binders == 0); // quantified where clauses not yet handled - self.obligations.push(predicate.cast(&Interner)); + self.push_obligation(predicate.cast(&Interner)); } // add obligation for trait implementation, if this is a trait method match def { @@ -974,7 +974,7 @@ impl<'a> InferenceContext<'a> { // construct a TraitRef let substs = parameters.prefix(generics(self.db.upcast(), trait_.into()).len()); - self.obligations.push( + self.push_obligation( TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: substs } .cast(&Interner), ); diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index cefa38509..717738789 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -258,7 +258,7 @@ impl<'a> InferenceContext<'a> { .push(ty.clone()) .fill(std::iter::repeat_with(|| self.table.new_type_var())) .build(); - self.obligations.push( + self.push_obligation( TraitRef { trait_id: to_chalk_trait_id(trait_), substitution: trait_substs.clone(), diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 6e7b0f5a6..5ea4b7481 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -231,6 +231,7 @@ pub(crate) struct TypeVariableData { pub(crate) struct InferenceTable { pub(super) var_unification_table: InPlaceUnificationTable, pub(super) type_variable_table: TypeVariableTable, + pub(super) revision: u32, } impl InferenceTable { @@ -238,6 +239,7 @@ impl InferenceTable { InferenceTable { var_unification_table: InPlaceUnificationTable::new(), type_variable_table: TypeVariableTable { inner: Vec::new() }, + revision: 0, } } @@ -360,7 +362,10 @@ impl InferenceTable { == self.type_variable_table.is_diverging(*tv2) => { // both type vars are unknown since we tried to resolve them - self.var_unification_table.union(tv1.to_inner(), tv2.to_inner()); + if !self.var_unification_table.unioned(tv1.to_inner(), tv2.to_inner()) { + self.var_unification_table.union(tv1.to_inner(), tv2.to_inner()); + self.revision += 1; + } true } @@ -398,6 +403,7 @@ impl InferenceTable { tv.to_inner(), TypeVarValue::Known(other.clone().intern(&Interner)), ); + self.revision += 1; true } -- cgit v1.2.3