summary refs log tree commit diff
path: root/compiler/rustc_span
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2022-04-15 19:27:53 +0200
committerCamille GILLOT <gillot.camille@gmail.com>2022-04-17 12:14:42 +0200
commit07ee0317638cf1f290a8dace1c7ccc8fea16a236 (patch)
tree059bd5dcde9ee799551fed543e4e8ff474170678 /compiler/rustc_span
parent878c7833f6c1ff10e2fd89074e5bd4ef5ff15936 (diff)
downloadrust-07ee0317638cf1f290a8dace1c7ccc8fea16a236.tar.gz
rust-07ee0317638cf1f290a8dace1c7ccc8fea16a236.zip
Stop using CRATE_DEF_INDEX.
`CRATE_DEF_ID` and `CrateNum::as_def_id` are almost always what we want.
Diffstat (limited to 'compiler/rustc_span')
-rw-r--r--compiler/rustc_span/src/def_id.rs15
1 files changed, 13 insertions, 2 deletions
diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs
index 6d1b36796d8..d5f806308cf 100644
--- a/compiler/rustc_span/src/def_id.rs
+++ b/compiler/rustc_span/src/def_id.rs
@@ -283,8 +283,19 @@ impl DefId {
         self.as_local().unwrap_or_else(|| panic!("DefId::expect_local: `{:?}` isn't local", self))
     }
 
+    #[inline]
+    pub fn is_crate_root(self) -> bool {
+        self.index == CRATE_DEF_INDEX
+    }
+
+    #[inline]
+    pub fn as_crate_root(self) -> Option<CrateNum> {
+        if self.is_crate_root() { Some(self.krate) } else { None }
+    }
+
+    #[inline]
     pub fn is_top_level_module(self) -> bool {
-        self.is_local() && self.index == CRATE_DEF_INDEX
+        self.is_local() && self.is_crate_root()
     }
 }
 
@@ -357,7 +368,7 @@ impl LocalDefId {
 
     #[inline]
     pub fn is_top_level_module(self) -> bool {
-        self.local_def_index == CRATE_DEF_INDEX
+        self == CRATE_DEF_ID
     }
 }