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/builder.rs | 5 +++-- crates/hir_ty/src/display.rs | 12 ++++++------ crates/hir_ty/src/infer.rs | 8 ++++---- crates/hir_ty/src/infer/expr.rs | 14 ++++++++------ crates/hir_ty/src/infer/pat.rs | 9 ++++++--- crates/hir_ty/src/infer/path.rs | 7 ++++--- crates/hir_ty/src/lib.rs | 10 +++++----- crates/hir_ty/src/lower.rs | 8 ++++---- crates/hir_ty/src/method_resolution.rs | 4 ++-- crates/hir_ty/src/traits/chalk/mapping.rs | 2 +- crates/hir_ty/src/utils.rs | 2 +- 11 files changed, 44 insertions(+), 37 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/builder.rs b/crates/hir_ty/src/builder.rs index c5196200c..9b2c6975a 100644 --- a/crates/hir_ty/src/builder.rs +++ b/crates/hir_ty/src/builder.rs @@ -139,7 +139,8 @@ impl TyBuilder { } else { // each default can depend on the previous parameters let subst_so_far = Substitution::intern(self.vec.clone()); - self.vec.push(default_ty.clone().substitute(&subst_so_far).cast(&Interner)); + self.vec + .push(default_ty.clone().substitute(&Interner, &subst_so_far).cast(&Interner)); } } self @@ -200,7 +201,7 @@ impl> TyBuilder> { pub fn build(self) -> T { let (b, subst) = self.build_internal(); - b.substitute(&subst) + b.substitute(&Interner, &subst) } } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 704504b02..f31e6b108 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -352,7 +352,7 @@ impl HirDisplay for Ty { let data = (*datas) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - let bounds = data.substitute(parameters); + let bounds = data.substitute(&Interner, parameters); bounds.into_value_and_skipped_binders().0 } else { Vec::new() @@ -397,7 +397,7 @@ impl HirDisplay for Ty { } TyKind::FnDef(def, parameters) => { let def = from_chalk(f.db, *def); - let sig = f.db.callable_item_signature(def).substitute(parameters); + let sig = f.db.callable_item_signature(def).substitute(&Interner, parameters); match def { CallableDefId::FunctionId(ff) => { write!(f, "fn {}", f.db.function_data(ff).name)? @@ -482,7 +482,7 @@ impl HirDisplay for Ty { (_, Some(default_parameter)) => { let actual_default = default_parameter .clone() - .substitute(¶meters.prefix(i)); + .substitute(&Interner, ¶meters.prefix(i)); if parameter.assert_ty_ref(&Interner) != &actual_default { default_from = i + 1; @@ -542,7 +542,7 @@ impl HirDisplay for Ty { let data = (*datas) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - let bounds = data.substitute(¶meters); + let bounds = data.substitute(&Interner, ¶meters); write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?; // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution } @@ -595,7 +595,7 @@ impl HirDisplay for Ty { let bounds = f.db.generic_predicates(id.parent) .into_iter() - .map(|pred| pred.clone().substitute(&substs)) + .map(|pred| pred.clone().substitute(&Interner, &substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { tr.self_type_parameter(&Interner) == self @@ -629,7 +629,7 @@ impl HirDisplay for Ty { let data = (*datas) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - let bounds = data.substitute(&opaque_ty.substitution); + let bounds = data.substitute(&Interner, &opaque_ty.substitution); write_bounds_like_dyn_trait_with_prefix("impl", bounds.skip_binders(), f)?; } ImplTraitId::AsyncBlockTypeImplTrait(..) => { diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index a87037344..1c3faf5cb 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -470,25 +470,25 @@ impl<'a> InferenceContext<'a> { TypeNs::AdtId(AdtId::StructId(strukt)) => { let substs = ctx.substs_from_path(path, strukt.into(), true); let ty = self.db.ty(strukt.into()); - let ty = self.insert_type_vars(ty.substitute(&substs)); + let ty = self.insert_type_vars(ty.substitute(&Interner, &substs)); forbid_unresolved_segments((ty, Some(strukt.into())), unresolved) } TypeNs::AdtId(AdtId::UnionId(u)) => { let substs = ctx.substs_from_path(path, u.into(), true); let ty = self.db.ty(u.into()); - let ty = self.insert_type_vars(ty.substitute(&substs)); + let ty = self.insert_type_vars(ty.substitute(&Interner, &substs)); forbid_unresolved_segments((ty, Some(u.into())), unresolved) } TypeNs::EnumVariantId(var) => { let substs = ctx.substs_from_path(path, var.into(), true); let ty = self.db.ty(var.parent.into()); - let ty = self.insert_type_vars(ty.substitute(&substs)); + let ty = self.insert_type_vars(ty.substitute(&Interner, &substs)); forbid_unresolved_segments((ty, Some(var.into())), unresolved) } TypeNs::SelfType(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); match unresolved { None => { let variant = ty_variant(&ty); 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) } diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index e15840c9a..827317fce 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -130,9 +130,9 @@ impl Binders { impl Binders { /// Substitutes all variables. - pub fn substitute(self, subst: &Substitution) -> T { + pub fn substitute(self, interner: &Interner, subst: &Substitution) -> T { let (value, binders) = self.into_value_and_skipped_binders(); - assert_eq!(subst.len(&Interner), binders.len(&Interner)); + assert_eq!(subst.len(interner), binders.len(interner)); value.subst_bound_vars(subst) } } @@ -362,7 +362,7 @@ impl Ty { TyKind::FnDef(def, parameters) => { let callable_def = db.lookup_intern_callable_def((*def).into()); let sig = db.callable_item_signature(callable_def); - Some(sig.substitute(¶meters)) + Some(sig.substitute(&Interner, ¶meters)) } TyKind::Closure(.., substs) => { let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner); @@ -436,7 +436,7 @@ impl Ty { let data = (*it) .as_ref() .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); - data.substitute(&opaque_ty.substitution) + data.substitute(&Interner, &opaque_ty.substitution) }) } // It always has an parameter for Future::Output type. @@ -455,7 +455,7 @@ impl Ty { let predicates = db .generic_predicates(id.parent) .into_iter() - .map(|pred| pred.clone().substitute(&substs)) + .map(|pred| pred.clone().substitute(&Interner, &substs)) .filter(|wc| match &wc.skip_binders() { WhereClause::Implemented(tr) => { tr.self_type_parameter(&Interner) == self diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 6a3a880e0..00838b298 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -414,7 +414,7 @@ impl<'a> TyLoweringContext<'a> { TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), }; - self.db.impl_self_ty(impl_id).substitute(&substs) + self.db.impl_self_ty(impl_id).substitute(&Interner, &substs) } TypeNs::AdtSelfType(adt) => { let generics = generics(self.db.upcast(), adt.into()); @@ -422,7 +422,7 @@ impl<'a> TyLoweringContext<'a> { TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db), TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders), }; - self.db.ty(adt.into()).substitute(&substs) + self.db.ty(adt.into()).substitute(&Interner, &substs) } TypeNs::AdtId(it) => self.lower_path_inner(resolved_segment, it.into(), infer_args), @@ -516,7 +516,7 @@ impl<'a> TyLoweringContext<'a> { TyDefId::TypeAliasId(it) => Some(it.into()), }; let substs = self.substs_from_path_segment(segment, generic_def, infer_args, None); - self.db.ty(typeable).substitute(&substs) + self.db.ty(typeable).substitute(&Interner, &substs) } /// Collect generic arguments from a path into a `Substs`. See also @@ -620,7 +620,7 @@ impl<'a> TyLoweringContext<'a> { for default_ty in defaults.iter().skip(substs.len()) { // each default can depend on the previous parameters let substs_so_far = Substitution::from_iter(&Interner, substs.clone()); - substs.push(default_ty.clone().substitute(&substs_so_far)); + substs.push(default_ty.clone().substitute(&Interner, &substs_so_far)); } } } diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index 88750acf3..19a1fa793 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -712,7 +712,7 @@ pub(crate) fn inherent_impl_substs( let vars = TyBuilder::subst_for_def(db, impl_id) .fill_with_bound_vars(DebruijnIndex::INNERMOST, self_ty.binders.len(&Interner)) .build(); - let self_ty_with_vars = db.impl_self_ty(impl_id).substitute(&vars); + let self_ty_with_vars = db.impl_self_ty(impl_id).substitute(&Interner, &vars); let mut kinds = self_ty.binders.interned().to_vec(); kinds.extend( iter::repeat(chalk_ir::WithKind::new( @@ -774,7 +774,7 @@ fn transform_receiver_ty( AssocContainerId::ModuleId(_) => unreachable!(), }; let sig = db.callable_item_signature(function_id.into()); - Some(sig.map(|s| s.params()[0].clone()).substitute(&substs)) + Some(sig.map(|s| s.params()[0].clone()).substitute(&Interner, &substs)) } pub fn implements_trait( diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index 481b0bb10..9db2b0c2f 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -519,7 +519,7 @@ pub(super) fn convert_where_clauses( let generic_predicates = db.generic_predicates(def); let mut result = Vec::with_capacity(generic_predicates.len()); for pred in generic_predicates.iter() { - result.push(pred.clone().substitute(substs).to_chalk(db)); + result.push(pred.clone().substitute(&Interner, substs).to_chalk(db)); } result } diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs index a63075a19..c7786a199 100644 --- a/crates/hir_ty/src/utils.rs +++ b/crates/hir_ty/src/utils.rs @@ -72,7 +72,7 @@ fn direct_super_trait_refs(db: &dyn HirDatabase, trait_ref: &TraitRef) -> Vec None, }) }) - .map(|pred| pred.substitute(&trait_ref.substitution)) + .map(|pred| pred.substitute(&Interner, &trait_ref.substitution)) .collect() } -- cgit v1.2.3