From a87579500a2c35597071efd0ad6983927f0c1815 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 27 Nov 2019 17:46:02 +0300 Subject: Move Ty --- crates/ra_hir/src/code_model.rs | 47 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir/src/code_model.rs') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 52ad4e5d1..87c78d98e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -6,8 +6,10 @@ use std::sync::Arc; use hir_def::{ adt::VariantData, + body::{Body, BodySourceMap}, builtin_type::BuiltinType, docs::Documentation, + expr::{BindingAnnotation, Pat, PatId}, per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, @@ -20,12 +22,12 @@ use hir_expand::{ name::{self, AsName}, AstId, MacroDefId, }; +use hir_ty::expr::ExprValidator; use ra_db::{CrateId, Edition, FileId, FilePosition}; use ra_syntax::{ast, AstNode, SyntaxNode}; use crate::{ db::{DefDatabase, HirDatabase}, - expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, ty::display::HirFormatter, ty::{ self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TyDefId, TypeCtor, @@ -353,8 +355,8 @@ impl Struct { .map(|(id, _)| StructField { parent: self.into(), id }) } - pub fn ty(self, db: &impl HirDatabase) -> Ty { - db.ty(self.id.into()) + pub fn ty(self, db: &impl HirDatabase) -> Type { + Type::from_def(db, self.id.module(db).krate, self.id) } pub fn constructor_ty(self, db: &impl HirDatabase) -> Ty { @@ -380,8 +382,8 @@ impl Union { Module { id: self.id.module(db) } } - pub fn ty(self, db: &impl HirDatabase) -> Ty { - db.ty(self.id.into()) + pub fn ty(self, db: &impl HirDatabase) -> Type { + Type::from_def(db, self.id.module(db).krate, self.id) } pub fn fields(self, db: &impl HirDatabase) -> Vec { @@ -441,8 +443,8 @@ impl Enum { .map(|(id, _)| EnumVariant { parent: self, id }) } - pub fn ty(self, db: &impl HirDatabase) -> Ty { - db.ty(self.id.into()) + pub fn ty(self, db: &impl HirDatabase) -> Type { + Type::from_def(db, self.id.module(db).krate, self.id) } } @@ -640,7 +642,7 @@ impl Function { pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { let infer = self.infer(db); infer.add_diagnostics(db, self.id, sink); - let mut validator = ExprValidator::new(self, infer, sink); + let mut validator = ExprValidator::new(self.id, infer, sink); validator.validate_body(db); } } @@ -946,13 +948,12 @@ impl ImplBlock { db.impl_data(self.id).target_type.clone() } - pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { - Ty::from_hir(db, &self.id.resolver(db), &self.target_type(db)) - } - - pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option { - let target_ty = self.target_ty(db); - TraitRef::from_hir(db, &self.id.resolver(db), &self.target_trait(db)?, Some(target_ty)) + pub fn target_ty(&self, db: &impl HirDatabase) -> Type { + let impl_data = db.impl_data(self.id); + let resolver = self.id.resolver(db); + let environment = TraitEnvironment::lower(db, &resolver); + let ty = Ty::from_hir(db, &resolver, &impl_data.target_type); + Type { krate: self.id.module(db).krate, ty: InEnvironment { value: ty, environment } } } pub fn items(&self, db: &impl DefDatabase) -> Vec { @@ -1130,6 +1131,22 @@ impl Type { Some(adt.into()) } + // FIXME: provide required accessors such that it becomes implementable from outside. + pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { + match (&self.ty.value, &other.ty.value) { + (Ty::Apply(a_original_ty), Ty::Apply(ty::ApplicationTy { ctor, parameters })) => { + match ctor { + TypeCtor::Ref(..) => match parameters.as_single() { + Ty::Apply(a_ty) => a_original_ty.ctor == a_ty.ctor, + _ => false, + }, + _ => a_original_ty.ctor == *ctor, + } + } + _ => false, + } + } + fn derived(&self, ty: Ty) -> Type { Type { krate: self.krate, -- cgit v1.2.3