From 559aeb2932368b40cda9ff8d3f7c4cf3aa065819 Mon Sep 17 00:00:00 2001 From: veetaha Date: Mon, 23 Mar 2020 02:01:07 +0200 Subject: ra_hir: add more privacy for Type --- crates/ra_hir/src/code_model.rs | 20 ++++++++++++++------ crates/ra_hir/src/source_analyzer.rs | 12 +++--------- 2 files changed, 17 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 4b150ef06..e91abf6f5 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,7 +10,7 @@ use hir_def::{ docs::Documentation, expr::{BindingAnnotation, Pat, PatId}, per_ns::PerNs, - resolver::HasResolver, + resolver::{HasResolver, Resolver}, type_ref::{Mutability, TypeRef}, AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LocalEnumVariantId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, @@ -912,10 +912,8 @@ impl Local { let def = DefWithBodyId::from(self.parent); let infer = db.infer(def); let ty = infer[self.pat_id].clone(); - let resolver = def.resolver(db.upcast()); let krate = def.module(db.upcast()).krate; - let environment = TraitEnvironment::lower(db, &resolver); - Type { krate, ty: InEnvironment { value: ty, environment } } + Type::new(db, krate, def, ty) } pub fn source(self, db: &dyn HirDatabase) -> InFile> { @@ -1020,11 +1018,21 @@ impl ImplDef { #[derive(Clone, PartialEq, Eq, Debug)] pub struct Type { - pub(crate) krate: CrateId, - pub(crate) ty: InEnvironment, + krate: CrateId, + ty: InEnvironment, } impl Type { + pub(crate) fn new_with_resolver( + db: &dyn HirDatabase, + resolver: &Resolver, + ty: Ty, + ) -> Option { + let krate = resolver.krate()?; + let environment = TraitEnvironment::lower(db, &resolver); + Some(Type { krate, ty: InEnvironment { value: ty, environment } }) + } + fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type { let resolver = lexical_env.resolver(db.upcast()); let environment = TraitEnvironment::lower(db, &resolver); diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index e8afef328..10c12c910 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -17,7 +17,7 @@ use hir_def::{ AsMacroCall, DefWithBodyId, }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; -use hir_ty::{InEnvironment, InferenceResult, TraitEnvironment}; +use hir_ty::InferenceResult; use ra_syntax::{ ast::{self, AstNode}, SyntaxNode, SyntaxNodePtr, TextUnit, @@ -103,10 +103,6 @@ impl SourceAnalyzer { Some(res) } - fn trait_env(&self, db: &dyn HirDatabase) -> Arc { - TraitEnvironment::lower(db, &self.resolver) - } - pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option { let expr_id = match expr { ast::Expr::MacroCall(call) => { @@ -117,15 +113,13 @@ impl SourceAnalyzer { }?; let ty = self.infer.as_ref()?[expr_id].clone(); - let environment = self.trait_env(db); - Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) + Type::new_with_resolver(db, &self.resolver, ty) } pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option { let pat_id = self.pat_id(pat)?; let ty = self.infer.as_ref()?[pat_id].clone(); - let environment = self.trait_env(db); - Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) + Type::new_with_resolver(db, &self.resolver, ty) } pub(crate) fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { -- cgit v1.2.3