2635: Remove import source map r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-12-21 16:35:21 +00:00 committed by GitHub
commit c59d10ab35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 107 deletions

View file

@ -4,8 +4,8 @@ pub use hir_def::db::{
BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, StaticDataQuery, StructDataQuery,
StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery, TraitDataQuery, TypeAliasDataQuery,
}; };
pub use hir_expand::db::{ pub use hir_expand::db::{
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,

View file

@ -13,10 +13,7 @@ use crate::{
docs::Documentation, docs::Documentation,
generics::GenericParams, generics::GenericParams,
lang_item::{LangItemTarget, LangItems}, lang_item::{LangItemTarget, LangItems},
nameres::{ nameres::{raw::RawItems, CrateDefMap},
raw::{ImportSourceMap, RawItems},
CrateDefMap,
},
AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
@ -46,12 +43,6 @@ pub trait InternDatabase: SourceDatabase {
#[salsa::query_group(DefDatabaseStorage)] #[salsa::query_group(DefDatabaseStorage)]
pub trait DefDatabase: InternDatabase + AstDatabase { pub trait DefDatabase: InternDatabase + AstDatabase {
#[salsa::invoke(RawItems::raw_items_with_source_map_query)]
fn raw_items_with_source_map(
&self,
file_id: HirFileId,
) -> (Arc<RawItems>, Arc<ImportSourceMap>);
#[salsa::invoke(RawItems::raw_items_query)] #[salsa::invoke(RawItems::raw_items_query)]
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>; fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;

View file

@ -5,7 +5,7 @@ use hir_expand::name::Name;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId}; use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq)]
pub struct ItemScope { pub struct ItemScope {
@ -30,7 +30,7 @@ static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
BuiltinType::ALL BuiltinType::ALL
.iter() .iter()
.map(|(name, ty)| { .map(|(name, ty)| {
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: false })
}) })
.collect() .collect()
}); });
@ -54,7 +54,7 @@ impl ItemScope {
pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
self.entries() self.entries()
.filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) .filter_map(|(_name, res)| if !res.import { Some(res.def) } else { None })
.flat_map(|per_ns| { .flat_map(|per_ns| {
per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
}) })
@ -112,36 +112,27 @@ impl ItemScope {
self.legacy_macros.insert(name, mac); self.legacy_macros.insert(name, mac);
} }
pub(crate) fn push_res( pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, import: bool) -> bool {
&mut self,
name: Name,
res: &Resolution,
import: Option<LocalImportId>,
) -> bool {
let mut changed = false; let mut changed = false;
let existing = self.items.entry(name.clone()).or_default(); let existing = self.items.entry(name.clone()).or_default();
if existing.def.types.is_none() && res.def.types.is_some() { if existing.def.types.is_none() && res.def.types.is_some() {
existing.def.types = res.def.types; existing.def.types = res.def.types;
existing.import = import.or(res.import); existing.import = import || res.import;
changed = true; changed = true;
} }
if existing.def.values.is_none() && res.def.values.is_some() { if existing.def.values.is_none() && res.def.values.is_some() {
existing.def.values = res.def.values; existing.def.values = res.def.values;
existing.import = import.or(res.import); existing.import = import || res.import;
changed = true; changed = true;
} }
if existing.def.macros.is_none() && res.def.macros.is_some() { if existing.def.macros.is_none() && res.def.macros.is_some() {
existing.def.macros = res.def.macros; existing.def.macros = res.def.macros;
existing.import = import.or(res.import); existing.import = import || res.import;
changed = true; changed = true;
} }
if existing.def.is_none() if existing.def.is_none() && res.def.is_none() && !existing.import && res.import {
&& res.def.is_none()
&& existing.import.is_none()
&& res.import.is_some()
{
existing.import = res.import; existing.import = res.import;
} }
changed changed
@ -160,6 +151,5 @@ impl ItemScope {
pub struct Resolution { pub struct Resolution {
/// None for unresolved /// None for unresolved
pub def: PerNs, pub def: PerNs,
/// ident by which this is imported into local scope. pub(crate) import: bool,
pub(crate) import: Option<LocalImportId>,
} }

View file

@ -51,10 +51,6 @@ use ra_syntax::{ast, AstNode};
use crate::body::Expander; use crate::body::Expander;
use crate::builtin_type::BuiltinType; use crate::builtin_type::BuiltinType;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct LocalImportId(RawId);
impl_arena_id!(LocalImportId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ModuleId { pub struct ModuleId {
pub krate: CrateId, pub krate: CrateId,

View file

@ -26,8 +26,7 @@ use crate::{
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
TypeAliasLoc, UnionLoc,
}; };
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@ -93,7 +92,7 @@ impl PartialResolvedImport {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
struct ImportDirective { struct ImportDirective {
module_id: LocalModuleId, module_id: LocalModuleId,
import_id: LocalImportId, import_id: raw::Import,
import: raw::ImportData, import: raw::ImportData,
status: PartialResolvedImport, status: PartialResolvedImport,
} }
@ -110,7 +109,7 @@ struct MacroDirective {
struct DefCollector<'a, DB> { struct DefCollector<'a, DB> {
db: &'a DB, db: &'a DB,
def_map: CrateDefMap, def_map: CrateDefMap,
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, LocalImportId)>>, glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::Import)>>,
unresolved_imports: Vec<ImportDirective>, unresolved_imports: Vec<ImportDirective>,
resolved_imports: Vec<ImportDirective>, resolved_imports: Vec<ImportDirective>,
unexpanded_macros: Vec<MacroDirective>, unexpanded_macros: Vec<MacroDirective>,
@ -219,7 +218,7 @@ where
self.update( self.update(
self.def_map.root, self.def_map.root,
None, None,
&[(name, Resolution { def: PerNs::macros(macro_), import: None })], &[(name, Resolution { def: PerNs::macros(macro_), import: false })],
); );
} }
} }
@ -404,7 +403,7 @@ where
let variant = EnumVariantId { parent: e, local_id }; let variant = EnumVariantId { parent: e, local_id };
let res = Resolution { let res = Resolution {
def: PerNs::both(variant.into(), variant.into()), def: PerNs::both(variant.into(), variant.into()),
import: Some(import_id), import: true,
}; };
(name, res) (name, res)
}) })
@ -431,7 +430,7 @@ where
} }
} }
let resolution = Resolution { def, import: Some(import_id) }; let resolution = Resolution { def, import: true };
self.update(module_id, Some(import_id), &[(name, resolution)]); self.update(module_id, Some(import_id), &[(name, resolution)]);
} }
None => tested_by!(bogus_paths), None => tested_by!(bogus_paths),
@ -442,7 +441,7 @@ where
fn update( fn update(
&mut self, &mut self,
module_id: LocalModuleId, module_id: LocalModuleId,
import: Option<LocalImportId>, import: Option<raw::Import>,
resolutions: &[(Name, Resolution)], resolutions: &[(Name, Resolution)],
) { ) {
self.update_recursive(module_id, import, resolutions, 0) self.update_recursive(module_id, import, resolutions, 0)
@ -451,7 +450,7 @@ where
fn update_recursive( fn update_recursive(
&mut self, &mut self,
module_id: LocalModuleId, module_id: LocalModuleId,
import: Option<LocalImportId>, import: Option<raw::Import>,
resolutions: &[(Name, Resolution)], resolutions: &[(Name, Resolution)],
depth: usize, depth: usize,
) { ) {
@ -462,7 +461,7 @@ where
let scope = &mut self.def_map.modules[module_id].scope; let scope = &mut self.def_map.modules[module_id].scope;
let mut changed = false; let mut changed = false;
for (name, res) in resolutions { for (name, res) in resolutions {
changed |= scope.push_res(name.clone(), res, import); changed |= scope.push_res(name.clone(), res, import.is_some());
} }
if !changed { if !changed {
@ -719,7 +718,7 @@ where
def: PerNs::types( def: PerNs::types(
ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
), ),
import: None, import: false,
}; };
self.def_collector.update(self.module_id, None, &[(name, resolution)]); self.def_collector.update(self.module_id, None, &[(name, resolution)]);
res res
@ -791,7 +790,7 @@ where
PerNs::types(def.into()) PerNs::types(def.into())
} }
}; };
let resolution = Resolution { def, import: None }; let resolution = Resolution { def, import: false };
self.def_collector.update(self.module_id, None, &[(name, resolution)]) self.def_collector.update(self.module_id, None, &[(name, resolution)])
} }

View file

@ -7,24 +7,20 @@
use std::{ops::Index, sync::Arc}; use std::{ops::Index, sync::Arc};
use either::Either;
use hir_expand::{ use hir_expand::{
ast_id_map::AstIdMap, ast_id_map::AstIdMap,
db::AstDatabase, db::AstDatabase,
hygiene::Hygiene, hygiene::Hygiene,
name::{AsName, Name}, name::{AsName, Name},
}; };
use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_arena::{impl_arena_id, Arena, RawId};
use ra_syntax::{ use ra_syntax::{
ast::{self, AttrsOwner, NameOwner}, ast::{self, AttrsOwner, NameOwner},
AstNode, AstPtr, AstNode,
}; };
use test_utils::tested_by; use test_utils::tested_by;
use crate::{ use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile};
attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile,
LocalImportId,
};
/// `RawItems` is a set of top-level items in a file (except for impls). /// `RawItems` is a set of top-level items in a file (except for impls).
/// ///
@ -33,7 +29,7 @@ use crate::{
#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq)]
pub struct RawItems { pub struct RawItems {
modules: Arena<Module, ModuleData>, modules: Arena<Module, ModuleData>,
imports: Arena<LocalImportId, ImportData>, imports: Arena<Import, ImportData>,
defs: Arena<Def, DefData>, defs: Arena<Def, DefData>,
macros: Arena<Macro, MacroData>, macros: Arena<Macro, MacroData>,
impls: Arena<Impl, ImplData>, impls: Arena<Impl, ImplData>,
@ -41,29 +37,14 @@ pub struct RawItems {
items: Vec<RawItem>, items: Vec<RawItem>,
} }
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ImportSourceMap {
map: ArenaMap<LocalImportId, ImportSourcePtr>,
}
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
impl RawItems { impl RawItems {
pub(crate) fn raw_items_query( pub(crate) fn raw_items_query(
db: &(impl DefDatabase + AstDatabase), db: &(impl DefDatabase + AstDatabase),
file_id: HirFileId, file_id: HirFileId,
) -> Arc<RawItems> { ) -> Arc<RawItems> {
db.raw_items_with_source_map(file_id).0
}
pub(crate) fn raw_items_with_source_map_query(
db: &(impl DefDatabase + AstDatabase),
file_id: HirFileId,
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
let mut collector = RawItemsCollector { let mut collector = RawItemsCollector {
raw_items: RawItems::default(), raw_items: RawItems::default(),
source_ast_id_map: db.ast_id_map(file_id), source_ast_id_map: db.ast_id_map(file_id),
imports: Trace::new(),
file_id, file_id,
hygiene: Hygiene::new(db, file_id), hygiene: Hygiene::new(db, file_id),
}; };
@ -74,11 +55,8 @@ impl RawItems {
collector.process_module(None, item_list); collector.process_module(None, item_list);
} }
} }
let mut raw_items = collector.raw_items; let raw_items = collector.raw_items;
let (arena, map) = collector.imports.into_arena_and_map(); Arc::new(raw_items)
raw_items.imports = arena;
let source_map = ImportSourceMap { map };
(Arc::new(raw_items), Arc::new(source_map))
} }
pub(super) fn items(&self) -> &[RawItem] { pub(super) fn items(&self) -> &[RawItem] {
@ -93,9 +71,9 @@ impl Index<Module> for RawItems {
} }
} }
impl Index<LocalImportId> for RawItems { impl Index<Import> for RawItems {
type Output = ImportData; type Output = ImportData;
fn index(&self, idx: LocalImportId) -> &ImportData { fn index(&self, idx: Import) -> &ImportData {
&self.imports[idx] &self.imports[idx]
} }
} }
@ -130,7 +108,7 @@ pub(super) struct RawItem {
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub(super) enum RawItemKind { pub(super) enum RawItemKind {
Module(Module), Module(Module),
Import(LocalImportId), Import(Import),
Def(Def), Def(Def),
Macro(Macro), Macro(Macro),
Impl(Impl), Impl(Impl),
@ -146,6 +124,10 @@ pub(super) enum ModuleData {
Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> }, Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct Import(RawId);
impl_arena_id!(Import);
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportData { pub struct ImportData {
pub(super) path: ModPath, pub(super) path: ModPath,
@ -217,7 +199,6 @@ pub(super) struct ImplData {
struct RawItemsCollector { struct RawItemsCollector {
raw_items: RawItems, raw_items: RawItems,
imports: Trace<LocalImportId, ImportData, ImportSourcePtr>,
source_ast_id_map: Arc<AstIdMap>, source_ast_id_map: Arc<AstIdMap>,
file_id: HirFileId, file_id: HirFileId,
hygiene: Hygiene, hygiene: Hygiene,
@ -324,7 +305,7 @@ impl RawItemsCollector {
ModPath::expand_use_item( ModPath::expand_use_item(
InFile { value: use_item, file_id: self.file_id }, InFile { value: use_item, file_id: self.file_id },
&self.hygiene, &self.hygiene,
|path, use_tree, is_glob, alias| { |path, _use_tree, is_glob, alias| {
let import_data = ImportData { let import_data = ImportData {
path, path,
alias, alias,
@ -333,11 +314,11 @@ impl RawItemsCollector {
is_extern_crate: false, is_extern_crate: false,
is_macro_use: false, is_macro_use: false,
}; };
buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); buf.push(import_data);
}, },
); );
for (import_data, ptr) in buf { for import_data in buf {
self.push_import(current_module, attrs.clone(), import_data, ptr); self.push_import(current_module, attrs.clone(), import_data);
} }
} }
@ -360,12 +341,7 @@ impl RawItemsCollector {
is_extern_crate: true, is_extern_crate: true,
is_macro_use, is_macro_use,
}; };
self.push_import( self.push_import(current_module, attrs, import_data);
current_module,
attrs,
import_data,
Either::Right(AstPtr::new(&extern_crate)),
);
} }
} }
@ -396,14 +372,8 @@ impl RawItemsCollector {
self.push_item(current_module, attrs, RawItemKind::Impl(imp)) self.push_item(current_module, attrs, RawItemKind::Impl(imp))
} }
fn push_import( fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) {
&mut self, let import = self.raw_items.imports.alloc(data);
current_module: Option<Module>,
attrs: Attrs,
data: ImportData,
source: ImportSourcePtr,
) {
let import = self.imports.alloc(|| source, || data);
self.push_item(current_module, attrs, RawItemKind::Import(import)) self.push_item(current_module, attrs, RawItemKind::Import(import))
} }

View file

@ -18,10 +18,6 @@ pub(crate) struct Trace<ID: ArenaId, T, V> {
} }
impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> { impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> {
pub(crate) fn new() -> Trace<ID, T, V> {
Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 }
}
pub(crate) fn new_for_arena() -> Trace<ID, T, V> { pub(crate) fn new_for_arena() -> Trace<ID, T, V> {
Trace { arena: Some(Arena::default()), map: None, len: 0 } Trace { arena: Some(Arena::default()), map: None, len: 0 }
} }
@ -52,8 +48,4 @@ impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> {
pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> { pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> {
self.map.take().unwrap() self.map.take().unwrap()
} }
pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) {
(self.arena.take().unwrap(), self.map.take().unwrap())
}
} }

View file

@ -270,7 +270,6 @@ impl RootDatabase {
self.query(hir::db::AstIdMapQuery).sweep(sweep); self.query(hir::db::AstIdMapQuery).sweep(sweep);
self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep);
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
self.query(hir::db::ExprScopesQuery).sweep(sweep); self.query(hir::db::ExprScopesQuery).sweep(sweep);
@ -309,7 +308,6 @@ impl RootDatabase {
hir::db::StructDataQuery hir::db::StructDataQuery
hir::db::EnumDataQuery hir::db::EnumDataQuery
hir::db::TraitDataQuery hir::db::TraitDataQuery
hir::db::RawItemsWithSourceMapQuery
hir::db::RawItemsQuery hir::db::RawItemsQuery
hir::db::CrateDefMapQuery hir::db::CrateDefMapQuery
hir::db::GenericParamsQuery hir::db::GenericParamsQuery