Skip to content

Commit 30f526c

Browse files
committed
Auto merge of #15378 - Veykril:import-use-rename, r=Veykril
Name change Import to Use in hir-def, add unused placeholder variants for UseId cc #14079
2 parents 12cb6e7 + ecb6d07 commit 30f526c

File tree

15 files changed

+71
-43
lines changed

15 files changed

+71
-43
lines changed

crates/hir-def/src/attr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ impl AttrsWithOwner {
485485
},
486486
AttrDefId::ExternBlockId(it) => attrs_from_item_tree_loc(db, it),
487487
AttrDefId::ExternCrateId(it) => attrs_from_item_tree_loc(db, it),
488+
AttrDefId::UseId(it) => attrs_from_item_tree_loc(db, it),
488489
};
489490

490491
let attrs = raw_attrs.filter(db.upcast(), def.krate(db));
@@ -570,6 +571,7 @@ impl AttrsWithOwner {
570571
},
571572
AttrDefId::ExternBlockId(id) => any_has_attrs(db, id),
572573
AttrDefId::ExternCrateId(id) => any_has_attrs(db, id),
574+
AttrDefId::UseId(id) => any_has_attrs(db, id),
573575
};
574576

575577
AttrSourceMap::new(owner.as_ref().map(|node| node as &dyn HasAttrs))

crates/hir-def/src/child_by_source.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
nameres::DefMap,
1616
src::{HasChildSource, HasSource},
1717
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId, ImplId,
18-
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, VariantId,
18+
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId, VariantId,
1919
};
2020

2121
pub trait ChildBySource {
@@ -92,6 +92,7 @@ impl ChildBySource for ItemScope {
9292
self.declarations().for_each(|item| add_module_def(db, res, file_id, item));
9393
self.impls().for_each(|imp| add_impl(db, res, file_id, imp));
9494
self.extern_crate_decls().for_each(|ext| add_extern_crate(db, res, file_id, ext));
95+
self.use_decls().for_each(|ext| add_use(db, res, file_id, ext));
9596
self.unnamed_consts().for_each(|konst| {
9697
let loc = konst.lookup(db);
9798
if loc.id.file_id() == file_id {
@@ -179,6 +180,12 @@ impl ChildBySource for ItemScope {
179180
map[keys::EXTERN_CRATE].insert(loc.source(db).value, ext)
180181
}
181182
}
183+
fn add_use(db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId, ext: UseId) {
184+
let loc = ext.lookup(db);
185+
if loc.id.file_id() == file_id {
186+
map[keys::USE].insert(loc.source(db).value, ext)
187+
}
188+
}
182189
}
183190
}
184191

crates/hir-def/src/db.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ use crate::{
2323
visibility::{self, Visibility},
2424
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
2525
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
26-
FunctionLoc, GenericDefId, ImplId, ImplLoc, ImportId, ImportLoc, InTypeConstId, InTypeConstLoc,
27-
LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc,
28-
ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId,
29-
TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId,
26+
FunctionLoc, GenericDefId, ImplId, ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId,
27+
LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
28+
StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc,
29+
TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
3030
};
3131

3232
#[salsa::query_group(InternDatabaseStorage)]
3333
pub trait InternDatabase: SourceDatabase {
3434
// region: items
3535
#[salsa::interned]
36-
fn intern_import(&self, loc: ImportLoc) -> ImportId;
36+
fn intern_use(&self, loc: UseLoc) -> UseId;
3737
#[salsa::interned]
3838
fn intern_extern_crate(&self, loc: ExternCrateLoc) -> ExternCrateId;
3939
#[salsa::interned]

crates/hir-def/src/dyn_map/keys.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
dyn_map::{DynMap, Policy},
1111
ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId, LifetimeParamId,
1212
Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId, TraitId, TypeAliasId,
13-
TypeOrConstParamId, UnionId,
13+
TypeOrConstParamId, UnionId, UseId,
1414
};
1515

1616
pub type Key<K, V> = crate::dyn_map::Key<K, V, AstPtrPolicy<K, V>>;
@@ -26,6 +26,7 @@ pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
2626
pub const UNION: Key<ast::Union, UnionId> = Key::new();
2727
pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
2828
pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new();
29+
pub const USE: Key<ast::Use, UseId> = Key::new();
2930

3031
pub const VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
3132
pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();

crates/hir-def/src/item_scope.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::ast;
1616
use crate::{
1717
db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ConstId,
1818
ExternCrateId, HasModule, ImplId, LocalModuleId, MacroId, ModuleDefId, ModuleId, TraitId,
19+
UseId,
1920
};
2021

2122
#[derive(Copy, Clone, Debug)]
@@ -119,6 +120,11 @@ impl ItemScope {
119120
self.extern_crate_decls.iter().copied()
120121
}
121122

123+
pub fn use_decls(&self) -> impl Iterator<Item = UseId> + ExactSizeIterator + '_ {
124+
// FIXME: to be implemented
125+
std::iter::empty()
126+
}
127+
122128
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
123129
self.impls.iter().copied()
124130
}

crates/hir-def/src/item_tree.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl ItemTree {
188188
fn shrink_to_fit(&mut self) {
189189
if let Some(data) = &mut self.data {
190190
let ItemTreeData {
191-
imports,
191+
uses,
192192
extern_crates,
193193
extern_blocks,
194194
functions,
@@ -211,7 +211,7 @@ impl ItemTree {
211211
vis,
212212
} = &mut **data;
213213

214-
imports.shrink_to_fit();
214+
uses.shrink_to_fit();
215215
extern_crates.shrink_to_fit();
216216
extern_blocks.shrink_to_fit();
217217
functions.shrink_to_fit();
@@ -262,7 +262,7 @@ static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(P
262262

263263
#[derive(Default, Debug, Eq, PartialEq)]
264264
struct ItemTreeData {
265-
imports: Arena<Import>,
265+
uses: Arena<Use>,
266266
extern_crates: Arena<ExternCrate>,
267267
extern_blocks: Arena<ExternBlock>,
268268
functions: Arena<Function>,
@@ -486,7 +486,7 @@ macro_rules! mod_items {
486486
}
487487

488488
mod_items! {
489-
Import in imports -> ast::Use,
489+
Use in uses -> ast::Use,
490490
ExternCrate in extern_crates -> ast::ExternCrate,
491491
ExternBlock in extern_blocks -> ast::ExternBlock,
492492
Function in functions -> ast::Fn,
@@ -541,7 +541,7 @@ impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
541541
}
542542

543543
#[derive(Debug, Clone, Eq, PartialEq)]
544-
pub struct Import {
544+
pub struct Use {
545545
pub visibility: RawVisibilityId,
546546
pub ast_id: FileAstId<ast::Use>,
547547
pub use_tree: UseTree,
@@ -744,7 +744,7 @@ pub struct MacroDef {
744744
pub ast_id: FileAstId<ast::MacroDef>,
745745
}
746746

747-
impl Import {
747+
impl Use {
748748
/// Maps a `UseTree` contained in this import back to its AST node.
749749
pub fn use_tree_to_ast(
750750
&self,
@@ -870,7 +870,7 @@ macro_rules! impl_froms {
870870
impl ModItem {
871871
pub fn as_assoc_item(&self) -> Option<AssocItem> {
872872
match self {
873-
ModItem::Import(_)
873+
ModItem::Use(_)
874874
| ModItem::ExternCrate(_)
875875
| ModItem::ExternBlock(_)
876876
| ModItem::Struct(_)
@@ -892,7 +892,7 @@ impl ModItem {
892892

893893
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
894894
match self {
895-
ModItem::Import(it) => tree[it.index].ast_id().upcast(),
895+
ModItem::Use(it) => tree[it.index].ast_id().upcast(),
896896
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
897897
ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
898898
ModItem::Function(it) => tree[it.index].ast_id().upcast(),

crates/hir-def/src/item_tree/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,13 @@ impl<'a> Ctx<'a> {
502502
Some(id(self.data().impls.alloc(res)))
503503
}
504504

505-
fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Import>> {
505+
fn lower_use(&mut self, use_item: &ast::Use) -> Option<FileItemTreeId<Use>> {
506506
let visibility = self.lower_visibility(use_item);
507507
let ast_id = self.source_ast_id_map.ast_id(use_item);
508508
let (use_tree, _) = lower_use_tree(self.db, self.hygiene(), use_item.use_tree()?)?;
509509

510-
let res = Import { visibility, ast_id, use_tree };
511-
Some(id(self.data().imports.alloc(res)))
510+
let res = Use { visibility, ast_id, use_tree };
511+
Some(id(self.data().uses.alloc(res)))
512512
}
513513

514514
fn lower_extern_crate(

crates/hir-def/src/item_tree/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ impl Printer<'_> {
198198
self.print_attrs_of(item);
199199

200200
match item {
201-
ModItem::Import(it) => {
202-
let Import { visibility, use_tree, ast_id: _ } = &self.tree[it];
201+
ModItem::Use(it) => {
202+
let Use { visibility, use_tree, ast_id: _ } = &self.tree[it];
203203
self.print_visibility(*visibility);
204204
w!(self, "use ");
205205
self.print_use_tree(use_tree);

crates/hir-def/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ use crate::{
8888
builtin_type::BuiltinType,
8989
data::adt::VariantData,
9090
item_tree::{
91-
Const, Enum, ExternCrate, Function, Impl, Import, ItemTreeId, ItemTreeNode, MacroDef,
92-
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union,
91+
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, MacroDef, MacroRules,
92+
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use,
9393
},
9494
};
9595

@@ -324,9 +324,9 @@ type ImplLoc = ItemLoc<Impl>;
324324
impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
325325

326326
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
327-
pub struct ImportId(salsa::InternId);
328-
type ImportLoc = ItemLoc<Import>;
329-
impl_intern!(ImportId, ImportLoc, intern_import, lookup_intern_import);
327+
pub struct UseId(salsa::InternId);
328+
type UseLoc = ItemLoc<Use>;
329+
impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use);
330330

331331
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
332332
pub struct ExternCrateId(salsa::InternId);
@@ -842,6 +842,7 @@ pub enum AttrDefId {
842842
GenericParamId(GenericParamId),
843843
ExternBlockId(ExternBlockId),
844844
ExternCrateId(ExternCrateId),
845+
UseId(UseId),
845846
}
846847

847848
impl_from!(
@@ -1079,6 +1080,7 @@ impl AttrDefId {
10791080
}
10801081
AttrDefId::MacroId(it) => it.module(db).krate,
10811082
AttrDefId::ExternCrateId(it) => it.lookup(db).container.krate,
1083+
AttrDefId::UseId(it) => it.lookup(db).container.krate,
10821084
}
10831085
}
10841086
}

crates/hir-def/src/nameres/collector.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ use crate::{
5252
tt,
5353
visibility::{RawVisibility, Visibility},
5454
AdtId, AstId, AstIdWithPath, ConstLoc, CrateRootModuleId, EnumLoc, EnumVariantId,
55-
ExternBlockLoc, ExternCrateLoc, FunctionId, FunctionLoc, ImplLoc, ImportLoc, Intern,
56-
ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId,
57-
MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc, StructLoc,
58-
TraitAliasLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro,
55+
ExternBlockLoc, ExternCrateLoc, FunctionId, FunctionLoc, ImplLoc, Intern, ItemContainerId,
56+
LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId, MacroRulesLoc,
57+
ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc, StructLoc, TraitAliasLoc,
58+
TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro, UseLoc,
5959
};
6060

6161
static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
@@ -146,7 +146,7 @@ impl PartialResolvedImport {
146146

147147
#[derive(Clone, Debug, Eq, PartialEq)]
148148
enum ImportSource {
149-
Import { id: ItemTreeId<item_tree::Import>, use_tree: Idx<ast::UseTree> },
149+
Use { id: ItemTreeId<item_tree::Use>, use_tree: Idx<ast::UseTree> },
150150
ExternCrate(ItemTreeId<item_tree::ExternCrate>),
151151
}
152152

@@ -166,7 +166,7 @@ impl Import {
166166
db: &dyn DefDatabase,
167167
krate: CrateId,
168168
tree: &ItemTree,
169-
id: ItemTreeId<item_tree::Import>,
169+
id: ItemTreeId<item_tree::Use>,
170170
mut cb: impl FnMut(Self),
171171
) {
172172
let it = &tree[id.value];
@@ -181,7 +181,7 @@ impl Import {
181181
kind,
182182
is_prelude,
183183
is_macro_use: false,
184-
source: ImportSource::Import { id, use_tree: idx },
184+
source: ImportSource::Use { id, use_tree: idx },
185185
});
186186
});
187187
}
@@ -1474,7 +1474,7 @@ impl DefCollector<'_> {
14741474
}
14751475

14761476
for directive in &self.unresolved_imports {
1477-
if let ImportSource::Import { id: import, use_tree } = directive.import.source {
1477+
if let ImportSource::Use { id: import, use_tree } = directive.import.source {
14781478
if matches!(
14791479
(directive.import.path.segments().first(), &directive.import.path.kind),
14801480
(Some(krate), PathKind::Plain | PathKind::Abs) if diagnosed_extern_crates.contains(krate)
@@ -1576,12 +1576,10 @@ impl ModCollector<'_, '_> {
15761576

15771577
match item {
15781578
ModItem::Mod(m) => self.collect_module(m, &attrs),
1579-
ModItem::Import(import_id) => {
1580-
let _import_id = ImportLoc {
1581-
container: module,
1582-
id: ItemTreeId::new(self.tree_id, import_id),
1583-
}
1584-
.intern(db);
1579+
ModItem::Use(import_id) => {
1580+
let _import_id =
1581+
UseLoc { container: module, id: ItemTreeId::new(self.tree_id, import_id) }
1582+
.intern(db);
15851583
Import::from_use(
15861584
db,
15871585
krate,

crates/hir-def/src/nameres/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum DefDiagnosticKind {
1919

2020
UnresolvedExternCrate { ast: AstId<ast::ExternCrate> },
2121

22-
UnresolvedImport { id: ItemTreeId<item_tree::Import>, index: Idx<ast::UseTree> },
22+
UnresolvedImport { id: ItemTreeId<item_tree::Use>, index: Idx<ast::UseTree> },
2323

2424
UnconfiguredCode { ast: ErasedAstId, cfg: CfgExpr, opts: CfgOptions },
2525

@@ -70,7 +70,7 @@ impl DefDiagnostic {
7070

7171
pub(super) fn unresolved_import(
7272
container: LocalModuleId,
73-
id: ItemTreeId<item_tree::Import>,
73+
id: ItemTreeId<item_tree::Use>,
7474
index: Idx<ast::UseTree>,
7575
) -> Self {
7676
Self { in_module: container, kind: DefDiagnosticKind::UnresolvedImport { id, index } }

crates/hir-def/src/resolver.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
EnumVariantId, ExternBlockId, ExternCrateId, FunctionId, GenericDefId, GenericParamId,
2626
HasModule, ImplId, ItemContainerId, LifetimeParamId, LocalModuleId, Lookup, Macro2Id, MacroId,
2727
MacroRulesId, ModuleDefId, ModuleId, ProcMacroId, StaticId, StructId, TraitAliasId, TraitId,
28-
TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, VariantId,
28+
TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, UseId, VariantId,
2929
};
3030

3131
#[derive(Debug, Clone)]
@@ -1024,6 +1024,12 @@ impl HasResolver for ExternCrateId {
10241024
}
10251025
}
10261026

1027+
impl HasResolver for UseId {
1028+
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
1029+
self.lookup(db).container.resolver(db)
1030+
}
1031+
}
1032+
10271033
impl HasResolver for TypeOwnerId {
10281034
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
10291035
match self {

crates/hir-ty/src/diagnostics/decl_check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ impl<'a> DeclValidator<'a> {
176176
AttrDefId::ImplId(iid) => Some(iid.lookup(self.db.upcast()).container.into()),
177177
AttrDefId::ExternBlockId(id) => Some(id.lookup(self.db.upcast()).container.into()),
178178
AttrDefId::ExternCrateId(id) => Some(id.lookup(self.db.upcast()).container.into()),
179+
AttrDefId::UseId(id) => Some(id.lookup(self.db.upcast()).container.into()),
179180
// These warnings should not explore macro definitions at all
180181
AttrDefId::MacroId(_) => None,
181182
AttrDefId::AdtId(aid) => match aid {

crates/hir/src/attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ fn resolve_doc_path(
173173
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
174174
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
175175
AttrDefId::ExternBlockId(it) => it.resolver(db.upcast()),
176+
AttrDefId::UseId(it) => it.resolver(db.upcast()),
176177
AttrDefId::MacroId(it) => it.resolver(db.upcast()),
177178
AttrDefId::ExternCrateId(it) => it.resolver(db.upcast()),
178179
AttrDefId::GenericParamId(it) => match it {

crates/hir/src/semantics/source_to_def.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use hir_def::{
9595
hir::{BindingId, LabelId},
9696
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId,
9797
FunctionId, GenericDefId, GenericParamId, ImplId, LifetimeParamId, MacroId, ModuleId, StaticId,
98-
StructId, TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
98+
StructId, TraitAliasId, TraitId, TypeAliasId, TypeParamId, UnionId, UseId, VariantId,
9999
};
100100
use hir_expand::{attrs::AttrId, name::AsName, HirFileId, MacroCallId};
101101
use rustc_hash::FxHashMap;
@@ -209,6 +209,10 @@ impl SourceToDefCtx<'_, '_> {
209209
) -> Option<ExternCrateId> {
210210
self.to_def(src, keys::EXTERN_CRATE)
211211
}
212+
#[allow(dead_code)]
213+
pub(super) fn use_to_def(&mut self, src: InFile<ast::Use>) -> Option<UseId> {
214+
self.to_def(src, keys::USE)
215+
}
212216
pub(super) fn adt_to_def(
213217
&mut self,
214218
InFile { file_id, value }: InFile<ast::Adt>,

0 commit comments

Comments
 (0)