aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-01-22 17:09:55 +0000
committerJonas Schievink <[email protected]>2021-01-22 17:09:55 +0000
commit3de8f57c51192fa8e57713a3580c2bcc6907a194 (patch)
treef062bb33b2cfe9983605d6957eb56ddc21af2e93 /crates/hir/src
parente73cc8b4abed6e6f5d801d0032070fa9fc662ec3 (diff)
Make `ModuleId`'s `krate` field private
Diffstat (limited to 'crates/hir/src')
-rw-r--r--crates/hir/src/code_model.rs51
-rw-r--r--crates/hir/src/semantics/source_to_def.rs7
2 files changed, 36 insertions, 22 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index c34a99d90..d9b4cdfce 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -270,13 +270,13 @@ 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
277impl Module { 277impl Module {
278 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> 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 } } 279 Module { id: ModuleId::top_level(krate.id, crate_module_id) }
280 } 280 }
281 281
282 /// Name of this module. 282 /// Name of this module.
@@ -294,14 +294,14 @@ impl Module {
294 294
295 /// Returns the crate this module is part of. 295 /// Returns the crate this module is part of.
296 pub fn krate(self) -> Crate { 296 pub fn krate(self) -> Crate {
297 Crate { id: self.id.krate } 297 Crate { id: self.id.krate() }
298 } 298 }
299 299
300 /// Topmost parent of this module. Every module has a `crate_root`, but some 300 /// 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 301 /// 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`. 302 /// in the module tree of any target in `Cargo.toml`.
303 pub fn crate_root(self, db: &dyn HirDatabase) -> Module { 303 pub fn crate_root(self, db: &dyn HirDatabase) -> Module {
304 let def_map = db.crate_def_map(self.id.krate); 304 let def_map = db.crate_def_map(self.id.krate());
305 self.with_module_id(def_map.root()) 305 self.with_module_id(def_map.root())
306 } 306 }
307 307
@@ -318,6 +318,7 @@ impl Module {
318 318
319 /// Finds a parent module. 319 /// Finds a parent module.
320 pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> { 320 pub fn parent(self, db: &dyn HirDatabase) -> Option<Module> {
321 // FIXME: handle block expressions as modules (their parent is in a different DefMap)
321 let def_map = self.id.def_map(db.upcast()); 322 let def_map = self.id.def_map(db.upcast());
322 let parent_id = def_map[self.id.local_id].parent?; 323 let parent_id = def_map[self.id.local_id].parent?;
323 Some(self.with_module_id(parent_id)) 324 Some(self.with_module_id(parent_id))
@@ -457,7 +458,7 @@ impl Field {
457 }; 458 };
458 let substs = Substs::type_params(db, generic_def_id); 459 let substs = Substs::type_params(db, generic_def_id);
459 let ty = db.field_types(var_id)[self.id].clone().subst(&substs); 460 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) 461 Type::new(db, self.parent.module(db).id.krate(), var_id, ty)
461 } 462 }
462 463
463 pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef { 464 pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
@@ -502,7 +503,11 @@ impl Struct {
502 } 503 }
503 504
504 pub fn ty(self, db: &dyn HirDatabase) -> Type { 505 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) 506 Type::from_def(
507 db,
508 self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
509 self.id,
510 )
506 } 511 }
507 512
508 pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> { 513 pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
@@ -533,7 +538,11 @@ impl Union {
533 } 538 }
534 539
535 pub fn ty(self, db: &dyn HirDatabase) -> Type { 540 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) 541 Type::from_def(
542 db,
543 self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
544 self.id,
545 )
537 } 546 }
538 547
539 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { 548 pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
@@ -573,7 +582,11 @@ impl Enum {
573 } 582 }
574 583
575 pub fn ty(self, db: &dyn HirDatabase) -> Type { 584 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) 585 Type::from_def(
586 db,
587 self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
588 self.id,
589 )
577 } 590 }
578} 591}
579 592
@@ -632,7 +645,7 @@ impl Adt {
632 /// general set of completions, but will not look very nice when printed. 645 /// general set of completions, but will not look very nice when printed.
633 pub fn ty(self, db: &dyn HirDatabase) -> Type { 646 pub fn ty(self, db: &dyn HirDatabase) -> Type {
634 let id = AdtId::from(self); 647 let id = AdtId::from(self);
635 Type::from_def(db, id.module(db.upcast()).krate, id) 648 Type::from_def(db, id.module(db.upcast()).krate(), id)
636 } 649 }
637 650
638 pub fn module(self, db: &dyn HirDatabase) -> Module { 651 pub fn module(self, db: &dyn HirDatabase) -> Module {
@@ -750,7 +763,7 @@ impl Function {
750 let ctx = hir_ty::TyLoweringContext::new(db, &resolver); 763 let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
751 let environment = TraitEnvironment::lower(db, &resolver); 764 let environment = TraitEnvironment::lower(db, &resolver);
752 Type { 765 Type {
753 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, 766 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
754 ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment }, 767 ty: InEnvironment { value: Ty::from_hir_ext(&ctx, ret_type).0, environment },
755 } 768 }
756 } 769 }
@@ -771,7 +784,7 @@ impl Function {
771 .iter() 784 .iter()
772 .map(|type_ref| { 785 .map(|type_ref| {
773 let ty = Type { 786 let ty = Type {
774 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, 787 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
775 ty: InEnvironment { 788 ty: InEnvironment {
776 value: Ty::from_hir_ext(&ctx, type_ref).0, 789 value: Ty::from_hir_ext(&ctx, type_ref).0,
777 environment: environment.clone(), 790 environment: environment.clone(),
@@ -795,7 +808,7 @@ impl Function {
795 } 808 }
796 809
797 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { 810 pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
798 let krate = self.module(db).id.krate; 811 let krate = self.module(db).id.krate();
799 hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink); 812 hir_def::diagnostics::validate_body(db.upcast(), self.id.into(), sink);
800 hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink); 813 hir_ty::diagnostics::validate_module_item(db, krate, self.id.into(), sink);
801 hir_ty::diagnostics::validate_body(db, self.id.into(), sink); 814 hir_ty::diagnostics::validate_body(db, self.id.into(), sink);
@@ -973,7 +986,7 @@ impl TypeAlias {
973 } 986 }
974 987
975 pub fn ty(self, db: &dyn HirDatabase) -> Type { 988 pub fn ty(self, db: &dyn HirDatabase) -> Type {
976 Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate, self.id) 989 Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id)
977 } 990 }
978 991
979 pub fn name(self, db: &dyn HirDatabase) -> Name { 992 pub fn name(self, db: &dyn HirDatabase) -> Name {
@@ -1230,7 +1243,7 @@ impl Local {
1230 let def = DefWithBodyId::from(self.parent); 1243 let def = DefWithBodyId::from(self.parent);
1231 let infer = db.infer(def); 1244 let infer = db.infer(def);
1232 let ty = infer[self.pat_id].clone(); 1245 let ty = infer[self.pat_id].clone();
1233 let krate = def.module(db.upcast()).krate; 1246 let krate = def.module(db.upcast()).krate();
1234 Type::new(db, krate, def, ty) 1247 Type::new(db, krate, def, ty)
1235 } 1248 }
1236 1249
@@ -1318,7 +1331,7 @@ impl TypeParam {
1318 let environment = TraitEnvironment::lower(db, &resolver); 1331 let environment = TraitEnvironment::lower(db, &resolver);
1319 let ty = Ty::Placeholder(self.id); 1332 let ty = Ty::Placeholder(self.id);
1320 Type { 1333 Type {
1321 krate: self.id.parent.module(db.upcast()).krate, 1334 krate: self.id.parent.module(db.upcast()).krate(),
1322 ty: InEnvironment { value: ty, environment }, 1335 ty: InEnvironment { value: ty, environment },
1323 } 1336 }
1324 } 1337 }
@@ -1344,7 +1357,7 @@ impl TypeParam {
1344 let subst = Substs::type_params(db, self.id.parent); 1357 let subst = Substs::type_params(db, self.id.parent);
1345 let ty = ty.subst(&subst.prefix(local_idx)); 1358 let ty = ty.subst(&subst.prefix(local_idx));
1346 Some(Type { 1359 Some(Type {
1347 krate: self.id.parent.module(db.upcast()).krate, 1360 krate: self.id.parent.module(db.upcast()).krate(),
1348 ty: InEnvironment { value: ty, environment }, 1361 ty: InEnvironment { value: ty, environment },
1349 }) 1362 })
1350 } 1363 }
@@ -1405,7 +1418,7 @@ impl ConstParam {
1405 1418
1406 pub fn ty(self, db: &dyn HirDatabase) -> Type { 1419 pub fn ty(self, db: &dyn HirDatabase) -> Type {
1407 let def = self.id.parent; 1420 let def = self.id.parent;
1408 let krate = def.module(db.upcast()).krate; 1421 let krate = def.module(db.upcast()).krate();
1409 Type::new(db, krate, def, db.const_param_ty(self.id)) 1422 Type::new(db, krate, def, db.const_param_ty(self.id))
1410 } 1423 }
1411} 1424}
@@ -1440,7 +1453,7 @@ impl Impl {
1440 let environment = TraitEnvironment::lower(db, &resolver); 1453 let environment = TraitEnvironment::lower(db, &resolver);
1441 let ty = Ty::from_hir(&ctx, &impl_data.target_type); 1454 let ty = Ty::from_hir(&ctx, &impl_data.target_type);
1442 Type { 1455 Type {
1443 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate, 1456 krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate(),
1444 ty: InEnvironment { value: ty, environment }, 1457 ty: InEnvironment { value: ty, environment },
1445 } 1458 }
1446 } 1459 }
@@ -1458,7 +1471,7 @@ impl Impl {
1458 } 1471 }
1459 1472
1460 pub fn krate(self, db: &dyn HirDatabase) -> Crate { 1473 pub fn krate(self, db: &dyn HirDatabase) -> Crate {
1461 Crate { id: self.module(db).id.krate } 1474 Crate { id: self.module(db).id.krate() }
1462 } 1475 }
1463 1476
1464 pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> { 1477 pub fn is_builtin_derive(self, db: &dyn HirDatabase) -> Option<InFile<ast::Attr>> {
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index 775f7ec8b..faede3269 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -36,7 +36,7 @@ impl SourceToDefCtx<'_, '_> {
36 let local_id = crate_def_map.modules_for_file(file).next()?; 36 let local_id = crate_def_map.modules_for_file(file).next()?;
37 Some((crate_id, local_id)) 37 Some((crate_id, local_id))
38 })?; 38 })?;
39 Some(ModuleId { krate, local_id }) 39 Some(ModuleId::top_level(krate, local_id))
40 } 40 }
41 41
42 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { 42 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
@@ -63,7 +63,8 @@ impl SourceToDefCtx<'_, '_> {
63 let child_name = src.value.name()?.as_name(); 63 let child_name = src.value.name()?.as_name();
64 let def_map = parent_module.def_map(self.db.upcast()); 64 let def_map = parent_module.def_map(self.db.upcast());
65 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?; 65 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
66 Some(ModuleId { krate: parent_module.krate, local_id: child_id }) 66 // FIXME: handle block expression modules
67 Some(ModuleId::top_level(parent_module.krate(), child_id))
67 } 68 }
68 69
69 pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> { 70 pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
@@ -186,7 +187,7 @@ impl SourceToDefCtx<'_, '_> {
186 ) -> Option<MacroDefId> { 187 ) -> Option<MacroDefId> {
187 let kind = MacroDefKind::Declarative; 188 let kind = MacroDefKind::Declarative;
188 let file_id = src.file_id.original_file(self.db.upcast()); 189 let file_id = src.file_id.original_file(self.db.upcast());
189 let krate = self.file_to_def(file_id)?.krate; 190 let krate = self.file_to_def(file_id)?.krate();
190 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); 191 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
191 let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast())); 192 let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast()));
192 Some(MacroDefId { krate, ast_id, kind, local_inner: false }) 193 Some(MacroDefId { krate, ast_id, kind, local_inner: false })