Change terminology, do not name use items and use trees as imports

This commit is contained in:
Lukas Wirth 2023-08-02 14:19:38 +02:00
parent 12cb6e7327
commit f86f6a89eb
7 changed files with 37 additions and 39 deletions

View File

@ -23,17 +23,17 @@
visibility::{self, Visibility}, visibility::{self, Visibility},
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId, AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId, EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
FunctionLoc, GenericDefId, ImplId, ImplLoc, ImportId, ImportLoc, InTypeConstId, InTypeConstLoc, FunctionLoc, GenericDefId, ImplId, ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId,
LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc,
TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
}; };
#[salsa::query_group(InternDatabaseStorage)] #[salsa::query_group(InternDatabaseStorage)]
pub trait InternDatabase: SourceDatabase { pub trait InternDatabase: SourceDatabase {
// region: items // region: items
#[salsa::interned] #[salsa::interned]
fn intern_import(&self, loc: ImportLoc) -> ImportId; fn intern_use(&self, loc: UseLoc) -> UseId;
#[salsa::interned] #[salsa::interned]
fn intern_extern_crate(&self, loc: ExternCrateLoc) -> ExternCrateId; fn intern_extern_crate(&self, loc: ExternCrateLoc) -> ExternCrateId;
#[salsa::interned] #[salsa::interned]

View File

@ -188,7 +188,7 @@ fn block_item_tree(db: &dyn DefDatabase, block: BlockId) -> Arc<ItemTree> {
fn shrink_to_fit(&mut self) { fn shrink_to_fit(&mut self) {
if let Some(data) = &mut self.data { if let Some(data) = &mut self.data {
let ItemTreeData { let ItemTreeData {
imports, uses,
extern_crates, extern_crates,
extern_blocks, extern_blocks,
functions, functions,
@ -211,7 +211,7 @@ fn shrink_to_fit(&mut self) {
vis, vis,
} = &mut **data; } = &mut **data;
imports.shrink_to_fit(); uses.shrink_to_fit();
extern_crates.shrink_to_fit(); extern_crates.shrink_to_fit();
extern_blocks.shrink_to_fit(); extern_blocks.shrink_to_fit();
functions.shrink_to_fit(); functions.shrink_to_fit();
@ -262,7 +262,7 @@ fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Default, Debug, Eq, PartialEq)]
struct ItemTreeData { struct ItemTreeData {
imports: Arena<Import>, uses: Arena<Use>,
extern_crates: Arena<ExternCrate>, extern_crates: Arena<ExternCrate>,
extern_blocks: Arena<ExternBlock>, extern_blocks: Arena<ExternBlock>,
functions: Arena<Function>, functions: Arena<Function>,
@ -486,7 +486,7 @@ fn index(&self, index: Idx<$typ>) -> &Self::Output {
} }
mod_items! { mod_items! {
Import in imports -> ast::Use, Use in uses -> ast::Use,
ExternCrate in extern_crates -> ast::ExternCrate, ExternCrate in extern_crates -> ast::ExternCrate,
ExternBlock in extern_blocks -> ast::ExternBlock, ExternBlock in extern_blocks -> ast::ExternBlock,
Function in functions -> ast::Fn, Function in functions -> ast::Fn,
@ -541,7 +541,7 @@ fn index(&self, id: FileItemTreeId<N>) -> &N {
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct Import { pub struct Use {
pub visibility: RawVisibilityId, pub visibility: RawVisibilityId,
pub ast_id: FileAstId<ast::Use>, pub ast_id: FileAstId<ast::Use>,
pub use_tree: UseTree, pub use_tree: UseTree,
@ -744,7 +744,7 @@ pub struct MacroDef {
pub ast_id: FileAstId<ast::MacroDef>, pub ast_id: FileAstId<ast::MacroDef>,
} }
impl Import { impl Use {
/// Maps a `UseTree` contained in this import back to its AST node. /// Maps a `UseTree` contained in this import back to its AST node.
pub fn use_tree_to_ast( pub fn use_tree_to_ast(
&self, &self,
@ -870,7 +870,7 @@ fn from(it: $t) -> $e {
impl ModItem { impl ModItem {
pub fn as_assoc_item(&self) -> Option<AssocItem> { pub fn as_assoc_item(&self) -> Option<AssocItem> {
match self { match self {
ModItem::Import(_) ModItem::Use(_)
| ModItem::ExternCrate(_) | ModItem::ExternCrate(_)
| ModItem::ExternBlock(_) | ModItem::ExternBlock(_)
| ModItem::Struct(_) | ModItem::Struct(_)
@ -892,7 +892,7 @@ pub fn as_assoc_item(&self) -> Option<AssocItem> {
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> { pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
match self { match self {
ModItem::Import(it) => tree[it.index].ast_id().upcast(), ModItem::Use(it) => tree[it.index].ast_id().upcast(),
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(), ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(), ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
ModItem::Function(it) => tree[it.index].ast_id().upcast(), ModItem::Function(it) => tree[it.index].ast_id().upcast(),

View File

@ -502,13 +502,13 @@ fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
Some(id(self.data().impls.alloc(res))) Some(id(self.data().impls.alloc(res)))
} }
fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>> { fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Use>> {
let visibility = self.lower_visibility(use_item); let visibility = self.lower_visibility(use_item);
let ast_id = self.source_ast_id_map.ast_id(use_item); let ast_id = self.source_ast_id_map.ast_id(use_item);
let (use_tree, _) = lower_use_tree(self.db, self.hygiene(), use_item.use_tree()?)?; let (use_tree, _) = lower_use_tree(self.db, self.hygiene(), use_item.use_tree()?)?;
let res = Import { visibility, ast_id, use_tree }; let res = Use { visibility, ast_id, use_tree };
Some(id(self.data().imports.alloc(res))) Some(id(self.data().uses.alloc(res)))
} }
fn lower_extern_crate( fn lower_extern_crate(

View File

@ -198,8 +198,8 @@ fn print_mod_item(&mut self, item: ModItem) {
self.print_attrs_of(item); self.print_attrs_of(item);
match item { match item {
ModItem::Import(it) => { ModItem::Use(it) => {
let Import { visibility, use_tree, ast_id: _ } = &self.tree[it]; let Use { visibility, use_tree, ast_id: _ } = &self.tree[it];
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "use "); w!(self, "use ");
self.print_use_tree(use_tree); self.print_use_tree(use_tree);

View File

@ -88,8 +88,8 @@ macro_rules! eprintln {
builtin_type::BuiltinType, builtin_type::BuiltinType,
data::adt::VariantData, data::adt::VariantData,
item_tree::{ item_tree::{
Const, Enum, ExternCrate, Function, Impl, Import, ItemTreeId, ItemTreeNode, MacroDef, Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, MacroDef, MacroRules,
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use,
}, },
}; };
@ -324,9 +324,9 @@ pub struct FieldId {
impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ImportId(salsa::InternId); pub struct UseId(salsa::InternId);
type ImportLoc = ItemLoc<Import>; type UseLoc = ItemLoc<Use>;
impl_intern!(ImportId, ImportLoc, intern_import, lookup_intern_import); impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ExternCrateId(salsa::InternId); pub struct ExternCrateId(salsa::InternId);

View File

@ -52,10 +52,10 @@
tt, tt,
visibility::{RawVisibility, Visibility}, visibility::{RawVisibility, Visibility},
AdtId, AstId, AstIdWithPath, ConstLoc, CrateRootModuleId, EnumLoc, EnumVariantId, AdtId, AstId, AstIdWithPath, ConstLoc, CrateRootModuleId, EnumLoc, EnumVariantId,
ExternBlockLoc, ExternCrateLoc, FunctionId, FunctionLoc, ImplLoc, ImportLoc, Intern, ExternBlockLoc, ExternCrateLoc, FunctionId, FunctionLoc, ImplLoc, Intern, ItemContainerId,
ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId, LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId, MacroRulesLoc,
MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc, StructLoc, ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc, StructLoc, TraitAliasLoc,
TraitAliasLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro, UseLoc,
}; };
static GLOB_RECURSION_LIMIT: Limit = Limit::new(100); static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
@ -146,7 +146,7 @@ fn namespaces(self) -> PerNs {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
enum ImportSource { enum ImportSource {
Import { id: ItemTreeId<item_tree::Import>, use_tree: Idx<ast::UseTree> }, Use { id: ItemTreeId<item_tree::Use>, use_tree: Idx<ast::UseTree> },
ExternCrate(ItemTreeId<item_tree::ExternCrate>), ExternCrate(ItemTreeId<item_tree::ExternCrate>),
} }
@ -166,7 +166,7 @@ fn from_use(
db: &dyn DefDatabase, db: &dyn DefDatabase,
krate: CrateId, krate: CrateId,
tree: &ItemTree, tree: &ItemTree,
id: ItemTreeId<item_tree::Import>, id: ItemTreeId<item_tree::Use>,
mut cb: impl FnMut(Self), mut cb: impl FnMut(Self),
) { ) {
let it = &tree[id.value]; let it = &tree[id.value];
@ -181,7 +181,7 @@ fn from_use(
kind, kind,
is_prelude, is_prelude,
is_macro_use: false, is_macro_use: false,
source: ImportSource::Import { id, use_tree: idx }, source: ImportSource::Use { id, use_tree: idx },
}); });
}); });
} }
@ -1474,7 +1474,7 @@ fn finish(mut self) -> DefMap {
} }
for directive in &self.unresolved_imports { for directive in &self.unresolved_imports {
if let ImportSource::Import { id: import, use_tree } = directive.import.source { if let ImportSource::Use { id: import, use_tree } = directive.import.source {
if matches!( if matches!(
(directive.import.path.segments().first(), &directive.import.path.kind), (directive.import.path.segments().first(), &directive.import.path.kind),
(Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate) (Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate)
@ -1576,12 +1576,10 @@ fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
match item { match item {
ModItem::Mod(m) => self.collect_module(m, &attrs), ModItem::Mod(m) => self.collect_module(m, &attrs),
ModItem::Import(import_id) => { ModItem::Use(import_id) => {
let _import_id = ImportLoc { let _import_id =
container: module, UseLoc { container: module, id: ItemTreeId::new(self.tree_id, import_id) }
id: ItemTreeId::new(self.tree_id, import_id), .intern(db);
}
.intern(db);
Import::from_use( Import::from_use(
db, db,
krate, krate,

View File

@ -19,7 +19,7 @@ pub enum DefDiagnosticKind {
UnresolvedExternCrate { ast: AstId<ast::ExternCrate> }, UnresolvedExternCrate { ast: AstId<ast::ExternCrate> },
UnresolvedImport { id: ItemTreeId<item_tree::Import>, index: Idx<ast::UseTree> }, UnresolvedImport { id: ItemTreeId<item_tree::Use>, index: Idx<ast::UseTree> },
UnconfiguredCode { ast: ErasedAstId, cfg: CfgExpr, opts: CfgOptions }, UnconfiguredCode { ast: ErasedAstId, cfg: CfgExpr, opts: CfgOptions },
@ -70,7 +70,7 @@ pub(super) fn unresolved_extern_crate(
pub(super) fn unresolved_import( pub(super) fn unresolved_import(
container: LocalModuleId, container: LocalModuleId,
id: ItemTreeId<item_tree::Import>, id: ItemTreeId<item_tree::Use>,
index: Idx<ast::UseTree>, index: Idx<ast::UseTree>,
) -> Self { ) -> Self {
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } } Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }