Skip to content

Commit 84f2509

Browse files
bors[bot]matklad
andcommitted
Merge #254
254: Defids r=matklad a=matklad Fleshing out DefIds some more Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 244f9a1 + 54d053c commit 84f2509

File tree

12 files changed

+186
-160
lines changed

12 files changed

+186
-160
lines changed

crates/ra_analysis/src/completion/reference_completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn complete_path(
163163
};
164164
let target_module = match def_id.resolve(db)? {
165165
Def::Module(it) => it,
166-
Def::Item => return Ok(()),
166+
_ => return Ok(()),
167167
};
168168
let module_scope = target_module.scope(db)?;
169169
let completions = module_scope.entries().map(|(name, _res)| CompletionItem {

crates/ra_analysis/src/db.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22
use salsa::{self, Database};
33
use ra_db::{LocationIntener, BaseDatabase};
4-
use hir::{self, DefId, DefLoc, FnId, SourceItemId};
4+
use hir::{self, DefId, DefLoc};
55

66
use crate::{
77
symbol_index,
@@ -15,7 +15,6 @@ pub(crate) struct RootDatabase {
1515

1616
#[derive(Debug, Default)]
1717
struct IdMaps {
18-
fns: LocationIntener<SourceItemId, FnId>,
1918
defs: LocationIntener<DefLoc, DefId>,
2019
}
2120

@@ -58,12 +57,6 @@ impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase {
5857
}
5958
}
6059

61-
impl AsRef<LocationIntener<hir::SourceItemId, FnId>> for RootDatabase {
62-
fn as_ref(&self) -> &LocationIntener<hir::SourceItemId, FnId> {
63-
&self.id_maps.fns
64-
}
65-
}
66-
6760
salsa::database_storage! {
6861
pub(crate) struct RootDatabaseStorage for RootDatabase {
6962
impl ra_db::FilesDatabase {

crates/ra_analysis/src/imp.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ impl AnalysisImpl {
190190
Some(it) => it,
191191
};
192192
let root = descr.crate_root();
193-
let file_id = root
194-
.source()
195-
.as_file()
196-
.expect("root module always has a file as a source");
193+
let file_id = root.source().file_id();
197194

198195
let crate_graph = self.db.crate_graph();
199196
let crate_id = crate_graph.crate_id_for_crate_root(file_id);
@@ -213,7 +210,7 @@ impl AnalysisImpl {
213210
let syntax = file.syntax();
214211
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
215212
if let Some(fn_descr) =
216-
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)
213+
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)?
217214
{
218215
let scope = fn_descr.scope(&*self.db);
219216
// First try to resolve the symbol locally
@@ -260,11 +257,11 @@ impl AnalysisImpl {
260257
Ok(vec![])
261258
}
262259

263-
pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
260+
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
264261
let file = self.db.source_file(position.file_id);
265262
// Find the binding associated with the offset
266-
let (binding, descr) = match find_binding(&self.db, &file, position) {
267-
None => return Vec::new(),
263+
let (binding, descr) = match find_binding(&self.db, &file, position)? {
264+
None => return Ok(Vec::new()),
268265
Some(it) => it,
269266
};
270267

@@ -277,25 +274,36 @@ impl AnalysisImpl {
277274
.map(|ref_desc| (position.file_id, ref_desc.range)),
278275
);
279276

280-
return ret;
277+
return Ok(ret);
281278

282279
fn find_binding<'a>(
283280
db: &db::RootDatabase,
284281
source_file: &'a SourceFileNode,
285282
position: FilePosition,
286-
) -> Option<(ast::BindPat<'a>, hir::Function)> {
283+
) -> Cancelable<Option<(ast::BindPat<'a>, hir::Function)>> {
287284
let syntax = source_file.syntax();
288285
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
289-
let descr = hir::Function::guess_for_bind_pat(db, position.file_id, binding)?;
290-
return Some((binding, descr));
286+
let descr = ctry!(hir::Function::guess_for_bind_pat(
287+
db,
288+
position.file_id,
289+
binding
290+
)?);
291+
return Ok(Some((binding, descr)));
291292
};
292-
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
293-
let descr = hir::Function::guess_for_name_ref(db, position.file_id, name_ref)?;
293+
let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset));
294+
let descr = ctry!(hir::Function::guess_for_name_ref(
295+
db,
296+
position.file_id,
297+
name_ref
298+
)?);
294299
let scope = descr.scope(db);
295-
let resolved = scope.resolve_local_name(name_ref)?;
300+
let resolved = ctry!(scope.resolve_local_name(name_ref));
296301
let resolved = resolved.ptr().resolve(source_file);
297-
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
298-
Some((binding, descr))
302+
let binding = ctry!(find_node_at_offset::<ast::BindPat>(
303+
syntax,
304+
resolved.range().end()
305+
));
306+
Ok(Some((binding, descr)))
299307
}
300308
}
301309

@@ -411,7 +419,9 @@ impl AnalysisImpl {
411419
if fs.kind == FN_DEF {
412420
let fn_file = self.db.source_file(fn_file_id);
413421
if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
414-
let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def);
422+
let descr = ctry!(hir::Function::guess_from_source(
423+
&*self.db, fn_file_id, fn_def
424+
)?);
415425
if let Some(descriptor) = descr.signature_info(&*self.db) {
416426
// If we have a calling expression let's find which argument we are on
417427
let mut current_parameter = None;

crates/ra_analysis/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Analysis {
248248
self.imp.approximately_resolve_symbol(position)
249249
}
250250
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
251-
Ok(self.imp.find_all_refs(position))
251+
self.imp.find_all_refs(position)
252252
}
253253
pub fn doc_comment_for(
254254
&self,

crates/ra_hir/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use ra_syntax::{
77
use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable};
88

99
use crate::{
10-
DefLoc, DefId, FnId,
10+
DefLoc, DefId,
1111
SourceFileItems, SourceItemId,
1212
query_definitions,
1313
FnScopes,
14+
function::FnId,
1415
module::{ModuleId, ModuleTree, ModuleSource,
1516
nameres::{ItemMap, InputModuleItems}},
1617
};
@@ -19,7 +20,6 @@ salsa::query_group! {
1920

2021
pub trait HirDatabase: SyntaxDatabase
2122
+ AsRef<LocationIntener<DefLoc, DefId>>
22-
+ AsRef<LocationIntener<SourceItemId, FnId>>
2323
{
2424
fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> {
2525
type FnScopesQuery;

crates/ra_hir/src/function/mod.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,67 @@ use ra_syntax::{
1212
use ra_db::FileId;
1313

1414
use crate::{
15-
FnId, HirDatabase, SourceItemId,
15+
Cancelable,
16+
DefLoc, DefKind, DefId, HirDatabase, SourceItemId,
17+
Module,
1618
};
1719

1820
pub use self::scope::FnScopes;
1921

20-
impl FnId {
21-
pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
22-
let file_items = db.file_items(file_id);
23-
let item_id = file_items.id_of(fn_def.syntax());
24-
let item_id = SourceItemId { file_id, item_id };
25-
FnId::from_loc(db, &item_id)
26-
}
27-
}
22+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23+
pub struct FnId(pub(crate) DefId);
2824

2925
pub struct Function {
3026
fn_id: FnId,
3127
}
3228

3329
impl Function {
30+
pub(crate) fn new(def_id: DefId) -> Function {
31+
let fn_id = FnId(def_id);
32+
Function { fn_id }
33+
}
34+
3435
pub fn guess_from_source(
3536
db: &impl HirDatabase,
3637
file_id: FileId,
3738
fn_def: ast::FnDef,
38-
) -> Function {
39-
let fn_id = FnId::get(db, file_id, fn_def);
40-
Function { fn_id }
39+
) -> Cancelable<Option<Function>> {
40+
let module = ctry!(Module::guess_from_child_node(db, file_id, fn_def.syntax())?);
41+
let file_items = db.file_items(file_id);
42+
let item_id = file_items.id_of(fn_def.syntax());
43+
let source_item_id = SourceItemId { file_id, item_id };
44+
let def_loc = DefLoc {
45+
kind: DefKind::Function,
46+
source_root_id: module.source_root_id,
47+
module_id: module.module_id,
48+
source_item_id,
49+
};
50+
Ok(Some(Function::new(def_loc.id(db))))
4151
}
4252

4353
pub fn guess_for_name_ref(
4454
db: &impl HirDatabase,
4555
file_id: FileId,
4656
name_ref: ast::NameRef,
47-
) -> Option<Function> {
57+
) -> Cancelable<Option<Function>> {
4858
Function::guess_for_node(db, file_id, name_ref.syntax())
4959
}
5060

5161
pub fn guess_for_bind_pat(
5262
db: &impl HirDatabase,
5363
file_id: FileId,
5464
bind_pat: ast::BindPat,
55-
) -> Option<Function> {
65+
) -> Cancelable<Option<Function>> {
5666
Function::guess_for_node(db, file_id, bind_pat.syntax())
5767
}
5868

5969
fn guess_for_node(
6070
db: &impl HirDatabase,
6171
file_id: FileId,
6272
node: SyntaxNodeRef,
63-
) -> Option<Function> {
64-
let fn_def = node.ancestors().find_map(ast::FnDef::cast)?;
65-
let res = Function::guess_from_source(db, file_id, fn_def);
66-
Some(res)
73+
) -> Cancelable<Option<Function>> {
74+
let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast));
75+
Function::guess_from_source(db, file_id, fn_def)
6776
}
6877

6978
pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {

crates/ra_hir/src/lib.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,63 +41,58 @@ pub use self::{
4141

4242
pub use self::function::FnSignatureInfo;
4343

44-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45-
pub struct FnId(u32);
46-
ra_db::impl_numeric_id!(FnId);
47-
48-
impl FnId {
49-
pub fn from_loc(
50-
db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
51-
loc: &SourceItemId,
52-
) -> FnId {
53-
db.as_ref().loc2id(loc)
54-
}
55-
pub fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
56-
db.as_ref().id2loc(self)
57-
}
58-
}
59-
44+
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
45+
/// in a specific module.
6046
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6147
pub struct DefId(u32);
6248
ra_db::impl_numeric_id!(DefId);
6349

50+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
51+
pub(crate) enum DefKind {
52+
Module,
53+
Function,
54+
Item,
55+
}
56+
6457
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
65-
pub enum DefLoc {
66-
Module {
67-
id: ModuleId,
68-
source_root: SourceRootId,
69-
},
70-
Item {
71-
source_item_id: SourceItemId,
72-
},
58+
pub struct DefLoc {
59+
pub(crate) kind: DefKind,
60+
source_root_id: SourceRootId,
61+
module_id: ModuleId,
62+
source_item_id: SourceItemId,
7363
}
7464

7565
impl DefId {
76-
pub fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
66+
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
7767
db.as_ref().id2loc(self)
7868
}
7969
}
8070

8171
impl DefLoc {
82-
pub fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
72+
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
8373
db.as_ref().loc2id(&self)
8474
}
8575
}
8676

8777
pub enum Def {
8878
Module(Module),
79+
Function(Function),
8980
Item,
9081
}
9182

9283
impl DefId {
9384
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
9485
let loc = self.loc(db);
95-
let res = match loc {
96-
DefLoc::Module { id, source_root } => {
97-
let descr = Module::new(db, source_root, id)?;
98-
Def::Module(descr)
86+
let res = match loc.kind {
87+
DefKind::Module => {
88+
let module = Module::new(db, loc.source_root_id, loc.module_id)?;
89+
Def::Module(module)
9990
}
100-
DefLoc::Item { .. } => Def::Item,
91+
DefKind::Function => {
92+
let function = Function::new(self);
93+
Def::Function(function)
94+
}
95+
DefKind::Item => Def::Item,
10196
};
10297
Ok(res)
10398
}
@@ -131,6 +126,10 @@ impl SourceFileItems {
131126
.unwrap();
132127
id
133128
}
129+
pub fn id_of_source_file(&self) -> SourceFileItemId {
130+
let (id, _syntax) = self.arena.iter().next().unwrap();
131+
id
132+
}
134133
}
135134

136135
impl Index<SourceFileItemId> for SourceFileItems {

crates/ra_hir/src/mock.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ra_db::{LocationIntener, BaseDatabase, FilePosition, mock::FileMap, FileId,
66
use relative_path::RelativePathBuf;
77
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
88

9-
use crate::{db, DefId, DefLoc, FnId, SourceItemId};
9+
use crate::{db, DefId, DefLoc};
1010

1111
#[derive(Debug)]
1212
pub(crate) struct MockDatabase {
@@ -65,7 +65,6 @@ impl MockDatabase {
6565

6666
#[derive(Debug, Default)]
6767
struct IdMaps {
68-
fns: LocationIntener<SourceItemId, FnId>,
6968
defs: LocationIntener<DefLoc, DefId>,
7069
}
7170

@@ -117,12 +116,6 @@ impl AsRef<LocationIntener<DefLoc, DefId>> for MockDatabase {
117116
}
118117
}
119118

120-
impl AsRef<LocationIntener<SourceItemId, FnId>> for MockDatabase {
121-
fn as_ref(&self) -> &LocationIntener<SourceItemId, FnId> {
122-
&self.id_maps.fns
123-
}
124-
}
125-
126119
impl MockDatabase {
127120
pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
128121
*self.events.lock() = Some(Vec::new());

0 commit comments

Comments
 (0)