Skip to content

Commit 40de40e

Browse files
Noratriebfee1-dead
authored andcommitted
Add typed {Local}DefId for modules
This allows for better type safety in the compiler and also improves the documentation for many things, making it clear that they expect modules.
1 parent cb0c299 commit 40de40e

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
14481448
for id in module.items() {
14491449
check_item_type(tcx, id);
14501450
}
1451-
if module_def_id == CRATE_DEF_ID {
1451+
if module_def_id == LocalModDefId::CRATE_DEF_ID {
14521452
super::entry::check_for_entry_fn(tcx);
14531453
}
14541454
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

+51-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::mir::mono::MonoItem;
6060
use crate::ty::TyCtxt;
6161

6262
use rustc_data_structures::fingerprint::Fingerprint;
63-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
63+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE};
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::{HirId, ItemLocalId, OwnerId};
6666
use rustc_query_system::dep_graph::FingerprintStyle;
@@ -387,3 +387,53 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
387387
}
388388
}
389389
}
390+
391+
macro_rules! impl_for_typed_def_id {
392+
($Name:ident, $LocalName:ident) => {
393+
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $Name {
394+
#[inline(always)]
395+
fn fingerprint_style() -> FingerprintStyle {
396+
FingerprintStyle::DefPathHash
397+
}
398+
399+
#[inline(always)]
400+
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
401+
self.to_def_id().to_fingerprint(tcx)
402+
}
403+
404+
#[inline(always)]
405+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
406+
self.to_def_id().to_debug_str(tcx)
407+
}
408+
409+
#[inline(always)]
410+
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
411+
DefId::recover(tcx, dep_node).map($Name::new_unchecked)
412+
}
413+
}
414+
415+
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $LocalName {
416+
#[inline(always)]
417+
fn fingerprint_style() -> FingerprintStyle {
418+
FingerprintStyle::DefPathHash
419+
}
420+
421+
#[inline(always)]
422+
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
423+
self.to_def_id().to_fingerprint(tcx)
424+
}
425+
426+
#[inline(always)]
427+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
428+
self.to_def_id().to_debug_str(tcx)
429+
}
430+
431+
#[inline(always)]
432+
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
433+
LocalDefId::recover(tcx, dep_node).map($LocalName::new_unchecked)
434+
}
435+
}
436+
};
437+
}
438+
439+
impl_for_typed_def_id! { ModDefId, LocalModDefId }

compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'tcx> EmbargoVisitor<'tcx> {
519519
loop {
520520
let changed_reachability =
521521
self.update_macro_reachable(module_def_id, macro_module_def_id, macro_ev);
522-
if changed_reachability || module_def_id == CRATE_DEF_ID {
522+
if changed_reachability || module_def_id == LocalModDefId::CRATE_DEF_ID {
523523
break;
524524
}
525525
module_def_id = self.tcx.local_parent(module_def_id);

compiler/rustc_span/src/def_id.rs

+95
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ impl CrateNum {
2828
CrateNum::from_usize(x)
2929
}
3030

31+
// FIXME(Nilstrieb): Replace this with `as_mod_def_id`.
3132
#[inline]
3233
pub fn as_def_id(self) -> DefId {
3334
DefId { krate: self, index: CRATE_DEF_INDEX }
3435
}
36+
37+
#[inline]
38+
pub fn as_mod_def_id(self) -> ModDefId {
39+
ModDefId::new_unchecked(DefId { krate: self, index: CRATE_DEF_INDEX })
40+
}
3541
}
3642

3743
impl fmt::Display for CrateNum {
@@ -485,3 +491,92 @@ impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
485491
self.as_def_id().to_stable_hash_key(hcx)
486492
}
487493
}
494+
495+
macro_rules! typed_def_id {
496+
($Name:ident, $LocalName:ident) => {
497+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
498+
pub struct $Name(DefId);
499+
500+
impl $Name {
501+
pub const fn new_unchecked(def_id: DefId) -> Self {
502+
Self(def_id)
503+
}
504+
505+
pub fn to_def_id(self) -> DefId {
506+
self.into()
507+
}
508+
509+
pub fn is_local(self) -> bool {
510+
self.0.is_local()
511+
}
512+
513+
pub fn as_local(self) -> Option<$LocalName> {
514+
self.0.as_local().map($LocalName::new_unchecked)
515+
}
516+
}
517+
518+
impl From<$LocalName> for $Name {
519+
fn from(local: $LocalName) -> Self {
520+
Self(local.0.to_def_id())
521+
}
522+
}
523+
524+
impl From<$Name> for DefId {
525+
fn from(typed: $Name) -> Self {
526+
typed.0
527+
}
528+
}
529+
530+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
531+
pub struct $LocalName(LocalDefId);
532+
533+
impl !Ord for $LocalName {}
534+
impl !PartialOrd for $LocalName {}
535+
536+
impl $LocalName {
537+
pub const fn new_unchecked(def_id: LocalDefId) -> Self {
538+
Self(def_id)
539+
}
540+
541+
pub fn to_def_id(self) -> DefId {
542+
self.0.into()
543+
}
544+
545+
pub fn to_local_def_id(self) -> LocalDefId {
546+
self.0
547+
}
548+
}
549+
550+
impl From<$LocalName> for LocalDefId {
551+
fn from(typed: $LocalName) -> Self {
552+
typed.0
553+
}
554+
}
555+
556+
impl From<$LocalName> for DefId {
557+
fn from(typed: $LocalName) -> Self {
558+
typed.0.into()
559+
}
560+
}
561+
};
562+
}
563+
564+
// N.B.: when adding new typed `DefId`s update the corresponding trait impls in
565+
// `rustc_middle::dep_graph::def_node` for `DepNodeParams`.
566+
typed_def_id! { ModDefId, LocalModDefId }
567+
568+
impl LocalModDefId {
569+
pub const CRATE_DEF_ID: Self = Self::new_unchecked(CRATE_DEF_ID);
570+
}
571+
572+
impl ModDefId {
573+
pub fn is_top_level_module(self) -> bool {
574+
self.0.is_top_level_module()
575+
}
576+
}
577+
578+
impl LocalModDefId {
579+
pub fn is_top_level_module(self) -> bool {
580+
self.0.is_top_level_module()
581+
}
582+
}

0 commit comments

Comments
 (0)