From ad20f00844cec9c794e34869be163673ebbed182 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 17:45:18 +0200 Subject: Use VariableKinds in Binders --- crates/hir_ty/src/infer/expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 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 ccaae53e9..d61ef65f2 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -849,7 +849,7 @@ impl<'a> InferenceContext<'a> { self.write_method_resolution(tgt_expr, func); (ty, self.db.value_ty(func.into()), Some(generics(self.db.upcast(), func.into()))) } - None => (receiver_ty, Binders::new(0, self.err_ty()), None), + None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None), }; let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty); let method_ty = method_ty.subst(&substs); @@ -951,7 +951,7 @@ impl<'a> InferenceContext<'a> { for predicate in generic_predicates.iter() { let (predicate, binders) = predicate.clone().subst(parameters).into_value_and_skipped_binders(); - always!(binders == 0); // quantified where clauses not yet handled + always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled self.push_obligation(predicate.cast(&Interner)); } // add obligation for trait implementation, if this is a trait method -- cgit v1.2.3 From 05eba0db3dd76f016aabdd49af6211e70a1812ed Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 18:39:53 +0200 Subject: Binders::subst -> substitute --- crates/hir_ty/src/infer/expr.rs | 10 +++++----- crates/hir_ty/src/infer/pat.rs | 4 ++-- crates/hir_ty/src/infer/path.rs | 6 +++--- 3 files changed, 10 insertions(+), 10 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 d61ef65f2..5f8ad2174 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -419,7 +419,7 @@ impl<'a> InferenceContext<'a> { self.result.record_field_resolutions.insert(field.expr, field_def); } let field_ty = field_def.map_or(self.err_ty(), |it| { - field_types[it.local_id].clone().subst(&substs) + field_types[it.local_id].clone().substitute(&substs) }); self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); } @@ -462,7 +462,7 @@ impl<'a> InferenceContext<'a> { Some( self.db.field_types((*s).into())[field.local_id] .clone() - .subst(¶meters), + .substitute(¶meters), ) } else { None @@ -476,7 +476,7 @@ impl<'a> InferenceContext<'a> { Some( self.db.field_types((*u).into())[field.local_id] .clone() - .subst(¶meters), + .substitute(¶meters), ) } else { None @@ -852,7 +852,7 @@ impl<'a> InferenceContext<'a> { None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None), }; let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty); - let method_ty = method_ty.subst(&substs); + let method_ty = method_ty.substitute(&substs); let method_ty = self.insert_type_vars(method_ty); self.register_obligations_for_call(&method_ty); let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) { @@ -950,7 +950,7 @@ impl<'a> InferenceContext<'a> { let generic_predicates = self.db.generic_predicates(def.into()); for predicate in generic_predicates.iter() { let (predicate, binders) = - predicate.clone().subst(parameters).into_value_and_skipped_binders(); + predicate.clone().substitute(parameters).into_value_and_skipped_binders(); always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled self.push_obligation(predicate.cast(&Interner)); } diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 469f37dd9..b74f1f4f8 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -49,7 +49,7 @@ impl<'a> InferenceContext<'a> { let expected_ty = var_data .as_ref() .and_then(|d| d.field(&Name::new_tuple_field(i))) - .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs)); + .map_or(self.err_ty(), |field| field_tys[field].clone().substitute(&substs)); let expected_ty = self.normalize_associated_types_in(expected_ty); self.infer_pat(subpat, &expected_ty, default_bm); } @@ -84,7 +84,7 @@ impl<'a> InferenceContext<'a> { } let expected_ty = matching_field - .map_or(self.err_ty(), |field| field_tys[field].clone().subst(&substs)); + .map_or(self.err_ty(), |field| field_tys[field].clone().substitute(&substs)); let expected_ty = self.normalize_associated_types_in(expected_ty); self.infer_pat(subpat.pat, &expected_ty, default_bm); } diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index 89d78e781..5e3a45766 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -81,9 +81,9 @@ impl<'a> InferenceContext<'a> { ValueNs::ImplSelf(impl_id) => { let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); let substs = generics.type_params_subst(self.db); - let ty = self.db.impl_self_ty(impl_id).subst(&substs); + let ty = self.db.impl_self_ty(impl_id).substitute(&substs); if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { - let ty = self.db.value_ty(struct_id.into()).subst(&substs); + let ty = self.db.value_ty(struct_id.into()).substitute(&substs); return Some(ty); } else { // FIXME: diagnostic, invalid Self reference @@ -243,7 +243,7 @@ impl<'a> InferenceContext<'a> { let impl_substs = TyBuilder::subst_for_def(self.db, impl_id) .fill(iter::repeat_with(|| self.table.new_type_var())) .build(); - let impl_self_ty = self.db.impl_self_ty(impl_id).subst(&impl_substs); + let impl_self_ty = self.db.impl_self_ty(impl_id).substitute(&impl_substs); self.unify(&impl_self_ty, &ty); Some(impl_substs) } -- cgit v1.2.3 From 30a339e038bfd94d8c91f79287be9b7db4f0cb4e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 18:49:26 +0200 Subject: Add Interner parameter to Binders::substitute --- crates/hir_ty/src/infer/expr.rs | 14 ++++++++------ crates/hir_ty/src/infer/pat.rs | 9 ++++++--- crates/hir_ty/src/infer/path.rs | 7 ++++--- 3 files changed, 18 insertions(+), 12 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 5f8ad2174..6966d26e7 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -419,7 +419,7 @@ impl<'a> InferenceContext<'a> { self.result.record_field_resolutions.insert(field.expr, field_def); } let field_ty = field_def.map_or(self.err_ty(), |it| { - field_types[it.local_id].clone().substitute(&substs) + field_types[it.local_id].clone().substitute(&Interner, &substs) }); self.infer_expr_coerce(field.expr, &Expectation::has_type(field_ty)); } @@ -462,7 +462,7 @@ impl<'a> InferenceContext<'a> { Some( self.db.field_types((*s).into())[field.local_id] .clone() - .substitute(¶meters), + .substitute(&Interner, ¶meters), ) } else { None @@ -476,7 +476,7 @@ impl<'a> InferenceContext<'a> { Some( self.db.field_types((*u).into())[field.local_id] .clone() - .substitute(¶meters), + .substitute(&Interner, ¶meters), ) } else { None @@ -852,7 +852,7 @@ impl<'a> InferenceContext<'a> { None => (receiver_ty, Binders::empty(&Interner, self.err_ty()), None), }; let substs = self.substs_for_method_call(def_generics, generic_args, &derefed_receiver_ty); - let method_ty = method_ty.substitute(&substs); + let method_ty = method_ty.substitute(&Interner, &substs); let method_ty = self.insert_type_vars(method_ty); self.register_obligations_for_call(&method_ty); let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) { @@ -949,8 +949,10 @@ impl<'a> InferenceContext<'a> { let def: CallableDefId = from_chalk(self.db, *fn_def); let generic_predicates = self.db.generic_predicates(def.into()); for predicate in generic_predicates.iter() { - let (predicate, binders) = - predicate.clone().substitute(parameters).into_value_and_skipped_binders(); + let (predicate, binders) = predicate + .clone() + .substitute(&Interner, parameters) + .into_value_and_skipped_binders(); always!(binders.len(&Interner) == 0); // quantified where clauses not yet handled self.push_obligation(predicate.cast(&Interner)); } diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index b74f1f4f8..252ae914a 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -49,7 +49,9 @@ impl<'a> InferenceContext<'a> { let expected_ty = var_data .as_ref() .and_then(|d| d.field(&Name::new_tuple_field(i))) - .map_or(self.err_ty(), |field| field_tys[field].clone().substitute(&substs)); + .map_or(self.err_ty(), |field| { + field_tys[field].clone().substitute(&Interner, &substs) + }); let expected_ty = self.normalize_associated_types_in(expected_ty); self.infer_pat(subpat, &expected_ty, default_bm); } @@ -83,8 +85,9 @@ impl<'a> InferenceContext<'a> { self.result.record_pat_field_resolutions.insert(subpat.pat, field_def); } - let expected_ty = matching_field - .map_or(self.err_ty(), |field| field_tys[field].clone().substitute(&substs)); + let expected_ty = matching_field.map_or(self.err_ty(), |field| { + field_tys[field].clone().substitute(&Interner, &substs) + }); let expected_ty = self.normalize_associated_types_in(expected_ty); self.infer_pat(subpat.pat, &expected_ty, default_bm); } diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index 5e3a45766..14f705173 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -81,9 +81,9 @@ impl<'a> InferenceContext<'a> { ValueNs::ImplSelf(impl_id) => { let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); let substs = generics.type_params_subst(self.db); - let ty = self.db.impl_self_ty(impl_id).substitute(&substs); + let ty = self.db.impl_self_ty(impl_id).substitute(&Interner, &substs); if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { - let ty = self.db.value_ty(struct_id.into()).substitute(&substs); + let ty = self.db.value_ty(struct_id.into()).substitute(&Interner, &substs); return Some(ty); } else { // FIXME: diagnostic, invalid Self reference @@ -243,7 +243,8 @@ impl<'a> InferenceContext<'a> { let impl_substs = TyBuilder::subst_for_def(self.db, impl_id) .fill(iter::repeat_with(|| self.table.new_type_var())) .build(); - let impl_self_ty = self.db.impl_self_ty(impl_id).substitute(&impl_substs); + let impl_self_ty = + self.db.impl_self_ty(impl_id).substitute(&Interner, &impl_substs); self.unify(&impl_self_ty, &ty); Some(impl_substs) } -- cgit v1.2.3 From e28f0c98ba449c372ffcc450ac756f0adbdf9549 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 18:54:31 +0200 Subject: Get rid of some walk_mut uses --- crates/hir_ty/src/infer/unify.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 8370f2e1c..06187df56 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -108,19 +108,22 @@ impl<'a, 'b> Canonicalizer<'a, 'b> { } impl Canonicalized { - pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { - ty.walk_mut_binders( + pub(super) fn decanonicalize_ty(&self, ty: Ty) -> Ty { + ty.fold_binders( &mut |ty, binders| { - if let &mut TyKind::BoundVar(bound) = ty.interned_mut() { + if let TyKind::BoundVar(bound) = ty.kind(&Interner) { if bound.debruijn >= binders { let (v, k) = self.free_vars[bound.index]; - *ty = TyKind::InferenceVar(v, k).intern(&Interner); + TyKind::InferenceVar(v, k).intern(&Interner) + } else { + ty } + } else { + ty } }, DebruijnIndex::INNERMOST, - ); - ty + ) } pub(super) fn apply_solution( -- cgit v1.2.3 From fbab69cbffed37291b463c0e57fade49581e1de2 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 19:01:41 +0200 Subject: Get rid of subst_bound_vars uses --- crates/hir_ty/src/infer/unify.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 06187df56..c90a16720 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -152,7 +152,7 @@ impl Canonicalized { // eagerly replace projections in the type; we may be getting types // e.g. from where clauses where this hasn't happened yet let ty = ctx.normalize_associated_types_in( - ty.assert_ty_ref(&Interner).clone().subst_bound_vars(&new_vars), + new_vars.apply(ty.assert_ty_ref(&Interner).clone(), &Interner), ); ctx.table.unify(&TyKind::InferenceVar(v, k).intern(&Interner), &ty); } @@ -173,8 +173,8 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option { // fallback to Unknown in the end (kind of hacky, as below) .map(|_| table.new_type_var()), ); - let ty1_with_vars = tys.value.0.clone().subst_bound_vars(&vars); - let ty2_with_vars = tys.value.1.clone().subst_bound_vars(&vars); + let ty1_with_vars = vars.apply(tys.value.0.clone(), &Interner); + let ty2_with_vars = vars.apply(tys.value.1.clone(), &Interner); if !table.unify(&ty1_with_vars, &ty2_with_vars) { return None; } -- cgit v1.2.3