From 19664e276aba21a42cad5351a2c91995d1ce5d52 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 13 Mar 2021 17:36:07 +0100 Subject: Use chalk_ir::AssocTypeId --- crates/hir_ty/src/autoderef.rs | 3 +- crates/hir_ty/src/display.rs | 28 +++++++++++------- crates/hir_ty/src/infer.rs | 7 +++-- crates/hir_ty/src/infer/expr.rs | 7 +++-- crates/hir_ty/src/lib.rs | 28 ++++++++++++------ crates/hir_ty/src/lower.rs | 10 +++++-- crates/hir_ty/src/traits/chalk.rs | 17 ++++++----- crates/hir_ty/src/traits/chalk/mapping.rs | 47 +++++++++---------------------- crates/hir_ty/src/traits/chalk/tls.rs | 9 +++--- 9 files changed, 82 insertions(+), 74 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 09009a3d8..d739d5d60 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -12,6 +12,7 @@ use log::{info, warn}; use crate::{ db::HirDatabase, + to_assoc_type_id, traits::{InEnvironment, Solution}, utils::generics, BoundVar, Canonical, DebruijnIndex, Interner, Obligation, Substs, TraitRef, Ty, TyKind, @@ -83,7 +84,7 @@ fn deref_by_trait( let projection = super::traits::ProjectionPredicate { ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.kinds.len())) .intern(&Interner), - projection_ty: super::ProjectionTy { associated_ty: target, parameters }, + projection_ty: super::ProjectionTy { associated_ty: to_assoc_type_id(target), parameters }, }; let obligation = super::Obligation::Projection(projection); diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 9b1e2ad05..2022069d8 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -11,9 +11,9 @@ use hir_def::{ use hir_expand::name::Name; use crate::{ - db::HirDatabase, from_foreign_def_id, primitive, utils::generics, AdtId, AliasTy, - CallableDefId, CallableSig, GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, - OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, + db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, + utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, GenericPredicate, Interner, + Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind, }; pub struct HirFormatter<'a> { @@ -256,7 +256,7 @@ impl HirDisplay for ProjectionTy { f.write_joined(&self.parameters[1..], ", ")?; write!(f, ">")?; } - write!(f, ">::{}", f.db.type_alias_data(self.associated_ty).name)?; + write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty)).name)?; Ok(()) } } @@ -467,13 +467,14 @@ impl HirDisplay for Ty { } } } - TyKind::AssociatedType(type_alias, parameters) => { + TyKind::AssociatedType(assoc_type_id, parameters) => { + let type_alias = from_assoc_type_id(*assoc_type_id); let trait_ = match type_alias.lookup(f.db.upcast()).container { AssocContainerId::TraitId(it) => it, _ => panic!("not an associated type"), }; let trait_ = f.db.trait_data(trait_); - let type_alias_data = f.db.type_alias_data(*type_alias); + let type_alias_data = f.db.type_alias_data(type_alias); // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) if f.display_target.is_test() { @@ -484,8 +485,10 @@ impl HirDisplay for Ty { write!(f, ">")?; } } else { - let projection_ty = - ProjectionTy { associated_ty: *type_alias, parameters: parameters.clone() }; + let projection_ty = ProjectionTy { + associated_ty: to_assoc_type_id(type_alias), + parameters: parameters.clone(), + }; projection_ty.hir_fmt(f)?; } @@ -697,7 +700,9 @@ fn write_bounds_like_dyn_trait( write!(f, "<")?; angle_open = true; } - let type_alias = f.db.type_alias_data(projection_pred.projection_ty.associated_ty); + let type_alias = f.db.type_alias_data(from_assoc_type_id( + projection_pred.projection_ty.associated_ty, + )); write!(f, "{} = ", type_alias.name)?; projection_pred.ty.hir_fmt(f)?; } @@ -768,7 +773,10 @@ impl HirDisplay for GenericPredicate { write!( f, ">::{} = ", - f.db.type_alias_data(projection_pred.projection_ty.associated_ty).name, + f.db.type_alias_data(from_assoc_type_id( + projection_pred.projection_ty.associated_ty + )) + .name, )?; projection_pred.ty.hir_fmt(f)?; } diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index acde99b04..9d9bf549c 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs @@ -42,7 +42,7 @@ use super::{ }; use crate::{ db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, - AliasTy, Interner, TyKind, + to_assoc_type_id, AliasTy, Interner, TyKind, }; pub(crate) use unify::unify; @@ -382,7 +382,10 @@ impl<'a> InferenceContext<'a> { let trait_ref = TraitRef { trait_, substs: substs.clone() }; let projection = ProjectionPredicate { ty: ty.clone(), - projection_ty: ProjectionTy { associated_ty: res_assoc_ty, parameters: substs }, + projection_ty: ProjectionTy { + associated_ty: to_assoc_type_id(res_assoc_ty), + parameters: substs, + }, }; self.obligations.push(Obligation::Trait(trait_ref)); self.obligations.push(Obligation::Projection(projection)); diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 4e77f22fd..a72424dc7 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -18,6 +18,7 @@ use crate::{ lower::lower_to_chalk_mutability, method_resolution, op, primitive::{self, UintTy}, + to_assoc_type_id, traits::{FnTrait, InEnvironment}, utils::{generics, variant_data, Generics}, AdtId, Binders, CallableDefId, FnPointer, FnSig, Interner, Obligation, OpaqueTyId, Rawness, @@ -97,8 +98,10 @@ impl<'a> InferenceContext<'a> { }); if self.db.trait_solve(krate, goal.value).is_some() { self.obligations.push(implements_fn_trait); - let output_proj_ty = - crate::ProjectionTy { associated_ty: output_assoc_type, parameters: substs }; + let output_proj_ty = crate::ProjectionTy { + associated_ty: to_assoc_type_id(output_assoc_type), + parameters: substs, + }; let return_ty = self.normalize_projection_ty(output_proj_ty); Some((arg_tys, return_ty)) } else { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 94cbb21ca..5752ddc4e 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -52,6 +52,7 @@ pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariabl pub use crate::traits::chalk::Interner; pub type ForeignDefId = chalk_ir::ForeignDefId; +pub type AssocTypeId = chalk_ir::AssocTypeId; #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub enum Lifetime { @@ -70,7 +71,7 @@ pub struct OpaqueTy { /// trait and all its parameters are fully known. #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct ProjectionTy { - pub associated_ty: TypeAliasId, + pub associated_ty: AssocTypeId, pub parameters: Substs, } @@ -80,7 +81,7 @@ impl ProjectionTy { } fn trait_(&self, db: &dyn HirDatabase) -> TraitId { - match self.associated_ty.lookup(db.upcast()).container { + match from_assoc_type_id(self.associated_ty).lookup(db.upcast()).container { AssocContainerId::TraitId(it) => it, _ => panic!("projection ty without parent trait"), } @@ -141,7 +142,7 @@ pub enum TyKind { /// when we have tried to normalize a projection like `T::Item` but /// couldn't find a better representation. In that case, we generate /// an **application type** like `(Iterator::Item)`. - AssociatedType(TypeAliasId, Substs), + AssociatedType(AssocTypeId, Substs), /// a scalar type like `bool` or `u32` Scalar(Scalar), @@ -706,7 +707,7 @@ impl Ty { match *self.interned(&Interner) { TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), TyKind::FnDef(callable, ..) => Some(callable.into()), - TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), + TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()), TyKind::ForeignType(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()), _ => None, } @@ -920,14 +921,15 @@ impl Ty { pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option { match self.interned(&Interner) { - TyKind::AssociatedType(type_alias_id, ..) => { - match type_alias_id.lookup(db.upcast()).container { + TyKind::AssociatedType(id, ..) => { + match from_assoc_type_id(*id).lookup(db.upcast()).container { AssocContainerId::TraitId(trait_id) => Some(trait_id), _ => None, } } TyKind::Alias(AliasTy::Projection(projection_ty)) => { - match projection_ty.associated_ty.lookup(db.upcast()).container { + match from_assoc_type_id(projection_ty.associated_ty).lookup(db.upcast()).container + { AssocContainerId::TraitId(trait_id) => Some(trait_id), _ => None, } @@ -1121,10 +1123,18 @@ pub(crate) struct ReturnTypeImplTrait { pub(crate) bounds: Binders>, } -pub(crate) fn to_foreign_def_id(id: TypeAliasId) -> chalk_ir::ForeignDefId { +pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId { chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id)) } -pub(crate) fn from_foreign_def_id(id: chalk_ir::ForeignDefId) -> TypeAliasId { +pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId { + salsa::InternKey::from_intern_id(id.0) +} + +pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId { + chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id)) +} + +pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId { salsa::InternKey::from_intern_id(id.0) } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index a35d7266d..a5ab1ff70 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -27,6 +27,7 @@ use stdx::impl_from; use crate::{ db::HirDatabase, + to_assoc_type_id, traits::chalk::{Interner, ToChalk}, utils::{ all_super_trait_refs, associated_type_by_name_including_super_traits, generics, @@ -358,7 +359,7 @@ impl Ty { Some((super_trait_ref, associated_ty)) => { // FIXME handle type parameters on the segment TyKind::Alias(AliasTy::Projection(ProjectionTy { - associated_ty, + associated_ty: to_assoc_type_id(associated_ty), parameters: super_trait_ref.substs, })) .intern(&Interner) @@ -487,7 +488,7 @@ impl Ty { // FIXME handle type parameters on the segment return Some( TyKind::Alias(AliasTy::Projection(ProjectionTy { - associated_ty, + associated_ty: to_assoc_type_id(associated_ty), parameters: substs, })) .intern(&Interner), @@ -753,7 +754,10 @@ fn assoc_type_bindings_from_type_bound<'a>( None => return SmallVec::<[GenericPredicate; 1]>::new(), Some(t) => t, }; - let projection_ty = ProjectionTy { associated_ty, parameters: super_trait_ref.substs }; + let projection_ty = ProjectionTy { + associated_ty: to_assoc_type_id(associated_ty), + parameters: super_trait_ref.substs, + }; let mut preds = SmallVec::with_capacity( binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(), ); diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 55181cc49..2d3cd6719 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -17,14 +17,15 @@ use super::ChalkContext; use crate::{ db::HirDatabase, display::HirDisplay, + from_assoc_type_id, method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS}, + to_assoc_type_id, utils::generics, BoundVar, CallableDefId, CallableSig, DebruijnIndex, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TyKind, }; use mapping::{ - convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, - TypeAliasAsValue, + convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, }; pub use self::interner::Interner; @@ -234,7 +235,7 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { ty: TyKind::BoundVar(BoundVar { debruijn: DebruijnIndex::ONE, index: 0 }) .intern(&Interner), projection_ty: ProjectionTy { - associated_ty: future_output, + associated_ty: to_assoc_type_id(future_output), // Self type as the first parameter. parameters: Substs::single( TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) @@ -383,7 +384,7 @@ pub(crate) fn associated_ty_data_query( id: AssocTypeId, ) -> Arc { debug!("associated_ty_data {:?}", id); - let type_alias: TypeAliasId = from_chalk::(db, id).0; + let type_alias: TypeAliasId = from_assoc_type_id(id); let trait_ = match type_alias.lookup(db.upcast()).container { AssocContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), @@ -438,10 +439,8 @@ pub(crate) fn trait_datum_query( fundamental: false, }; let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars); - let associated_ty_ids = trait_data - .associated_types() - .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db)) - .collect(); + let associated_ty_ids = + trait_data.associated_types().map(|type_alias| to_assoc_type_id(type_alias)).collect(); let trait_datum_bound = rust_ir::TraitDatumBound { where_clauses }; let well_known = lang_attr(db.upcast(), trait_).and_then(|name| well_known_trait_from_lang_attr(&name)); @@ -623,7 +622,7 @@ fn type_alias_associated_ty_value( let value_bound = rust_ir::AssociatedTyValueBound { ty: ty.value.to_chalk(db) }; let value = rust_ir::AssociatedTyValue { impl_id: impl_id.to_chalk(db), - associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db), + associated_ty_id: to_assoc_type_id(assoc_ty), value: make_binders(value_bound, ty.num_binders), }; Arc::new(value) diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index cf490f9c5..67ac95a0e 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -14,6 +14,7 @@ use hir_def::{AssocContainerId, GenericDefId, Lookup, TypeAliasId}; use crate::{ db::HirDatabase, + from_assoc_type_id, primitive::UintTy, traits::{Canonical, Obligation}, AliasTy, CallableDefId, FnPointer, FnSig, GenericPredicate, InEnvironment, OpaqueTy, @@ -38,9 +39,7 @@ impl ToChalk for Ty { }) .intern(&Interner) } - TyKind::AssociatedType(type_alias, substs) => { - let assoc_type = TypeAliasAsAssocType(type_alias); - let assoc_type_id = assoc_type.to_chalk(db); + TyKind::AssociatedType(assoc_type_id, substs) => { let substitution = substs.to_chalk(db); chalk_ir::TyKind::AssociatedType(assoc_type_id, substitution).intern(&Interner) } @@ -85,7 +84,7 @@ impl ToChalk for Ty { chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner) } TyKind::Alias(AliasTy::Projection(proj_ty)) => { - let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); + let associated_ty_id = proj_ty.associated_ty; let substitution = proj_ty.parameters.to_chalk(db); chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { associated_ty_id, @@ -139,8 +138,7 @@ impl ToChalk for Ty { TyKind::Placeholder(db.lookup_intern_type_param_id(interned_id)) } chalk_ir::TyKind::Alias(chalk_ir::AliasTy::Projection(proj)) => { - let associated_ty = - from_chalk::(db, proj.associated_ty_id).0; + let associated_ty = proj.associated_ty_id; let parameters = from_chalk(db, proj.substitution); TyKind::Alias(AliasTy::Projection(ProjectionTy { associated_ty, parameters })) } @@ -180,10 +178,9 @@ impl ToChalk for Ty { } chalk_ir::TyKind::Adt(adt_id, subst) => TyKind::Adt(adt_id, from_chalk(db, subst)), - chalk_ir::TyKind::AssociatedType(type_id, subst) => TyKind::AssociatedType( - from_chalk::(db, type_id).0, - from_chalk(db, subst), - ), + chalk_ir::TyKind::AssociatedType(type_id, subst) => { + TyKind::AssociatedType(type_id, from_chalk(db, subst)) + } chalk_ir::TyKind::OpaqueType(opaque_type_id, subst) => { TyKind::OpaqueType(from_chalk(db, opaque_type_id), from_chalk(db, subst)) @@ -332,20 +329,6 @@ impl ToChalk for CallableDefId { } } -pub(crate) struct TypeAliasAsAssocType(pub(crate) TypeAliasId); - -impl ToChalk for TypeAliasAsAssocType { - type Chalk = AssocTypeId; - - fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { - chalk_ir::AssocTypeId(self.0.as_intern_id()) - } - - fn from_chalk(_db: &dyn HirDatabase, assoc_type_id: AssocTypeId) -> TypeAliasAsAssocType { - TypeAliasAsAssocType(InternKey::from_intern_id(assoc_type_id.0)) - } -} - pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId); impl ToChalk for TypeAliasAsValue { @@ -427,7 +410,7 @@ impl ToChalk for ProjectionTy { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy { chalk_ir::ProjectionTy { - associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), + associated_ty_id: self.associated_ty, substitution: self.parameters.to_chalk(db), } } @@ -437,11 +420,7 @@ impl ToChalk for ProjectionTy { projection_ty: chalk_ir::ProjectionTy, ) -> ProjectionTy { ProjectionTy { - associated_ty: from_chalk::( - db, - projection_ty.associated_ty_id, - ) - .0, + associated_ty: projection_ty.associated_ty_id, parameters: from_chalk(db, projection_ty.substitution), } } @@ -595,7 +574,10 @@ pub(super) fn generic_predicate_to_inline_bound( if &proj.projection_ty.parameters[0] != self_ty { return None; } - let trait_ = match proj.projection_ty.associated_ty.lookup(db.upcast()).container { + let trait_ = match from_assoc_type_id(proj.projection_ty.associated_ty) + .lookup(db.upcast()) + .container + { AssocContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), }; @@ -606,8 +588,7 @@ pub(super) fn generic_predicate_to_inline_bound( let alias_eq_bound = rust_ir::AliasEqBound { value: proj.ty.clone().to_chalk(db), trait_bound: rust_ir::TraitBound { trait_id: trait_.to_chalk(db), args_no_self }, - associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) - .to_chalk(db), + associated_ty_id: proj.projection_ty.associated_ty, parameters: Vec::new(), // FIXME we don't support generic associated types yet }; Some(rust_ir::InlineBound::AliasEqBound(alias_eq_bound)) diff --git a/crates/hir_ty/src/traits/chalk/tls.rs b/crates/hir_ty/src/traits/chalk/tls.rs index 75b16172e..8892a63a9 100644 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ b/crates/hir_ty/src/traits/chalk/tls.rs @@ -4,8 +4,8 @@ use std::fmt; use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication}; use itertools::Itertools; -use super::{from_chalk, Interner, TypeAliasAsAssocType}; -use crate::{db::HirDatabase, CallableDefId}; +use super::{from_chalk, Interner}; +use crate::{db::HirDatabase, from_assoc_type_id, CallableDefId}; use hir_def::{AdtId, AssocContainerId, Lookup, TypeAliasId}; pub(crate) use unsafe_tls::{set_current_program, with_current_program}; @@ -41,7 +41,7 @@ impl DebugContext<'_> { id: super::AssocTypeId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let type_alias: TypeAliasId = from_chalk::(self.0, id).0; + let type_alias: TypeAliasId = from_assoc_type_id(id); let type_alias_data = self.0.type_alias_data(type_alias); let trait_ = match type_alias.lookup(self.0.upcast()).container { AssocContainerId::TraitId(t) => t, @@ -75,8 +75,7 @@ impl DebugContext<'_> { projection_ty: &chalk_ir::ProjectionTy, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let type_alias: TypeAliasId = - from_chalk::(self.0, projection_ty.associated_ty_id).0; + let type_alias = from_assoc_type_id(projection_ty.associated_ty_id); let type_alias_data = self.0.type_alias_data(type_alias); let trait_ = match type_alias.lookup(self.0.upcast()).container { AssocContainerId::TraitId(t) => t, -- cgit v1.2.3