Skip to content

Commit a2b77d0

Browse files
authored
Rollup merge of rust-lang#147075 - Lysxia:no-panic-def-path-hash, r=petrochenkov
Make `def_path_hash_to_def_id` not panic when passed an invalid hash I'm using this function in a third-party application (Creusot) to access private items (by reverse engineering their hash). This works in the happy path, but it panics when an item does not exist. There is no way to hack it downstream because the hook `def_path_hash_to_def_id_extern` must always return a `DefId` and its implementation uses `def_path_hash_to_def_index` which is internal and which is where the panic happens.
2 parents 848009f + c0e0d4b commit a2b77d0

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ impl<'a> CrateMetadataRef<'a> {
15551555
}
15561556

15571557
#[inline]
1558-
fn def_path_hash_to_def_index(self, hash: DefPathHash) -> DefIndex {
1558+
fn def_path_hash_to_def_index(self, hash: DefPathHash) -> Option<DefIndex> {
15591559
self.def_path_hash_map.def_path_hash_to_def_index(&hash)
15601560
}
15611561

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,8 @@ fn provide_cstore_hooks(providers: &mut Providers) {
691691
.get(&stable_crate_id)
692692
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"));
693693
assert_ne!(cnum, LOCAL_CRATE);
694-
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
695-
DefId { krate: cnum, index: def_index }
694+
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash)?;
695+
Some(DefId { krate: cnum, index: def_index })
696696
};
697697

698698
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ pub(crate) enum DefPathHashMapRef<'tcx> {
1212

1313
impl DefPathHashMapRef<'_> {
1414
#[inline]
15-
pub(crate) fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
15+
pub(crate) fn def_path_hash_to_def_index(
16+
&self,
17+
def_path_hash: &DefPathHash,
18+
) -> Option<DefIndex> {
1619
match *self {
17-
DefPathHashMapRef::OwnedFromMetadata(ref map) => {
18-
map.get(&def_path_hash.local_hash()).unwrap()
19-
}
20+
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(&def_path_hash.local_hash()),
2021
DefPathHashMapRef::BorrowedFromTcx(_) => {
2122
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
2223
}

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ declare_hooks! {
7777
/// session, if it still exists. This is used during incremental compilation to
7878
/// turn a deserialized `DefPathHash` into its current `DefId`.
7979
/// Will fetch a DefId from a DefPathHash for a foreign crate.
80-
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
80+
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> Option<DefId>;
8181

8282
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
8383
/// can just link to the upstream crate and therefore don't need a mono item.

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,7 @@ impl<'tcx> TyCtxt<'tcx> {
20122012
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
20132013
Some(self.untracked.definitions.read().local_def_path_hash_to_def_id(hash)?.to_def_id())
20142014
} else {
2015-
Some(self.def_path_hash_to_def_id_extern(hash, stable_crate_id))
2015+
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
20162016
}
20172017
}
20182018

0 commit comments

Comments
 (0)