diff options
Diffstat (limited to 'crates/hir/src/code_model.rs')
-rw-r--r-- | crates/hir/src/code_model.rs | 92 |
1 files changed, 49 insertions, 43 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index aaa7013b6..e9bb4f541 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -18,8 +18,8 @@ use hir_def::{ | |||
18 | type_ref::{Mutability, TypeRef}, | 18 | type_ref::{Mutability, TypeRef}, |
19 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, | 19 | AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, |
20 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, | 20 | DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, |
21 | LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, | 21 | LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, |
22 | TypeAliasId, TypeParamId, UnionId, | 22 | TypeParamId, UnionId, |
23 | }; | 23 | }; |
24 | use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; | 24 | use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; |
25 | use hir_expand::{ | 25 | use hir_expand::{ |
@@ -90,8 +90,8 @@ impl Crate { | |||
90 | } | 90 | } |
91 | 91 | ||
92 | pub fn root_module(self, db: &dyn HirDatabase) -> Module { | 92 | pub fn root_module(self, db: &dyn HirDatabase) -> Module { |
93 | let module_id = db.crate_def_map(self.id).root(); | 93 | let def_map = db.crate_def_map(self.id); |
94 | Module::new(self, module_id) | 94 | Module { id: def_map.module_id(def_map.root()) } |
95 | } | 95 | } |
96 | 96 | ||
97 | pub fn root_file(self, db: &dyn HirDatabase) -> FileId { | 97 | pub fn root_file(self, db: &dyn HirDatabase) -> FileId { |
@@ -270,18 +270,14 @@ impl ModuleDef { | |||
270 | None => return, | 270 | None => return, |
271 | }; | 271 | }; |
272 | 272 | ||
273 | hir_ty::diagnostics::validate_module_item(db, module.id.krate, id, sink) | 273 | hir_ty::diagnostics::validate_module_item(db, module.id.krate(), id, sink) |
274 | } | 274 | } |
275 | } | 275 | } |
276 | 276 | ||
277 | impl Module { | 277 | impl Module { |
278 | pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { | ||
279 | Module { id: ModuleId { krate: krate.id, local_id: crate_module_id } } | ||
280 | } | ||
281 | |||
282 | /// Name of this module. | 278 | /// Name of this module. |
283 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | 279 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { |
284 | let def_map = db.crate_def_map(self.id.krate); | 280 | let def_map = self.id.def_map(db.upcast()); |
285 | let parent = def_map[self.id.local_id].parent?; | 281 | let parent = def_map[self.id.local_id].parent?; |
286 | def_map[parent].children.iter().find_map(|(name, module_id)| { | 282 | def_map[parent].children.iter().find_map(|(name, module_id)| { |
287 | if *module_id == self.id.local_id { | 283 | if *module_id == self.id.local_id { |
@@ -294,33 +290,34 @@ impl Module { | |||
294 | 290 | ||
295 | /// Returns the crate this module is part of. | 291 | /// Returns the crate this module is part of. |
296 | pub fn krate(self) -> Crate { | 292 | pub fn krate(self) -> Crate { |
297 | Crate { id: self.id.krate } | 293 | Crate { id: self.id.krate() } |
298 | } | 294 | } |
299 | 295 | ||
300 | /// Topmost parent of this module. Every module has a `crate_root`, but some | 296 | /// Topmost parent of this module. Every module has a `crate_root`, but some |
301 | /// might be missing `krate`. This can happen if a module's file is not included | 297 | /// might be missing `krate`. This can happen if a module's file is not included |
302 | /// in the module tree of any target in `Cargo.toml`. | 298 | /// in the module tree of any target in `Cargo.toml`. |
303 | pub fn crate_root(self, db: &dyn HirDatabase) -> Module { | 299 | pub fn crate_root(self, db: &dyn HirDatabase) -> Module { |
304 | let def_map = db.crate_def_map(self.id.krate); | 300 | let def_map = db.crate_def_map(self.id.krate()); |
305 | self.with_module_id(def_map.root()) | 301 | Module { id: def_map.module_id(def_map.root()) } |
306 | } | 302 | } |
307 | 303 | ||
308 | /// Iterates over all child modules. | 304 | /// Iterates over all child modules. |
309 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { | 305 | pub fn children(self, db: &dyn HirDatabase) -> impl Iterator<Item = Module> { |
310 | let def_map = db.crate_def_map(self.id.krate); | 306 | let def_map = self.id.def_map(db.upcast()); |
311 | let children = def_map[self.id.local_id] | 307 | let children = def_map[self.id.local_id] |
312 | .children | 308 | .children |
313 | .iter() | 309 | .iter() |
314 | .map(|(_, module_id)| self.with_module_id(*module_id)) | 310 | .map(|(_, module_id)| Module { id: def_map.module_id(*module_id) }) |
315 | .collect::<Vec<_>>(); | 311 | .collect::<Vec<_>>(); |
316 | children.into_iter() | 312 | children.into_iter() |
317 | } | 313 | } |
318 | 314 | ||
319 | /// Finds a parent module. | 315 | /// Finds a parent module. |
320 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { | 316 | pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { |
321 | let def_map = db.crate_def_map(self.id.krate); | 317 | // FIXME: handle block expressions as modules (their parent is in a different DefMap) |
318 | let def_map = self.id.def_map(db.upcast()); | ||
322 | let parent_id = def_map[self.id.local_id].parent?; | 319 | let parent_id = def_map[self.id.local_id].parent?; |
323 | Some(self.with_module_id(parent_id)) | 320 | Some(Module { id: def_map.module_id(parent_id) }) |
324 | } | 321 | } |
325 | 322 | ||
326 | pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> { | 323 | pub fn path_to_root(self, db: &dyn HirDatabase) -> Vec<Module> { |
@@ -339,7 +336,7 @@ impl Module { | |||
339 | db: &dyn HirDatabase, | 336 | db: &dyn HirDatabase, |
340 | visible_from: Option<Module>, | 337 | visible_from: Option<Module>, |
341 | ) -> Vec<(Name, ScopeDef)> { | 338 | ) -> Vec<(Name, ScopeDef)> { |
342 | db.crate_def_map(self.id.krate)[self.id.local_id] | 339 | self.id.def_map(db.upcast())[self.id.local_id] |
343 | .scope | 340 | .scope |
344 | .entries() | 341 | .entries() |
345 | .filter_map(|(name, def)| { | 342 | .filter_map(|(name, def)| { |
@@ -362,14 +359,14 @@ impl Module { | |||
362 | } | 359 | } |
363 | 360 | ||
364 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | 361 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { |
365 | db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into()) | 362 | self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) |
366 | } | 363 | } |
367 | 364 | ||
368 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 365 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
369 | let _p = profile::span("Module::diagnostics").detail(|| { | 366 | let _p = profile::span("Module::diagnostics").detail(|| { |
370 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) | 367 | format!("{:?}", self.name(db).map_or("<unknown>".into(), |name| name.to_string())) |
371 | }); | 368 | }); |
372 | let crate_def_map = db.crate_def_map(self.id.krate); | 369 | let crate_def_map = self.id.def_map(db.upcast()); |
373 | crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); | 370 | crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); |
374 | for decl in self.declarations(db) { | 371 | for decl in self.declarations(db) { |
375 | match decl { | 372 | match decl { |
@@ -396,19 +393,15 @@ impl Module { | |||
396 | } | 393 | } |
397 | 394 | ||
398 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { | 395 | pub fn declarations(self, db: &dyn HirDatabase) -> Vec<ModuleDef> { |
399 | let def_map = db.crate_def_map(self.id.krate); | 396 | let def_map = self.id.def_map(db.upcast()); |
400 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() | 397 | def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() |
401 | } | 398 | } |
402 | 399 | ||
403 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { | 400 | pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec<Impl> { |
404 | let def_map = db.crate_def_map(self.id.krate); | 401 | let def_map = self.id.def_map(db.upcast()); |
405 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() | 402 | def_map[self.id.local_id].scope.impls().map(Impl::from).collect() |
406 | } | 403 | } |
407 | 404 | ||
408 | pub(crate) fn with_module_id(self, module_id: LocalModuleId) -> Module { | ||
409 | Module::new(self.krate(), module_id) | ||
410 | } | ||
411 | |||
412 | /// Finds a path that can be used to refer to the given item from within | 405 | /// Finds a path that can be used to refer to the given item from within |
413 | /// this module, if possible. | 406 | /// this module, if possible. |
414 | pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { | 407 | pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { |
@@ -457,7 +450,7 @@ impl Field { | |||
457 | }; | 450 | }; |
458 | let substs = Substs::type_params(db, generic_def_id); | 451 | let substs = Substs::type_params(db, generic_def_id); |
459 | let ty = db.field_types(var_id)[self.id].clone().subst(&substs); | 452 | let ty = db.field_types(var_id)[self.id].clone().subst(&substs); |
460 | Type::new(db, self.parent.module(db).id.krate, var_id, ty) | 453 | Type::new(db, self.parent.module(db).id.krate(), var_id, ty) |
461 | } | 454 | } |
462 | 455 | ||
463 | pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { | 456 | pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { |
@@ -502,7 +495,11 @@ impl Struct { | |||
502 | } | 495 | } |
503 | 496 | ||
504 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 497 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
505 | Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) | 498 | Type::from_def( |
499 | db, | ||
500 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
501 | self.id, | ||
502 | ) | ||
506 | } | 503 | } |
507 | 504 | ||
508 | pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> { | 505 | pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> { |
@@ -533,7 +530,11 @@ impl Union { | |||
533 | } | 530 | } |
534 | 531 | ||
535 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 532 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
536 | Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) | 533 | Type::from_def( |
534 | db, | ||
535 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
536 | self.id, | ||
537 | ) | ||
537 | } | 538 | } |
538 | 539 | ||
539 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | 540 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { |
@@ -573,7 +574,11 @@ impl Enum { | |||
573 | } | 574 | } |
574 | 575 | ||
575 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 576 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
576 | Type::from_def(db, self.id.lookup(db.upcast()).container.module(db.upcast()).krate, self.id) | 577 | Type::from_def( |
578 | db, | ||
579 | self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), | ||
580 | self.id, | ||
581 | ) | ||
577 | } | 582 | } |
578 | } | 583 | } |
579 | 584 | ||
@@ -632,7 +637,7 @@ impl Adt { | |||
632 | /// general set of completions, but will not look very nice when printed. | 637 | /// general set of completions, but will not look very nice when printed. |
633 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 638 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
634 | let id = AdtId::from(self); | 639 | let id = AdtId::from(self); |
635 | Type::from_def(db, id.module(db.upcast()).krate, id) | 640 | Type::from_def(db, id.module(db.upcast()).krate(), id) |
636 | } | 641 | } |
637 | 642 | ||
638 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 643 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
@@ -750,7 +755,7 @@ impl Function { | |||
750 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); | 755 | let ctx = hir_ty::TyLoweringContext::new(db, &resolver); |
751 | let environment = TraitEnvironment::lower(db, &resolver); | 756 | let environment = TraitEnvironment::lower(db, &resolver); |
752 | Type { | 757 | Type { |
753 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, | 758 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), |
754 | ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, | 759 | ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, |
755 | } | 760 | } |
756 | } | 761 | } |
@@ -771,7 +776,7 @@ impl Function { | |||
771 | .iter() | 776 | .iter() |
772 | .map(|type_ref| { | 777 | .map(|type_ref| { |
773 | let ty = Type { | 778 | let ty = Type { |
774 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, | 779 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), |
775 | ty: InEnvironment { | 780 | ty: InEnvironment { |
776 | value: Ty::from_hir_ext(&ctx, type_ref).0, | 781 | value: Ty::from_hir_ext(&ctx, type_ref).0, |
777 | environment: environment.clone(), | 782 | environment: environment.clone(), |
@@ -795,7 +800,7 @@ impl Function { | |||
795 | } | 800 | } |
796 | 801 | ||
797 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 802 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
798 | let krate = self.module(db).id.krate; | 803 | let krate = self.module(db).id.krate(); |
799 | hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); | 804 | hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); |
800 | hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink); | 805 | hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink); |
801 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink); | 806 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink); |
@@ -973,7 +978,7 @@ impl TypeAlias { | |||
973 | } | 978 | } |
974 | 979 | ||
975 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 980 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
976 | Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate, self.id) | 981 | Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id) |
977 | } | 982 | } |
978 | 983 | ||
979 | pub fn name(self, db: &dyn HirDatabase) -> Name { | 984 | pub fn name(self, db: &dyn HirDatabase) -> Name { |
@@ -1000,8 +1005,9 @@ impl MacroDef { | |||
1000 | /// early, in `hir_expand`, where modules simply do not exist yet. | 1005 | /// early, in `hir_expand`, where modules simply do not exist yet. |
1001 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | 1006 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { |
1002 | let krate = self.id.krate; | 1007 | let krate = self.id.krate; |
1003 | let module_id = db.crate_def_map(krate).root(); | 1008 | let def_map = db.crate_def_map(krate); |
1004 | Some(Module::new(Crate { id: krate }, module_id)) | 1009 | let module_id = def_map.root(); |
1010 | Some(Module { id: def_map.module_id(module_id) }) | ||
1005 | } | 1011 | } |
1006 | 1012 | ||
1007 | /// XXX: this parses the file | 1013 | /// XXX: this parses the file |
@@ -1230,7 +1236,7 @@ impl Local { | |||
1230 | let def = DefWithBodyId::from(self.parent); | 1236 | let def = DefWithBodyId::from(self.parent); |
1231 | let infer = db.infer(def); | 1237 | let infer = db.infer(def); |
1232 | let ty = infer[self.pat_id].clone(); | 1238 | let ty = infer[self.pat_id].clone(); |
1233 | let krate = def.module(db.upcast()).krate; | 1239 | let krate = def.module(db.upcast()).krate(); |
1234 | Type::new(db, krate, def, ty) | 1240 | Type::new(db, krate, def, ty) |
1235 | } | 1241 | } |
1236 | 1242 | ||
@@ -1318,7 +1324,7 @@ impl TypeParam { | |||
1318 | let environment = TraitEnvironment::lower(db, &resolver); | 1324 | let environment = TraitEnvironment::lower(db, &resolver); |
1319 | let ty = Ty::Placeholder(self.id); | 1325 | let ty = Ty::Placeholder(self.id); |
1320 | Type { | 1326 | Type { |
1321 | krate: self.id.parent.module(db.upcast()).krate, | 1327 | krate: self.id.parent.module(db.upcast()).krate(), |
1322 | ty: InEnvironment { value: ty, environment }, | 1328 | ty: InEnvironment { value: ty, environment }, |
1323 | } | 1329 | } |
1324 | } | 1330 | } |
@@ -1344,7 +1350,7 @@ impl TypeParam { | |||
1344 | let subst = Substs::type_params(db, self.id.parent); | 1350 | let subst = Substs::type_params(db, self.id.parent); |
1345 | let ty = ty.subst(&subst.prefix(local_idx)); | 1351 | let ty = ty.subst(&subst.prefix(local_idx)); |
1346 | Some(Type { | 1352 | Some(Type { |
1347 | krate: self.id.parent.module(db.upcast()).krate, | 1353 | krate: self.id.parent.module(db.upcast()).krate(), |
1348 | ty: InEnvironment { value: ty, environment }, | 1354 | ty: InEnvironment { value: ty, environment }, |
1349 | }) | 1355 | }) |
1350 | } | 1356 | } |
@@ -1405,7 +1411,7 @@ impl ConstParam { | |||
1405 | 1411 | ||
1406 | pub fn ty(self, db: &dyn HirDatabase) -> Type { | 1412 | pub fn ty(self, db: &dyn HirDatabase) -> Type { |
1407 | let def = self.id.parent; | 1413 | let def = self.id.parent; |
1408 | let krate = def.module(db.upcast()).krate; | 1414 | let krate = def.module(db.upcast()).krate(); |
1409 | Type::new(db, krate, def, db.const_param_ty(self.id)) | 1415 | Type::new(db, krate, def, db.const_param_ty(self.id)) |
1410 | } | 1416 | } |
1411 | } | 1417 | } |
@@ -1440,7 +1446,7 @@ impl Impl { | |||
1440 | let environment = TraitEnvironment::lower(db, &resolver); | 1446 | let environment = TraitEnvironment::lower(db, &resolver); |
1441 | let ty = Ty::from_hir(&ctx, &impl_data.target_type); | 1447 | let ty = Ty::from_hir(&ctx, &impl_data.target_type); |
1442 | Type { | 1448 | Type { |
1443 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, | 1449 | krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(), |
1444 | ty: InEnvironment { value: ty, environment }, | 1450 | ty: InEnvironment { value: ty, environment }, |
1445 | } | 1451 | } |
1446 | } | 1452 | } |
@@ -1458,7 +1464,7 @@ impl Impl { | |||
1458 | } | 1464 | } |
1459 | 1465 | ||
1460 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { | 1466 | pub fn krate(self, db: &dyn HirDatabase) -> Crate { |
1461 | Crate { id: self.module(db).id.krate } | 1467 | Crate { id: self.module(db).id.krate() } |
1462 | } | 1468 | } |
1463 | 1469 | ||
1464 | pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> { | 1470 | pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> { |