aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-22 19:12:23 +0000
committerAleksey Kladov <[email protected]>2019-12-22 21:41:21 +0000
commite424545c0f5cbaf135c52764169ea20df7d07d35 (patch)
tree86f6d29653ba7e548f2a321b2ecdb20e7dc366f4 /crates/ra_hir_def
parent7c405c01567e814f18ebf8bdc6aa1f8f694919f0 (diff)
Rudimentary name resolution for local items
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs32
-rw-r--r--crates/ra_hir_def/src/item_scope.rs8
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs2
-rw-r--r--crates/ra_hir_def/src/resolver.rs103
4 files changed, 102 insertions, 43 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 754924050..5323af097 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -494,45 +494,57 @@ where
494 fn collect_block_items(&mut self, block: &ast::Block) { 494 fn collect_block_items(&mut self, block: &ast::Block) {
495 let container = ContainerId::DefWithBodyId(self.def); 495 let container = ContainerId::DefWithBodyId(self.def);
496 for item in block.items() { 496 for item in block.items() {
497 let def: ModuleDefId = match item { 497 let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
498 ast::ModuleItem::FnDef(def) => { 498 ast::ModuleItem::FnDef(def) => {
499 let ast_id = self.expander.ast_id(&def); 499 let ast_id = self.expander.ast_id(&def);
500 FunctionLoc { container: container.into(), ast_id }.intern(self.db).into() 500 (
501 FunctionLoc { container: container.into(), ast_id }.intern(self.db).into(),
502 def.name(),
503 )
501 } 504 }
502 ast::ModuleItem::TypeAliasDef(def) => { 505 ast::ModuleItem::TypeAliasDef(def) => {
503 let ast_id = self.expander.ast_id(&def); 506 let ast_id = self.expander.ast_id(&def);
504 TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into() 507 (
508 TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into(),
509 def.name(),
510 )
505 } 511 }
506 ast::ModuleItem::ConstDef(def) => { 512 ast::ModuleItem::ConstDef(def) => {
507 let ast_id = self.expander.ast_id(&def); 513 let ast_id = self.expander.ast_id(&def);
508 ConstLoc { container: container.into(), ast_id }.intern(self.db).into() 514 (
515 ConstLoc { container: container.into(), ast_id }.intern(self.db).into(),
516 def.name(),
517 )
509 } 518 }
510 ast::ModuleItem::StaticDef(def) => { 519 ast::ModuleItem::StaticDef(def) => {
511 let ast_id = self.expander.ast_id(&def); 520 let ast_id = self.expander.ast_id(&def);
512 StaticLoc { container, ast_id }.intern(self.db).into() 521 (StaticLoc { container, ast_id }.intern(self.db).into(), def.name())
513 } 522 }
514 ast::ModuleItem::StructDef(def) => { 523 ast::ModuleItem::StructDef(def) => {
515 let ast_id = self.expander.ast_id(&def); 524 let ast_id = self.expander.ast_id(&def);
516 StructLoc { container, ast_id }.intern(self.db).into() 525 (StructLoc { container, ast_id }.intern(self.db).into(), def.name())
517 } 526 }
518 ast::ModuleItem::EnumDef(def) => { 527 ast::ModuleItem::EnumDef(def) => {
519 let ast_id = self.expander.ast_id(&def); 528 let ast_id = self.expander.ast_id(&def);
520 EnumLoc { container, ast_id }.intern(self.db).into() 529 (EnumLoc { container, ast_id }.intern(self.db).into(), def.name())
521 } 530 }
522 ast::ModuleItem::UnionDef(def) => { 531 ast::ModuleItem::UnionDef(def) => {
523 let ast_id = self.expander.ast_id(&def); 532 let ast_id = self.expander.ast_id(&def);
524 UnionLoc { container, ast_id }.intern(self.db).into() 533 (UnionLoc { container, ast_id }.intern(self.db).into(), def.name())
525 } 534 }
526 ast::ModuleItem::TraitDef(def) => { 535 ast::ModuleItem::TraitDef(def) => {
527 let ast_id = self.expander.ast_id(&def); 536 let ast_id = self.expander.ast_id(&def);
528 TraitLoc { container, ast_id }.intern(self.db).into() 537 (TraitLoc { container, ast_id }.intern(self.db).into(), def.name())
529 } 538 }
530 ast::ModuleItem::ImplBlock(_) 539 ast::ModuleItem::ImplBlock(_)
531 | ast::ModuleItem::UseItem(_) 540 | ast::ModuleItem::UseItem(_)
532 | ast::ModuleItem::ExternCrateItem(_) 541 | ast::ModuleItem::ExternCrateItem(_)
533 | ast::ModuleItem::Module(_) => continue, 542 | ast::ModuleItem::Module(_) => continue,
534 }; 543 };
535 self.body.item_scope.define_def(def) 544 self.body.item_scope.define_def(def);
545 if let Some(name) = name {
546 self.body.item_scope.push_res(name.as_name(), def.into());
547 }
536 } 548 }
537 } 549 }
538 550
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index e8ddcc3c2..b0288ee8d 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -51,6 +51,12 @@ impl ItemScope {
51 self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def)) 51 self.visible.iter().chain(BUILTIN_SCOPE.iter()).map(|(n, def)| (n, *def))
52 } 52 }
53 53
54 pub fn entries_without_primitives<'a>(
55 &'a self,
56 ) -> impl Iterator<Item = (&'a Name, PerNs)> + 'a {
57 self.visible.iter().map(|(n, def)| (n, *def))
58 }
59
54 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { 60 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
55 self.defs.iter().copied() 61 self.defs.iter().copied()
56 } 62 }
@@ -118,7 +124,7 @@ impl ItemScope {
118 self.legacy_macros.insert(name, mac); 124 self.legacy_macros.insert(name, mac);
119 } 125 }
120 126
121 pub(crate) fn push_res(&mut self, name: Name, def: &PerNs) -> bool { 127 pub(crate) fn push_res(&mut self, name: Name, def: PerNs) -> bool {
122 let mut changed = false; 128 let mut changed = false;
123 let existing = self.visible.entry(name.clone()).or_default(); 129 let existing = self.visible.entry(name.clone()).or_default();
124 130
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 4f1fd4801..b9f40d3dd 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -446,7 +446,7 @@ where
446 let scope = &mut self.def_map.modules[module_id].scope; 446 let scope = &mut self.def_map.modules[module_id].scope;
447 let mut changed = false; 447 let mut changed = false;
448 for (name, res) in resolutions { 448 for (name, res) in resolutions {
449 changed |= scope.push_res(name.clone(), res); 449 changed |= scope.push_res(name.clone(), *res);
450 } 450 }
451 451
452 if !changed { 452 if !changed {
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index e70049617..cf3c33d78 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -10,6 +10,7 @@ use rustc_hash::FxHashSet;
10 10
11use crate::{ 11use crate::{
12 body::scope::{ExprScopes, ScopeId}, 12 body::scope::{ExprScopes, ScopeId},
13 body::Body,
13 builtin_type::BuiltinType, 14 builtin_type::BuiltinType,
14 db::DefDatabase, 15 db::DefDatabase,
15 expr::{ExprId, PatId}, 16 expr::{ExprId, PatId},
@@ -55,6 +56,8 @@ enum Scope {
55 AdtScope(AdtId), 56 AdtScope(AdtId),
56 /// Local bindings 57 /// Local bindings
57 ExprScope(ExprScope), 58 ExprScope(ExprScope),
59 /// Temporary hack to support local items.
60 LocalItemsScope(Arc<Body>),
58} 61}
59 62
60#[derive(Debug, Clone, PartialEq, Eq, Hash)] 63#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -149,7 +152,13 @@ impl Resolver {
149 for scope in self.scopes.iter().rev() { 152 for scope in self.scopes.iter().rev() {
150 match scope { 153 match scope {
151 Scope::ExprScope(_) => continue, 154 Scope::ExprScope(_) => continue,
152 Scope::GenericParams { .. } | Scope::ImplBlockScope(_) if skip_to_mod => continue, 155 Scope::GenericParams { .. }
156 | Scope::ImplBlockScope(_)
157 | Scope::LocalItemsScope(_)
158 if skip_to_mod =>
159 {
160 continue
161 }
153 162
154 Scope::GenericParams { params, def } => { 163 Scope::GenericParams { params, def } => {
155 if let Some(local_id) = params.find_by_name(first_name) { 164 if let Some(local_id) = params.find_by_name(first_name) {
@@ -179,25 +188,35 @@ impl Resolver {
179 &path, 188 &path,
180 BuiltinShadowMode::Other, 189 BuiltinShadowMode::Other,
181 ); 190 );
182 let res = match module_def.take_types()? { 191 let res = to_type_ns(module_def)?;
183 ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
184 ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
185
186 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
187 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
188
189 ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
190
191 ModuleDefId::FunctionId(_)
192 | ModuleDefId::ConstId(_)
193 | ModuleDefId::StaticId(_)
194 | ModuleDefId::ModuleId(_) => return None,
195 };
196 return Some((res, idx)); 192 return Some((res, idx));
197 } 193 }
194 Scope::LocalItemsScope(body) => {
195 let def = body.item_scope.get(first_name, BuiltinShadowMode::Other);
196 if let Some(res) = to_type_ns(def) {
197 return Some((res, None));
198 }
199 }
198 } 200 }
199 } 201 }
200 None 202 return None;
203 fn to_type_ns(per_ns: PerNs) -> Option<TypeNs> {
204 let res = match per_ns.take_types()? {
205 ModuleDefId::AdtId(it) => TypeNs::AdtId(it),
206 ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariantId(it),
207
208 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAliasId(it),
209 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
210
211 ModuleDefId::TraitId(it) => TypeNs::TraitId(it),
212
213 ModuleDefId::FunctionId(_)
214 | ModuleDefId::ConstId(_)
215 | ModuleDefId::StaticId(_)
216 | ModuleDefId::ModuleId(_) => return None,
217 };
218 Some(res)
219 }
201 } 220 }
202 221
203 pub fn resolve_path_in_type_ns_fully( 222 pub fn resolve_path_in_type_ns_fully(
@@ -227,6 +246,7 @@ impl Resolver {
227 | Scope::ExprScope(_) 246 | Scope::ExprScope(_)
228 | Scope::GenericParams { .. } 247 | Scope::GenericParams { .. }
229 | Scope::ImplBlockScope(_) 248 | Scope::ImplBlockScope(_)
249 | Scope::LocalItemsScope(_)
230 if skip_to_mod => 250 if skip_to_mod =>
231 { 251 {
232 continue 252 continue
@@ -276,20 +296,7 @@ impl Resolver {
276 ); 296 );
277 return match idx { 297 return match idx {
278 None => { 298 None => {
279 let value = match module_def.take_values()? { 299 let value = to_value_ns(module_def)?;
280 ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),
281 ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it),
282 ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it),
283 ModuleDefId::ConstId(it) => ValueNs::ConstId(it),
284 ModuleDefId::StaticId(it) => ValueNs::StaticId(it),
285
286 ModuleDefId::AdtId(AdtId::EnumId(_))
287 | ModuleDefId::AdtId(AdtId::UnionId(_))
288 | ModuleDefId::TraitId(_)
289 | ModuleDefId::TypeAliasId(_)
290 | ModuleDefId::BuiltinType(_)
291 | ModuleDefId::ModuleId(_) => return None,
292 };
293 Some(ResolveValueResult::ValueNs(value)) 300 Some(ResolveValueResult::ValueNs(value))
294 } 301 }
295 Some(idx) => { 302 Some(idx) => {
@@ -309,9 +316,33 @@ impl Resolver {
309 } 316 }
310 }; 317 };
311 } 318 }
319 Scope::LocalItemsScope(body) => {
320 let def = body.item_scope.get(first_name, BuiltinShadowMode::Other);
321 if let Some(res) = to_value_ns(def) {
322 return Some(ResolveValueResult::ValueNs(res));
323 }
324 }
312 } 325 }
313 } 326 }
314 None 327 return None;
328
329 fn to_value_ns(per_ns: PerNs) -> Option<ValueNs> {
330 let res = match per_ns.take_values()? {
331 ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),
332 ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it),
333 ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it),
334 ModuleDefId::ConstId(it) => ValueNs::ConstId(it),
335 ModuleDefId::StaticId(it) => ValueNs::StaticId(it),
336
337 ModuleDefId::AdtId(AdtId::EnumId(_))
338 | ModuleDefId::AdtId(AdtId::UnionId(_))
339 | ModuleDefId::TraitId(_)
340 | ModuleDefId::TypeAliasId(_)
341 | ModuleDefId::BuiltinType(_)
342 | ModuleDefId::ModuleId(_) => return None,
343 };
344 Some(res)
345 }
315 } 346 }
316 347
317 pub fn resolve_path_in_value_ns_fully( 348 pub fn resolve_path_in_value_ns_fully(
@@ -429,6 +460,11 @@ impl Scope {
429 }); 460 });
430 } 461 }
431 } 462 }
463 Scope::LocalItemsScope(body) => {
464 body.item_scope.entries_without_primitives().for_each(|(name, def)| {
465 f(name.clone(), ScopeDef::PerNs(def));
466 })
467 }
432 Scope::GenericParams { params, def } => { 468 Scope::GenericParams { params, def } => {
433 for (local_id, param) in params.types.iter() { 469 for (local_id, param) in params.types.iter() {
434 f( 470 f(
@@ -464,6 +500,7 @@ pub fn resolver_for_scope(
464 scope_id: Option<ScopeId>, 500 scope_id: Option<ScopeId>,
465) -> Resolver { 501) -> Resolver {
466 let mut r = owner.resolver(db); 502 let mut r = owner.resolver(db);
503 r = r.push_local_items_scope(db.body(owner));
467 let scopes = db.expr_scopes(owner); 504 let scopes = db.expr_scopes(owner);
468 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>(); 505 let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
469 for scope in scope_chain.into_iter().rev() { 506 for scope in scope_chain.into_iter().rev() {
@@ -499,6 +536,10 @@ impl Resolver {
499 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id })) 536 self.push_scope(Scope::ModuleScope(ModuleItemMap { crate_def_map, module_id }))
500 } 537 }
501 538
539 fn push_local_items_scope(self, body: Arc<Body>) -> Resolver {
540 self.push_scope(Scope::LocalItemsScope(body))
541 }
542
502 fn push_expr_scope( 543 fn push_expr_scope(
503 self, 544 self,
504 owner: DefWithBodyId, 545 owner: DefWithBodyId,