Skip to content

Only inherit local hash for paths #142903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ impl DefKey {
pub(crate) fn compute_stable_hash(&self, parent: DefPathHash) -> DefPathHash {
let mut hasher = StableHasher::new();

parent.hash(&mut hasher);
// The new path is in the same crate as `parent`, and will contain the stable_crate_id.
// Therefore, we only need to include information of the parent's local hash.
parent.local_hash().hash(&mut hasher);

let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data;

Expand Down Expand Up @@ -361,8 +363,16 @@ impl Definitions {
},
};

let parent_hash = DefPathHash::new(stable_crate_id, Hash64::ZERO);
let def_path_hash = key.compute_stable_hash(parent_hash);
// We want *both* halves of a DefPathHash to depend on the crate-id of the defining crate.
// The crate-id can be more easily changed than the DefPath of an item, so, in the case of
// a crate-local DefPathHash collision, the user can simply "roll the dice again" for all
// DefPathHashes in the crate by changing the crate disambiguator (e.g. via bumping the
// crate's version number).
//
// Children paths will only hash the local portion, and still inherit the change to the
// root hash.
let def_path_hash =
DefPathHash::new(stable_crate_id, Hash64::new(stable_crate_id.as_u64()));

// Create the root definition.
let mut table = DefPathTable::new(stable_crate_id);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn def_path_hash_depends_on_crate_id() {
assert_ne!(h0.local_hash(), h1.local_hash());

fn mk_test_hash(stable_crate_id: StableCrateId) -> DefPathHash {
let parent_hash = DefPathHash::new(stable_crate_id, Hash64::ZERO);
let parent_hash =
DefPathHash::new(stable_crate_id, Hash64::new(stable_crate_id.as_u64()));

let key = DefKey {
parent: None,
Expand Down
18 changes: 15 additions & 3 deletions compiler/rustc_span/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl DefPathHash {

/// Builds a new [DefPathHash] with the given [StableCrateId] and
/// `local_hash`, where `local_hash` must be unique within its crate.
#[inline]
pub fn new(stable_crate_id: StableCrateId, local_hash: Hash64) -> DefPathHash {
DefPathHash(Fingerprint::new(stable_crate_id.0, local_hash))
}
Expand Down Expand Up @@ -404,21 +405,21 @@ rustc_data_structures::define_id_collections!(
impl<CTX: HashStableContext> HashStable<CTX> for DefId {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
hcx.def_path_hash(*self).hash_stable(hcx, hasher);
}
}

impl<CTX: HashStableContext> HashStable<CTX> for LocalDefId {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
hcx.def_path_hash(self.to_def_id()).local_hash().hash_stable(hcx, hasher);
}
}

impl<CTX: HashStableContext> HashStable<CTX> for CrateNum {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
self.as_def_id().to_stable_hash_key(hcx).stable_crate_id().hash_stable(hcx, hasher);
}
}

Expand Down Expand Up @@ -464,30 +465,36 @@ macro_rules! typed_def_id {
pub struct $Name(DefId);

impl $Name {
#[inline]
pub const fn new_unchecked(def_id: DefId) -> Self {
Self(def_id)
}

#[inline]
pub fn to_def_id(self) -> DefId {
self.into()
}

#[inline]
pub fn is_local(self) -> bool {
self.0.is_local()
}

#[inline]
pub fn as_local(self) -> Option<$LocalName> {
self.0.as_local().map($LocalName::new_unchecked)
}
}

impl From<$LocalName> for $Name {
#[inline]
fn from(local: $LocalName) -> Self {
Self(local.0.to_def_id())
}
}

impl From<$Name> for DefId {
#[inline]
fn from(typed: $Name) -> Self {
typed.0
}
Expand All @@ -500,26 +507,31 @@ macro_rules! typed_def_id {
impl !PartialOrd for $LocalName {}

impl $LocalName {
#[inline]
pub const fn new_unchecked(def_id: LocalDefId) -> Self {
Self(def_id)
}

#[inline]
pub fn to_def_id(self) -> DefId {
self.0.into()
}

#[inline]
pub fn to_local_def_id(self) -> LocalDefId {
self.0
}
}

impl From<$LocalName> for LocalDefId {
#[inline]
fn from(typed: $LocalName) -> Self {
typed.0
}
}

impl From<$LocalName> for DefId {
#[inline]
fn from(typed: $LocalName) -> Self {
typed.0.into()
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/inline/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn f(g: impl Fn()) {
#[inline(always)]
fn g() {
// CHECK-LABEL: fn g(
// CHECK-NOT: (inlined f::<fn() {main}>)
// CHECK-NOT: inlined
f(main);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/symbol-names/basic.legacy.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: symbol-name(_ZN5basic4main17hc88b9d80a69d119aE)
error: symbol-name(_ZN5basic4main17h1dddcfd03744167fE)
--> $DIR/basic.rs:8:1
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(basic::main::hc88b9d80a69d119a)
error: demangling(basic::main::h1dddcfd03744167f)
--> $DIR/basic.rs:8:1
|
LL | #[rustc_symbol_name]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/symbol-names/issue-60925.legacy.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17hbddb77d6f71afb32E)
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h4b3099ec5dc5d306E)
--> $DIR/issue-60925.rs:21:9
|
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::hbddb77d6f71afb32)
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h4b3099ec5dc5d306)
--> $DIR/issue-60925.rs:21:9
|
LL | #[rustc_symbol_name]
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/thir-print/thir-tree-match.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ body:
did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo)
variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }]
flags: IS_ENUM
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 }
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 13397682652773712997 }
args: []
variant_index: 0
subpatterns: [
Expand All @@ -108,7 +108,7 @@ body:
did: DefId(0:3 ~ thir_tree_match[fcf8]::Bar)
variants: [VariantDef { def_id: DefId(0:4 ~ thir_tree_match[fcf8]::Bar::First), ctor: Some((Const, DefId(0:5 ~ thir_tree_match[fcf8]::Bar::First::{constructor#0}))), name: "First", discr: Relative(0), fields: [], tainted: None, flags: }, VariantDef { def_id: DefId(0:6 ~ thir_tree_match[fcf8]::Bar::Second), ctor: Some((Const, DefId(0:7 ~ thir_tree_match[fcf8]::Bar::Second::{constructor#0}))), name: "Second", discr: Relative(1), fields: [], tainted: None, flags: }, VariantDef { def_id: DefId(0:8 ~ thir_tree_match[fcf8]::Bar::Third), ctor: Some((Const, DefId(0:9 ~ thir_tree_match[fcf8]::Bar::Third::{constructor#0}))), name: "Third", discr: Relative(2), fields: [], tainted: None, flags: }]
flags: IS_ENUM
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 10333377570083945360 }
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 7908585036048874241 }
args: []
variant_index: 0
subpatterns: []
Expand Down Expand Up @@ -156,7 +156,7 @@ body:
did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo)
variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }]
flags: IS_ENUM
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 }
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 13397682652773712997 }
args: []
variant_index: 0
subpatterns: [
Expand Down Expand Up @@ -208,7 +208,7 @@ body:
did: DefId(0:10 ~ thir_tree_match[fcf8]::Foo)
variants: [VariantDef { def_id: DefId(0:11 ~ thir_tree_match[fcf8]::Foo::FooOne), ctor: Some((Fn, DefId(0:12 ~ thir_tree_match[fcf8]::Foo::FooOne::{constructor#0}))), name: "FooOne", discr: Relative(0), fields: [FieldDef { did: DefId(0:13 ~ thir_tree_match[fcf8]::Foo::FooOne::0), name: "0", vis: Restricted(DefId(0:0 ~ thir_tree_match[fcf8])), safety: Safe, value: None }], tainted: None, flags: }, VariantDef { def_id: DefId(0:14 ~ thir_tree_match[fcf8]::Foo::FooTwo), ctor: Some((Const, DefId(0:15 ~ thir_tree_match[fcf8]::Foo::FooTwo::{constructor#0}))), name: "FooTwo", discr: Relative(1), fields: [], tainted: None, flags: }]
flags: IS_ENUM
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 3477539199540094892 }
repr: ReprOptions { int: None, align: None, pack: None, flags: , field_shuffle_seed: 13397682652773712997 }
args: []
variant_index: 1
subpatterns: []
Expand Down
Loading