From d4f4ae0dd88f0baf0065d612f41fea3d54af79fd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 18:46:39 +0300 Subject: [PATCH 1/2] Move const&static date to hir_def --- crates/ra_hir/src/code_model.rs | 51 ++++----------------------------- crates/ra_hir/src/db.rs | 18 ++++-------- crates/ra_hir/src/lib.rs | 8 +++--- crates/ra_hir/src/ty/infer.rs | 8 +++--- crates/ra_hir/src/ty/lower.rs | 4 +-- crates/ra_hir_def/src/data.rs | 29 +++++++++++++++++-- crates/ra_hir_def/src/db.rs | 12 ++++++-- 7 files changed, 58 insertions(+), 72 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3daf7488ed..1b65eefe54 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,7 +10,7 @@ use hir_def::{ adt::VariantData, body::scope::ExprScopes, builtin_type::BuiltinType, - data::TraitData, + data::{ConstData, TraitData}, nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, @@ -22,10 +22,10 @@ use hir_expand::{ name::{self, AsName}, }; use ra_db::{CrateId, Edition}; -use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; +use ra_syntax::ast; use crate::{ - db::{AstDatabase, DefDatabase, HirDatabase}, + db::{DefDatabase, HirDatabase}, expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId}, ids::{ AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId, @@ -645,11 +645,11 @@ impl Const { } pub fn data(self, db: &impl HirDatabase) -> Arc { - db.const_data(self) + db.const_data(self.id) } pub fn name(self, db: &impl HirDatabase) -> Option { - self.data(db).name().cloned() + self.data(db).name.clone() } pub fn infer(self, db: &impl HirDatabase) -> Arc { @@ -681,45 +681,6 @@ impl Const { } } -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ConstData { - pub(crate) name: Option, - pub(crate) type_ref: TypeRef, -} - -impl ConstData { - pub fn name(&self) -> Option<&Name> { - self.name.as_ref() - } - - pub fn type_ref(&self) -> &TypeRef { - &self.type_ref - } - - pub(crate) fn const_data_query( - db: &(impl DefDatabase + AstDatabase), - konst: Const, - ) -> Arc { - let node = konst.source(db).value; - const_data_for(&node) - } - - pub(crate) fn static_data_query( - db: &(impl DefDatabase + AstDatabase), - konst: Static, - ) -> Arc { - let node = konst.source(db).value; - const_data_for(&node) - } -} - -fn const_data_for(node: &N) -> Arc { - let name = node.name().map(|n| n.as_name()); - let type_ref = TypeRef::from_ast_opt(node.ascribed_type()); - let sig = ConstData { name, type_ref }; - Arc::new(sig) -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Static { pub(crate) id: StaticId, @@ -735,7 +696,7 @@ impl Static { } pub fn data(self, db: &impl HirDatabase) -> Arc { - db.static_data(self) + db.static_data(self.id) } pub fn infer(self, db: &impl HirDatabase) -> Arc { diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 8b9af05652..85d46b485f 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -16,15 +16,15 @@ use crate::{ CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, }, - Const, ConstData, Crate, DefWithBody, GenericDef, ImplBlock, Module, Static, StructField, - Trait, + Crate, DefWithBody, GenericDef, ImplBlock, Module, StructField, Trait, }; pub use hir_def::db::{ - BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, - EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, - InternDatabase, InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, - StructDataQuery, TraitDataQuery, TypeAliasDataQuery, + BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, DefDatabase2, + DefDatabase2Storage, EnumDataQuery, ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, + ImplDataQuery, InternDatabase, InternDatabaseStorage, RawItemsQuery, + RawItemsWithSourceMapQuery, StaticDataQuery, StructDataQuery, TraitDataQuery, + TypeAliasDataQuery, }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, @@ -35,12 +35,6 @@ pub use hir_expand::db::{ #[salsa::query_group(DefDatabaseStorage)] #[salsa::requires(AstDatabase)] pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { - #[salsa::invoke(ConstData::const_data_query)] - fn const_data(&self, konst: Const) -> Arc; - - #[salsa::invoke(ConstData::static_data_query)] - fn static_data(&self, konst: Static) -> Arc; - #[salsa::invoke(LangItems::module_lang_items_query)] fn module_lang_items(&self, module: Module) -> Option>; diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index d29cc92580..e164c9b327 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -54,10 +54,10 @@ pub use crate::{ attrs::{AttrDef, HasAttrs}, docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, - Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, - EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, - MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, - TypeAlias, Union, VariantDef, + Adt, AssocItem, Const, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, + FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, MacroDef, + Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, TypeAlias, + Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 6e07ab86e8..39976a359e 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -22,7 +22,7 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use rustc_hash::FxHashMap; use hir_def::{ - data::FunctionData, + data::{ConstData, FunctionData}, path::known, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{Mutability, TypeRef}, @@ -44,8 +44,8 @@ use crate::{ db::HirDatabase, expr::{BindingAnnotation, Body, ExprId, PatId}, ty::infer::diagnostics::InferenceDiagnostic, - Adt, AssocItem, ConstData, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, - Trait, VariantDef, + Adt, AssocItem, DefWithBody, FloatTy, Function, HasBody, IntTy, Path, StructField, Trait, + VariantDef, }; macro_rules! ty_app { @@ -560,7 +560,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } fn collect_const(&mut self, data: &ConstData) { - self.return_ty = self.make_ty(data.type_ref()); + self.return_ty = self.make_ty(&data.type_ref); } fn collect_fn(&mut self, data: &FunctionData) { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 42daa9cb93..ac7f3f7752 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -642,7 +642,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { let data = def.data(db); let resolver = def.id.resolver(db); - Ty::from_hir(db, &resolver, data.type_ref()) + Ty::from_hir(db, &resolver, &data.type_ref) } /// Build the declared type of a static. @@ -650,7 +650,7 @@ fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { let data = def.data(db); let resolver = def.id.resolver(db); - Ty::from_hir(db, &resolver, data.type_ref()) + Ty::from_hir(db, &resolver, &data.type_ref) } /// Build the declared type of a static. diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index ba47629db3..91bac7415a 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -11,8 +11,8 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use crate::{ db::DefDatabase2, type_ref::{Mutability, TypeRef}, - AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, ImplId, - Intern, Lookup, TraitId, TypeAliasId, TypeAliasLoc, + AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, + ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -190,3 +190,28 @@ impl ImplData { Arc::new(res) } } + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ConstData { + pub name: Option, + pub type_ref: TypeRef, +} + +impl ConstData { + pub(crate) fn const_data_query(db: &impl DefDatabase2, konst: ConstId) -> Arc { + let node = konst.lookup(db).source(db).value; + const_data_for(&node) + } + + pub(crate) fn static_data_query(db: &impl DefDatabase2, konst: StaticId) -> Arc { + let node = konst.source(db).value; + const_data_for(&node) + } +} + +fn const_data_for(node: &N) -> Arc { + let name = node.name().map(|n| n.as_name()); + let type_ref = TypeRef::from_ast_opt(node.ascribed_type()); + let sig = ConstData { name, type_ref }; + Arc::new(sig) +} diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 8c1784ec9d..2c660ab88b 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -8,14 +8,14 @@ use ra_syntax::ast; use crate::{ adt::{EnumData, StructData}, body::{scope::ExprScopes, Body, BodySourceMap}, - data::{FunctionData, ImplData, TraitData, TypeAliasData}, + data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData}, generics::GenericParams, nameres::{ raw::{ImportSourceMap, RawItems}, CrateDefMap, }, - DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, - TypeAliasId, + ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StaticId, + StructOrUnionId, TraitId, TypeAliasId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -70,6 +70,12 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { #[salsa::invoke(FunctionData::fn_data_query)] fn function_data(&self, func: FunctionId) -> Arc; + #[salsa::invoke(ConstData::const_data_query)] + fn const_data(&self, konst: ConstId) -> Arc; + + #[salsa::invoke(ConstData::static_data_query)] + fn static_data(&self, konst: StaticId) -> Arc; + #[salsa::invoke(Body::body_with_source_map_query)] fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc, Arc); From fe119fef2746bade402fb73dcba8cde49779654d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 18:51:53 +0300 Subject: [PATCH 2/2] Hide data from public API --- crates/ra_hir/src/code_model.rs | 12 ++---------- crates/ra_hir/src/ty/infer.rs | 8 ++++---- crates/ra_hir/src/ty/lower.rs | 4 ++-- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 1b65eefe54..af07b2c4dd 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -10,7 +10,7 @@ use hir_def::{ adt::VariantData, body::scope::ExprScopes, builtin_type::BuiltinType, - data::{ConstData, TraitData}, + data::TraitData, nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, @@ -644,12 +644,8 @@ impl Const { Some(self.module(db).krate()) } - pub fn data(self, db: &impl HirDatabase) -> Arc { - db.const_data(self.id) - } - pub fn name(self, db: &impl HirDatabase) -> Option { - self.data(db).name.clone() + db.const_data(self.id).name.clone() } pub fn infer(self, db: &impl HirDatabase) -> Arc { @@ -695,10 +691,6 @@ impl Static { Some(self.module(db).krate()) } - pub fn data(self, db: &impl HirDatabase) -> Arc { - db.static_data(self.id) - } - pub fn infer(self, db: &impl HirDatabase) -> Arc { db.infer(self.into()) } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 39976a359e..471bdc3878 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -69,10 +69,10 @@ pub fn infer_query(db: &impl HirDatabase, def: DefWithBody) -> Arc ctx.collect_const(&c.data(db)), - DefWithBody::Function(ref f) => ctx.collect_fn(&db.function_data(f.id)), - DefWithBody::Static(ref s) => ctx.collect_const(&s.data(db)), + match &def { + DefWithBody::Const(c) => ctx.collect_const(&db.const_data(c.id)), + DefWithBody::Function(f) => ctx.collect_fn(&db.function_data(f.id)), + DefWithBody::Static(s) => ctx.collect_const(&db.static_data(s.id)), } ctx.infer_body(); diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index ac7f3f7752..2272510e8d 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -639,7 +639,7 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { /// Build the declared type of a const. fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { - let data = def.data(db); + let data = db.const_data(def.id); let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, &data.type_ref) @@ -647,7 +647,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty { /// Build the declared type of a static. fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty { - let data = def.data(db); + let data = db.static_data(def.id); let resolver = def.id.resolver(db); Ty::from_hir(db, &resolver, &data.type_ref)