From b302f69b7c5b1b966ec3f8637761ecb867e3bcca Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Sun, 13 Sep 2020 10:24:19 +0800 Subject: Update chalk to 0.27 and adapt to chalk changes. --- crates/hir_ty/src/traits/chalk.rs | 22 ++++++---- crates/hir_ty/src/traits/chalk/interner.rs | 1 + crates/hir_ty/src/traits/chalk/mapping.rs | 69 +++++++++++++++++++++++------- crates/hir_ty/src/traits/chalk/tls.rs | 11 +++-- 4 files changed, 76 insertions(+), 27 deletions(-) (limited to 'crates/hir_ty/src/traits') diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 57d0a32df..27f0ed628 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -23,7 +23,8 @@ use crate::{ ProjectionTy, Substs, TraitRef, Ty, TypeCtor, }; use mapping::{ - convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue, + convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsAssocType, + TypeAliasAsValue, }; pub use self::interner::*; @@ -340,7 +341,7 @@ pub(crate) fn associated_ty_data_query( id: AssocTypeId, ) -> Arc { debug!("associated_ty_data {:?}", id); - let type_alias: TypeAliasId = from_chalk(db, id); + let type_alias: TypeAliasId = from_chalk::(db, id).0; let trait_ = match type_alias.lookup(db.upcast()).container { AssocContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), @@ -394,8 +395,10 @@ 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| type_alias.to_chalk(db)).collect(); + let associated_ty_ids = trait_data + .associated_types() + .map(|type_alias| TypeAliasAsAssocType(type_alias).to_chalk(db)) + .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)); @@ -433,6 +436,7 @@ fn lang_attr_from_well_known_trait(attr: WellKnownTrait) -> &'static str { WellKnownTrait::FnMut => "fn_mut", WellKnownTrait::Fn => "fn", WellKnownTrait::Unsize => "unsize", + WellKnownTrait::Unpin => "unpin", } } @@ -576,7 +580,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: assoc_ty.to_chalk(db), + associated_ty_id: TypeAliasAsAssocType(assoc_ty).to_chalk(db), value: make_binders(value_bound, ty.num_binders), }; Arc::new(value) @@ -611,9 +615,11 @@ pub(crate) fn fn_def_datum_query( }; let datum = FnDefDatum { id: fn_def_id, - abi: (), - safety: chalk_ir::Safety::Safe, - variadic: sig.value.is_varargs, + sig: chalk_ir::FnSig { + abi: (), + safety: chalk_ir::Safety::Safe, + variadic: sig.value.is_varargs, + }, binders: make_binders(bound, sig.num_binders), }; Arc::new(datum) diff --git a/crates/hir_ty/src/traits/chalk/interner.rs b/crates/hir_ty/src/traits/chalk/interner.rs index eb35db3ff..f9304b7d0 100644 --- a/crates/hir_ty/src/traits/chalk/interner.rs +++ b/crates/hir_ty/src/traits/chalk/interner.rs @@ -12,6 +12,7 @@ pub struct Interner; pub type AssocTypeId = chalk_ir::AssocTypeId; pub type AssociatedTyDatum = chalk_solve::rust_ir::AssociatedTyDatum; +pub type ForeignDefId = chalk_ir::ForeignDefId; pub type TraitId = chalk_ir::TraitId; pub type TraitDatum = chalk_solve::rust_ir::TraitDatum; pub type AdtId = chalk_ir::AdtId; diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index d6bacba1d..d42f4bba9 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -34,9 +34,11 @@ impl ToChalk for Ty { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::FnPointer { num_binders: 0, - abi: (), - safety: chalk_ir::Safety::Safe, - variadic: is_varargs, + sig: chalk_ir::FnSig { + abi: (), + safety: chalk_ir::Safety::Safe, + variadic: is_varargs, + }, substitution, }) .intern(&Interner) @@ -48,7 +50,7 @@ impl ToChalk for Ty { } }, Ty::Projection(proj_ty) => { - let associated_ty_id = proj_ty.associated_ty.to_chalk(db); + let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db); let substitution = proj_ty.parameters.to_chalk(db); chalk_ir::AliasTy::Projection(chalk_ir::ProjectionTy { associated_ty_id, @@ -114,7 +116,8 @@ impl ToChalk for Ty { Ty::Placeholder(db.lookup_intern_type_param_id(interned_id)) } chalk_ir::TyData::Alias(chalk_ir::AliasTy::Projection(proj)) => { - let associated_ty = from_chalk(db, proj.associated_ty_id); + let associated_ty = + from_chalk::(db, proj.associated_ty_id).0; let parameters = from_chalk(db, proj.substitution); Ty::Projection(ProjectionTy { associated_ty, parameters }) } @@ -125,7 +128,7 @@ impl ToChalk for Ty { } chalk_ir::TyData::Function(chalk_ir::FnPointer { num_binders, - variadic, + sig: chalk_ir::FnSig { variadic, .. }, substitution, .. }) => { @@ -290,8 +293,9 @@ impl ToChalk for TypeCtor { fn to_chalk(self, db: &dyn HirDatabase) -> TypeName { match self { TypeCtor::AssociatedType(type_alias) => { - let type_id = type_alias.to_chalk(db); - TypeName::AssociatedType(type_id) + let assoc_type = TypeAliasAsAssocType(type_alias); + let assoc_type_id = assoc_type.to_chalk(db); + TypeName::AssociatedType(assoc_type_id) } TypeCtor::OpaqueType(impl_trait_id) => { @@ -299,6 +303,12 @@ impl ToChalk for TypeCtor { TypeName::OpaqueType(id) } + TypeCtor::ForeignType(type_alias) => { + let foreign_type = TypeAliasAsForeignType(type_alias); + let foreign_type_id = foreign_type.to_chalk(db); + TypeName::Foreign(foreign_type_id) + } + TypeCtor::Bool => TypeName::Scalar(Scalar::Bool), TypeCtor::Char => TypeName::Scalar(Scalar::Char), TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)), @@ -339,7 +349,9 @@ impl ToChalk for TypeCtor { fn from_chalk(db: &dyn HirDatabase, type_name: TypeName) -> TypeCtor { match type_name { TypeName::Adt(struct_id) => TypeCtor::Adt(struct_id.0), - TypeName::AssociatedType(type_id) => TypeCtor::AssociatedType(from_chalk(db, type_id)), + TypeName::AssociatedType(type_id) => { + TypeCtor::AssociatedType(from_chalk::(db, type_id).0) + } TypeName::OpaqueType(opaque_type_id) => { TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)) } @@ -379,6 +391,10 @@ impl ToChalk for TypeCtor { TypeCtor::Closure { def, expr } } + TypeName::Foreign(foreign_def_id) => { + TypeCtor::ForeignType(from_chalk::(db, foreign_def_id).0) + } + TypeName::Error => { // this should not be reached, since we don't represent TypeName::Error with TypeCtor unreachable!() @@ -488,15 +504,31 @@ impl ToChalk for CallableDefId { } } -impl ToChalk for TypeAliasId { +pub struct TypeAliasAsAssocType(pub TypeAliasId); + +impl ToChalk for TypeAliasAsAssocType { type Chalk = AssocTypeId; fn to_chalk(self, _db: &dyn HirDatabase) -> AssocTypeId { - chalk_ir::AssocTypeId(self.as_intern_id()) + 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 struct TypeAliasAsForeignType(pub TypeAliasId); + +impl ToChalk for TypeAliasAsForeignType { + type Chalk = ForeignDefId; + + fn to_chalk(self, _db: &dyn HirDatabase) -> ForeignDefId { + chalk_ir::ForeignDefId(self.0.as_intern_id()) } - fn from_chalk(_db: &dyn HirDatabase, type_alias_id: AssocTypeId) -> TypeAliasId { - InternKey::from_intern_id(type_alias_id.0) + fn from_chalk(_db: &dyn HirDatabase, foreign_def_id: ForeignDefId) -> TypeAliasAsForeignType { + TypeAliasAsForeignType(InternKey::from_intern_id(foreign_def_id.0)) } } @@ -580,7 +612,7 @@ impl ToChalk for ProjectionTy { fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::ProjectionTy { chalk_ir::ProjectionTy { - associated_ty_id: self.associated_ty.to_chalk(db), + associated_ty_id: TypeAliasAsAssocType(self.associated_ty).to_chalk(db), substitution: self.parameters.to_chalk(db), } } @@ -590,7 +622,11 @@ impl ToChalk for ProjectionTy { projection_ty: chalk_ir::ProjectionTy, ) -> ProjectionTy { ProjectionTy { - associated_ty: from_chalk(db, projection_ty.associated_ty_id), + associated_ty: from_chalk::( + db, + projection_ty.associated_ty_id, + ) + .0, parameters: from_chalk(db, projection_ty.substitution), } } @@ -789,7 +825,8 @@ 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: proj.projection_ty.associated_ty.to_chalk(db), + associated_ty_id: TypeAliasAsAssocType(proj.projection_ty.associated_ty) + .to_chalk(db), 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 cb6b0fe81..b4568cff6 100644 --- a/crates/hir_ty/src/traits/chalk/tls.rs +++ b/crates/hir_ty/src/traits/chalk/tls.rs @@ -4,7 +4,7 @@ use std::fmt; use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplication, TypeName}; use itertools::Itertools; -use super::{from_chalk, Interner}; +use super::{from_chalk, Interner, TypeAliasAsAssocType}; use crate::{db::HirDatabase, CallableDefId, TypeCtor}; use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId}; @@ -77,6 +77,10 @@ impl DebugContext<'_> { write!(f, "{{impl trait of async block {} of {:?}}}", idx.into_raw(), def)?; } }, + TypeCtor::ForeignType(type_alias) => { + let name = self.0.type_alias_data(type_alias).name.clone(); + write!(f, "{}", name)?; + } TypeCtor::Closure { def, expr } => { write!(f, "{{closure {:?} in ", expr.into_raw())?; match def { @@ -119,7 +123,7 @@ impl DebugContext<'_> { id: super::AssocTypeId, fmt: &mut fmt::Formatter<'_>, ) -> Result<(), fmt::Error> { - let type_alias: TypeAliasId = from_chalk(self.0, id); + let type_alias: TypeAliasId = from_chalk::(self.0, id).0; 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, @@ -153,7 +157,8 @@ 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); + let type_alias: TypeAliasId = + from_chalk::(self.0, projection_ty.associated_ty_id).0; 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