aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/resolve.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/resolve.rs')
-rw-r--r--crates/ra_hir/src/resolve.rs110
1 files changed, 57 insertions, 53 deletions
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 75b24d386..b932b0c8c 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -3,8 +3,9 @@ use std::sync::Arc;
3 3
4use hir_def::{ 4use hir_def::{
5 builtin_type::BuiltinType, 5 builtin_type::BuiltinType,
6 nameres::CrateDefMap,
6 path::{Path, PathKind}, 7 path::{Path, PathKind},
7 CrateModuleId, 8 AdtId, CrateModuleId, ModuleDefId,
8}; 9};
9use hir_expand::name::{self, Name}; 10use hir_expand::name::{self, Name};
10use rustc_hash::FxHashSet; 11use rustc_hash::FxHashSet;
@@ -18,8 +19,8 @@ use crate::{
18 }, 19 },
19 generics::GenericParams, 20 generics::GenericParams,
20 impl_block::ImplBlock, 21 impl_block::ImplBlock,
21 nameres::{CrateDefMap, PerNs}, 22 Adt, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, PerNs, Static, Struct, Trait,
22 Adt, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct, Trait, TypeAlias, 23 TypeAlias,
23}; 24};
24 25
25#[derive(Debug, Clone, Default)] 26#[derive(Debug, Clone, Default)]
@@ -90,7 +91,7 @@ impl Resolver {
90 pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option<Trait> { 91 pub(crate) fn resolve_known_trait(&self, db: &impl HirDatabase, path: &Path) -> Option<Trait> {
91 let res = self.resolve_module_path(db, path).take_types()?; 92 let res = self.resolve_module_path(db, path).take_types()?;
92 match res { 93 match res {
93 ModuleDef::Trait(it) => Some(it), 94 ModuleDefId::TraitId(it) => Some(it.into()),
94 _ => None, 95 _ => None,
95 } 96 }
96 } 97 }
@@ -103,7 +104,7 @@ impl Resolver {
103 ) -> Option<Struct> { 104 ) -> Option<Struct> {
104 let res = self.resolve_module_path(db, path).take_types()?; 105 let res = self.resolve_module_path(db, path).take_types()?;
105 match res { 106 match res {
106 ModuleDef::Adt(Adt::Struct(it)) => Some(it), 107 ModuleDefId::AdtId(AdtId::StructId(it)) => Some(it.into()),
107 _ => None, 108 _ => None,
108 } 109 }
109 } 110 }
@@ -112,7 +113,7 @@ impl Resolver {
112 pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> { 113 pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> Option<Enum> {
113 let res = self.resolve_module_path(db, path).take_types()?; 114 let res = self.resolve_module_path(db, path).take_types()?;
114 match res { 115 match res {
115 ModuleDef::Adt(Adt::Enum(it)) => Some(it), 116 ModuleDefId::AdtId(AdtId::EnumId(it)) => Some(it.into()),
116 _ => None, 117 _ => None,
117 } 118 }
118 } 119 }
@@ -166,18 +167,18 @@ impl Resolver {
166 Scope::ModuleScope(m) => { 167 Scope::ModuleScope(m) => {
167 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path); 168 let (module_def, idx) = m.crate_def_map.resolve_path(db, m.module_id, path);
168 let res = match module_def.take_types()? { 169 let res = match module_def.take_types()? {
169 ModuleDef::Adt(it) => TypeNs::Adt(it), 170 ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()),
170 ModuleDef::EnumVariant(it) => TypeNs::EnumVariant(it), 171 ModuleDefId::EnumVariantId(it) => TypeNs::EnumVariant(it.into()),
171 172
172 ModuleDef::TypeAlias(it) => TypeNs::TypeAlias(it), 173 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()),
173 ModuleDef::BuiltinType(it) => TypeNs::BuiltinType(it), 174 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
174 175
175 ModuleDef::Trait(it) => TypeNs::Trait(it), 176 ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()),
176 177
177 ModuleDef::Function(_) 178 ModuleDefId::FunctionId(_)
178 | ModuleDef::Const(_) 179 | ModuleDefId::ConstId(_)
179 | ModuleDef::Static(_) 180 | ModuleDefId::StaticId(_)
180 | ModuleDef::Module(_) => return None, 181 | ModuleDefId::ModuleId(_) => return None,
181 }; 182 };
182 return Some((res, idx)); 183 return Some((res, idx));
183 } 184 }
@@ -261,33 +262,35 @@ impl Resolver {
261 return match idx { 262 return match idx {
262 None => { 263 None => {
263 let value = match module_def.take_values()? { 264 let value = match module_def.take_values()? {
264 ModuleDef::Function(it) => ValueNs::Function(it), 265 ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()),
265 ModuleDef::Adt(Adt::Struct(it)) => ValueNs::Struct(it), 266 ModuleDefId::AdtId(AdtId::StructId(it)) => {
266 ModuleDef::EnumVariant(it) => ValueNs::EnumVariant(it), 267 ValueNs::Struct(it.into())
267 ModuleDef::Const(it) => ValueNs::Const(it), 268 }
268 ModuleDef::Static(it) => ValueNs::Static(it), 269 ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()),
269 270 ModuleDefId::ConstId(it) => ValueNs::Const(it.into()),
270 ModuleDef::Adt(Adt::Enum(_)) 271 ModuleDefId::StaticId(it) => ValueNs::Static(it.into()),
271 | ModuleDef::Adt(Adt::Union(_)) 272
272 | ModuleDef::Trait(_) 273 ModuleDefId::AdtId(AdtId::EnumId(_))
273 | ModuleDef::TypeAlias(_) 274 | ModuleDefId::AdtId(AdtId::UnionId(_))
274 | ModuleDef::BuiltinType(_) 275 | ModuleDefId::TraitId(_)
275 | ModuleDef::Module(_) => return None, 276 | ModuleDefId::TypeAliasId(_)
277 | ModuleDefId::BuiltinType(_)
278 | ModuleDefId::ModuleId(_) => return None,
276 }; 279 };
277 Some(ResolveValueResult::ValueNs(value)) 280 Some(ResolveValueResult::ValueNs(value))
278 } 281 }
279 Some(idx) => { 282 Some(idx) => {
280 let ty = match module_def.take_types()? { 283 let ty = match module_def.take_types()? {
281 ModuleDef::Adt(it) => TypeNs::Adt(it), 284 ModuleDefId::AdtId(it) => TypeNs::Adt(it.into()),
282 ModuleDef::Trait(it) => TypeNs::Trait(it), 285 ModuleDefId::TraitId(it) => TypeNs::Trait(it.into()),
283 ModuleDef::TypeAlias(it) => TypeNs::TypeAlias(it), 286 ModuleDefId::TypeAliasId(it) => TypeNs::TypeAlias(it.into()),
284 ModuleDef::BuiltinType(it) => TypeNs::BuiltinType(it), 287 ModuleDefId::BuiltinType(it) => TypeNs::BuiltinType(it),
285 288
286 ModuleDef::Module(_) 289 ModuleDefId::ModuleId(_)
287 | ModuleDef::Function(_) 290 | ModuleDefId::FunctionId(_)
288 | ModuleDef::EnumVariant(_) 291 | ModuleDefId::EnumVariantId(_)
289 | ModuleDef::Const(_) 292 | ModuleDefId::ConstId(_)
290 | ModuleDef::Static(_) => return None, 293 | ModuleDefId::StaticId(_) => return None,
291 }; 294 };
292 Some(ResolveValueResult::Partial(ty, idx)) 295 Some(ResolveValueResult::Partial(ty, idx))
293 } 296 }
@@ -315,7 +318,7 @@ impl Resolver {
315 path: &Path, 318 path: &Path,
316 ) -> Option<MacroDef> { 319 ) -> Option<MacroDef> {
317 let (item_map, module) = self.module()?; 320 let (item_map, module) = self.module()?;
318 item_map.resolve_path(db, module, path).0.get_macros() 321 item_map.resolve_path(db, module, path).0.get_macros().map(MacroDef::from)
319 } 322 }
320 323
321 pub(crate) fn process_all_names( 324 pub(crate) fn process_all_names(
@@ -333,10 +336,11 @@ impl Resolver {
333 for scope in &self.scopes { 336 for scope in &self.scopes {
334 if let Scope::ModuleScope(m) = scope { 337 if let Scope::ModuleScope(m) = scope {
335 if let Some(prelude) = m.crate_def_map.prelude() { 338 if let Some(prelude) = m.crate_def_map.prelude() {
336 let prelude_def_map = db.crate_def_map(prelude.krate()); 339 let prelude_def_map = db.crate_def_map(prelude.krate);
337 traits.extend(prelude_def_map[prelude.id.module_id].scope.traits()); 340 traits
341 .extend(prelude_def_map[prelude.module_id].scope.traits().map(Trait::from));
338 } 342 }
339 traits.extend(m.crate_def_map[m.module_id].scope.traits()); 343 traits.extend(m.crate_def_map[m.module_id].scope.traits().map(Trait::from));
340 } 344 }
341 } 345 }
342 traits 346 traits
@@ -351,7 +355,7 @@ impl Resolver {
351 } 355 }
352 356
353 pub(crate) fn krate(&self) -> Option<Crate> { 357 pub(crate) fn krate(&self) -> Option<Crate> {
354 self.module().map(|t| t.0.krate()) 358 self.module().map(|t| Crate { crate_id: t.0.krate() })
355 } 359 }
356 360
357 pub(crate) fn where_predicates_in_scope<'a>( 361 pub(crate) fn where_predicates_in_scope<'a>(
@@ -420,8 +424,10 @@ impl From<PerNs> for ScopeDef {
420 fn from(def: PerNs) -> Self { 424 fn from(def: PerNs) -> Self {
421 def.take_types() 425 def.take_types()
422 .or_else(|| def.take_values()) 426 .or_else(|| def.take_values())
423 .map(ScopeDef::ModuleDef) 427 .map(|module_def_id| ScopeDef::ModuleDef(module_def_id.into()))
424 .or_else(|| def.get_macros().map(ScopeDef::MacroDef)) 428 .or_else(|| {
429 def.get_macros().map(|macro_def_id| ScopeDef::MacroDef(macro_def_id.into()))
430 })
425 .unwrap_or(ScopeDef::Unknown) 431 .unwrap_or(ScopeDef::Unknown)
426 } 432 }
427} 433}
@@ -441,18 +447,16 @@ impl Scope {
441 f(name.clone(), res.def.into()); 447 f(name.clone(), res.def.into());
442 }); 448 });
443 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| { 449 m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
444 f(name.clone(), ScopeDef::MacroDef(macro_)); 450 f(name.clone(), ScopeDef::MacroDef(macro_.into()));
445 }); 451 });
446 m.crate_def_map.extern_prelude().iter().for_each(|(name, def)| { 452 m.crate_def_map.extern_prelude().iter().for_each(|(name, &def)| {
447 f(name.clone(), ScopeDef::ModuleDef(*def)); 453 f(name.clone(), ScopeDef::ModuleDef(def.into()));
448 }); 454 });
449 if let Some(prelude) = m.crate_def_map.prelude() { 455 if let Some(prelude) = m.crate_def_map.prelude() {
450 let prelude_def_map = db.crate_def_map(prelude.krate()); 456 let prelude_def_map = db.crate_def_map(prelude.krate);
451 prelude_def_map[prelude.id.module_id].scope.entries().for_each( 457 prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| {
452 |(name, res)| { 458 f(name.clone(), res.def.into());
453 f(name.clone(), res.def.into()); 459 });
454 },
455 );
456 } 460 }
457 } 461 }
458 Scope::GenericParams(gp) => { 462 Scope::GenericParams(gp) => {