aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-04-04 12:07:06 +0100
committerFlorian Diebold <[email protected]>2021-04-04 12:16:39 +0100
commita4d7bdf1c884a9f3dd415a882fa56422adae89bf (patch)
tree8b5d2db036649ddc15c587a03e1684b3314be50b /crates
parent715c178f0b52117c4c689c39a0921012bfbb2386 (diff)
Replace Substitution::bound_vars and ::type_params_for_generics
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_ty/src/display.rs4
-rw-r--r--crates/hir_ty/src/infer.rs4
-rw-r--r--crates/hir_ty/src/infer/path.rs2
-rw-r--r--crates/hir_ty/src/lib.rs28
-rw-r--r--crates/hir_ty/src/lower.rs24
-rw-r--r--crates/hir_ty/src/traits/chalk.rs10
-rw-r--r--crates/hir_ty/src/utils.rs24
7 files changed, 42 insertions, 54 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index eb82e1a11..39e0b328d 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -19,7 +19,7 @@ use crate::{
19 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, 19 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
20 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, 20 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy,
21 CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy, 21 CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy,
22 ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyExt, TyKind, 22 ProjectionTy, QuantifiedWhereClause, Scalar, TraitRef, Ty, TyExt, TyKind,
23 WhereClause, 23 WhereClause,
24}; 24};
25 25
@@ -592,7 +592,7 @@ impl HirDisplay for Ty {
592 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? 592 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
593 } 593 }
594 TypeParamProvenance::ArgumentImplTrait => { 594 TypeParamProvenance::ArgumentImplTrait => {
595 let substs = Substitution::type_params_for_generics(f.db, &generics); 595 let substs = generics.type_params_subst(f.db);
596 let bounds = f 596 let bounds = f
597 .db 597 .db
598 .generic_predicates(id.parent) 598 .generic_predicates(id.parent)
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index caafeccb8..1b1d4458c 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -38,7 +38,7 @@ use syntax::SmolStr;
38 38
39use super::{ 39use super::{
40 traits::{DomainGoal, Guidance, Solution}, 40 traits::{DomainGoal, Guidance, Solution},
41 InEnvironment, ProjectionTy, Substitution, TraitEnvironment, TraitRef, Ty, TypeWalk, 41 InEnvironment, ProjectionTy, TraitEnvironment, TraitRef, Ty, TypeWalk,
42}; 42};
43use crate::{ 43use crate::{
44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, 44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
@@ -487,7 +487,7 @@ impl<'a> InferenceContext<'a> {
487 } 487 }
488 TypeNs::SelfType(impl_id) => { 488 TypeNs::SelfType(impl_id) => {
489 let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); 489 let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
490 let substs = Substitution::type_params_for_generics(self.db, &generics); 490 let substs = generics.type_params_subst(self.db);
491 let ty = self.db.impl_self_ty(impl_id).subst(&substs); 491 let ty = self.db.impl_self_ty(impl_id).subst(&substs);
492 match unresolved { 492 match unresolved {
493 None => { 493 None => {
diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs
index d55ae4900..671ea355f 100644
--- a/crates/hir_ty/src/infer/path.rs
+++ b/crates/hir_ty/src/infer/path.rs
@@ -80,7 +80,7 @@ impl<'a> InferenceContext<'a> {
80 } 80 }
81 ValueNs::ImplSelf(impl_id) => { 81 ValueNs::ImplSelf(impl_id) => {
82 let generics = crate::utils::generics(self.db.upcast(), impl_id.into()); 82 let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
83 let substs = Substitution::type_params_for_generics(self.db, &generics); 83 let substs = generics.type_params_subst(self.db);
84 let ty = self.db.impl_self_ty(impl_id).subst(&substs); 84 let ty = self.db.impl_self_ty(impl_id).subst(&substs);
85 if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() { 85 if let Some((AdtId::StructId(struct_id), substs)) = ty.as_adt() {
86 let ty = self.db.value_ty(struct_id.into()).subst(&substs); 86 let ty = self.db.value_ty(struct_id.into()).subst(&substs);
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index ebdcc4804..6d6443ca3 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -41,7 +41,7 @@ use hir_def::{
41use crate::{ 41use crate::{
42 db::HirDatabase, 42 db::HirDatabase,
43 display::HirDisplay, 43 display::HirDisplay,
44 utils::{generics, make_mut_slice, Generics}, 44 utils::{generics, make_mut_slice},
45}; 45};
46 46
47pub use autoderef::autoderef; 47pub use autoderef::autoderef;
@@ -464,33 +464,9 @@ impl Substitution {
464 } 464 }
465 465
466 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). 466 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
467 pub(crate) fn type_params_for_generics(
468 db: &dyn HirDatabase,
469 generic_params: &Generics,
470 ) -> Substitution {
471 Substitution::from_iter(
472 &Interner,
473 generic_params
474 .iter()
475 .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)),
476 )
477 }
478
479 /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`).
480 pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution { 467 pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
481 let params = generics(db.upcast(), def.into()); 468 let params = generics(db.upcast(), def.into());
482 Substitution::type_params_for_generics(db, &params) 469 params.type_params_subst(db)
483 }
484
485 /// Return Substs that replace each parameter by a bound variable.
486 pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substitution {
487 Substitution::from_iter(
488 &Interner,
489 generic_params
490 .iter()
491 .enumerate()
492 .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
493 )
494 } 470 }
495} 471}
496 472
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index 644a2eed5..f9a721fdf 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -234,7 +234,7 @@ impl<'a> TyLoweringContext<'a> {
234 let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx); 234 let impl_trait_id = ImplTraitId::ReturnTypeImplTrait(func, idx);
235 let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into(); 235 let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
236 let generics = generics(self.db.upcast(), func.into()); 236 let generics = generics(self.db.upcast(), func.into());
237 let parameters = Substitution::bound_vars(&generics, self.in_binders); 237 let parameters = generics.bound_vars_subst(self.in_binders);
238 TyKind::Alias(AliasTy::Opaque(OpaqueTy { 238 TyKind::Alias(AliasTy::Opaque(OpaqueTy {
239 opaque_ty_id, 239 opaque_ty_id,
240 substitution: parameters, 240 substitution: parameters,
@@ -411,24 +411,16 @@ impl<'a> TyLoweringContext<'a> {
411 TypeNs::SelfType(impl_id) => { 411 TypeNs::SelfType(impl_id) => {
412 let generics = generics(self.db.upcast(), impl_id.into()); 412 let generics = generics(self.db.upcast(), impl_id.into());
413 let substs = match self.type_param_mode { 413 let substs = match self.type_param_mode {
414 TypeParamLoweringMode::Placeholder => { 414 TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db),
415 Substitution::type_params_for_generics(self.db, &generics) 415 TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders),
416 }
417 TypeParamLoweringMode::Variable => {
418 Substitution::bound_vars(&generics, self.in_binders)
419 }
420 }; 416 };
421 self.db.impl_self_ty(impl_id).subst(&substs) 417 self.db.impl_self_ty(impl_id).subst(&substs)
422 } 418 }
423 TypeNs::AdtSelfType(adt) => { 419 TypeNs::AdtSelfType(adt) => {
424 let generics = generics(self.db.upcast(), adt.into()); 420 let generics = generics(self.db.upcast(), adt.into());
425 let substs = match self.type_param_mode { 421 let substs = match self.type_param_mode {
426 TypeParamLoweringMode::Placeholder => { 422 TypeParamLoweringMode::Placeholder => generics.type_params_subst(self.db),
427 Substitution::type_params_for_generics(self.db, &generics) 423 TypeParamLoweringMode::Variable => generics.bound_vars_subst(self.in_binders),
428 }
429 TypeParamLoweringMode::Variable => {
430 Substitution::bound_vars(&generics, self.in_binders)
431 }
432 }; 424 };
433 self.db.ty(adt.into()).subst(&substs) 425 self.db.ty(adt.into()).subst(&substs)
434 } 426 }
@@ -1060,7 +1052,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
1060/// function body. 1052/// function body.
1061fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { 1053fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
1062 let generics = generics(db.upcast(), def.into()); 1054 let generics = generics(db.upcast(), def.into());
1063 let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); 1055 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1064 Binders::new( 1056 Binders::new(
1065 substs.len(&Interner), 1057 substs.len(&Interner),
1066 TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), 1058 TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
@@ -1105,7 +1097,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
1105 return type_for_adt(db, def.into()); 1097 return type_for_adt(db, def.into());
1106 } 1098 }
1107 let generics = generics(db.upcast(), def.into()); 1099 let generics = generics(db.upcast(), def.into());
1108 let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); 1100 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1109 Binders::new( 1101 Binders::new(
1110 substs.len(&Interner), 1102 substs.len(&Interner),
1111 TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), 1103 TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
@@ -1132,7 +1124,7 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
1132 return type_for_adt(db, def.parent.into()); 1124 return type_for_adt(db, def.parent.into());
1133 } 1125 }
1134 let generics = generics(db.upcast(), def.parent.into()); 1126 let generics = generics(db.upcast(), def.parent.into());
1135 let substs = Substitution::bound_vars(&generics, DebruijnIndex::INNERMOST); 1127 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1136 Binders::new( 1128 Binders::new(
1137 substs.len(&Interner), 1129 substs.len(&Interner),
1138 TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), 1130 TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs
index 36a397990..541e6082f 100644
--- a/crates/hir_ty/src/traits/chalk.rs
+++ b/crates/hir_ty/src/traits/chalk.rs
@@ -387,7 +387,7 @@ pub(crate) fn associated_ty_data_query(
387 // Lower bounds -- we could/should maybe move this to a separate query in `lower` 387 // Lower bounds -- we could/should maybe move this to a separate query in `lower`
388 let type_alias_data = db.type_alias_data(type_alias); 388 let type_alias_data = db.type_alias_data(type_alias);
389 let generic_params = generics(db.upcast(), type_alias.into()); 389 let generic_params = generics(db.upcast(), type_alias.into());
390 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 390 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
391 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast()); 391 let resolver = hir_def::resolver::HasResolver::resolver(type_alias, db.upcast());
392 let ctx = crate::TyLoweringContext::new(db, &resolver) 392 let ctx = crate::TyLoweringContext::new(db, &resolver)
393 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable); 393 .with_type_param_mode(crate::lower::TypeParamLoweringMode::Variable);
@@ -421,7 +421,7 @@ pub(crate) fn trait_datum_query(
421 let trait_data = db.trait_data(trait_); 421 let trait_data = db.trait_data(trait_);
422 debug!("trait {:?} = {:?}", trait_id, trait_data.name); 422 debug!("trait {:?} = {:?}", trait_id, trait_data.name);
423 let generic_params = generics(db.upcast(), trait_.into()); 423 let generic_params = generics(db.upcast(), trait_.into());
424 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 424 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
425 let flags = rust_ir::TraitFlags { 425 let flags = rust_ir::TraitFlags {
426 auto: trait_data.is_auto, 426 auto: trait_data.is_auto,
427 upstream: trait_.lookup(db.upcast()).container.krate() != krate, 427 upstream: trait_.lookup(db.upcast()).container.krate() != krate,
@@ -490,7 +490,7 @@ pub(crate) fn struct_datum_query(
490 let upstream = adt_id.module(db.upcast()).krate() != krate; 490 let upstream = adt_id.module(db.upcast()).krate() != krate;
491 let where_clauses = { 491 let where_clauses = {
492 let generic_params = generics(db.upcast(), adt_id.into()); 492 let generic_params = generics(db.upcast(), adt_id.into());
493 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 493 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
494 convert_where_clauses(db, adt_id.into(), &bound_vars) 494 convert_where_clauses(db, adt_id.into(), &bound_vars)
495 }; 495 };
496 let flags = rust_ir::AdtFlags { 496 let flags = rust_ir::AdtFlags {
@@ -539,7 +539,7 @@ fn impl_def_datum(
539 let impl_data = db.impl_data(impl_id); 539 let impl_data = db.impl_data(impl_id);
540 540
541 let generic_params = generics(db.upcast(), impl_id.into()); 541 let generic_params = generics(db.upcast(), impl_id.into());
542 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 542 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
543 let trait_ = trait_ref.hir_trait_id(); 543 let trait_ = trait_ref.hir_trait_id();
544 let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate { 544 let impl_type = if impl_id.lookup(db.upcast()).container.krate() == krate {
545 rust_ir::ImplType::Local 545 rust_ir::ImplType::Local
@@ -629,7 +629,7 @@ pub(crate) fn fn_def_datum_query(
629 let callable_def: CallableDefId = from_chalk(db, fn_def_id); 629 let callable_def: CallableDefId = from_chalk(db, fn_def_id);
630 let generic_params = generics(db.upcast(), callable_def.into()); 630 let generic_params = generics(db.upcast(), callable_def.into());
631 let sig = db.callable_item_signature(callable_def); 631 let sig = db.callable_item_signature(callable_def);
632 let bound_vars = Substitution::bound_vars(&generic_params, DebruijnIndex::INNERMOST); 632 let bound_vars = generic_params.bound_vars_subst(DebruijnIndex::INNERMOST);
633 let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars); 633 let where_clauses = convert_where_clauses(db, callable_def.into(), &bound_vars);
634 let bound = rust_ir::FnDefDatumBound { 634 let bound = rust_ir::FnDefDatumBound {
635 // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway 635 // Note: Chalk doesn't actually use this information yet as far as I am aware, but we provide it anyway
diff --git a/crates/hir_ty/src/utils.rs b/crates/hir_ty/src/utils.rs
index 42d7af146..b23e91b1b 100644
--- a/crates/hir_ty/src/utils.rs
+++ b/crates/hir_ty/src/utils.rs
@@ -2,7 +2,7 @@
2//! query, but can't be computed directly from `*Data` (ie, which need a `db`). 2//! query, but can't be computed directly from `*Data` (ie, which need a `db`).
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use chalk_ir::DebruijnIndex; 5use chalk_ir::{BoundVar, DebruijnIndex};
6use hir_def::{ 6use hir_def::{
7 adt::VariantData, 7 adt::VariantData,
8 db::DefDatabase, 8 db::DefDatabase,
@@ -16,7 +16,7 @@ use hir_def::{
16}; 16};
17use hir_expand::name::{name, Name}; 17use hir_expand::name::{name, Name};
18 18
19use crate::{db::HirDatabase, TraitRef, TypeWalk, WhereClause}; 19use crate::{db::HirDatabase, Interner, Substitution, TraitRef, TyKind, TypeWalk, WhereClause};
20 20
21fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> { 21fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> Vec<TraitId> {
22 let resolver = trait_.resolver(db); 22 let resolver = trait_.resolver(db);
@@ -249,6 +249,26 @@ impl Generics {
249 self.parent_generics.as_ref().and_then(|g| g.find_param(param)) 249 self.parent_generics.as_ref().and_then(|g| g.find_param(param))
250 } 250 }
251 } 251 }
252
253 /// Returns a Substitution that replaces each parameter by a bound variable.
254 pub(crate) fn bound_vars_subst(&self, debruijn: DebruijnIndex) -> Substitution {
255 Substitution::from_iter(
256 &Interner,
257 self.iter()
258 .enumerate()
259 .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)),
260 )
261 }
262
263 /// Returns a Substitution that replaces each parameter by itself (i.e. `Ty::Param`).
264 pub(crate) fn type_params_subst(&self, db: &dyn HirDatabase) -> Substitution {
265 Substitution::from_iter(
266 &Interner,
267 self.iter().map(|(id, _)| {
268 TyKind::Placeholder(crate::to_placeholder_idx(db, id)).intern(&Interner)
269 }),
270 )
271 }
252} 272}
253 273
254fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<GenericDefId> { 274fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<GenericDefId> {