From 788533d38091472ed05c8b55fd7a2002c49f4eb7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Apr 2021 21:14:49 +0200 Subject: Move ProjectionTy methods to extension trait --- crates/hir_ty/src/chalk_ext.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src/chalk_ext.rs') diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index b7463366b..0f4cb43e9 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -1,6 +1,11 @@ //! Various extensions traits for Chalk types. -use crate::{Interner, Ty, TyKind}; +use hir_def::{AssocContainerId, Lookup, TraitId}; + +use crate::{ + db::HirDatabase, from_assoc_type_id, to_chalk_trait_id, Interner, ProjectionTy, TraitRef, Ty, + TyKind, +}; pub trait TyExt { fn is_unit(&self) -> bool; @@ -11,3 +16,24 @@ impl TyExt for Ty { matches!(self.kind(&Interner), TyKind::Tuple(0, _)) } } + +pub trait ProjectionTyExt { + fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef; + fn trait_(&self, db: &dyn HirDatabase) -> TraitId; +} + +impl ProjectionTyExt for ProjectionTy { + fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef { + TraitRef { + trait_id: to_chalk_trait_id(self.trait_(db)), + substitution: self.substitution.clone(), + } + } + + fn trait_(&self, db: &dyn HirDatabase) -> TraitId { + match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container { + AssocContainerId::TraitId(it) => it, + _ => panic!("projection ty without parent trait"), + } + } +} -- cgit v1.2.3