rustc_symbol_mangling/
hashed.rs

1use std::fmt::Write;
2
3use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4use rustc_hashes::Hash64;
5use rustc_hir::def_id::CrateNum;
6use rustc_middle::ty::{Instance, TyCtxt};
7
8use crate::v0;
9
10pub(super) fn mangle<'tcx>(
11    tcx: TyCtxt<'tcx>,
12    instance: Instance<'tcx>,
13    instantiating_crate: Option<CrateNum>,
14    full_mangling_name: impl FnOnce() -> String,
15) -> String {
16    // The symbol of a generic function may be scattered in multiple downstream dylibs.
17    // If the symbol of a generic function still contains `crate name`, hash conflicts between the
18    // generic function and other symbols of the same `crate` cannot be detected in time during
19    // construction. This symbol conflict is left over until it occurs during run time.
20    // In this case, `instantiating-crate name` is used to replace `crate name` can completely
21    // eliminate the risk of the preceding potential hash conflict.
22    let crate_num =
23        if let Some(krate) = instantiating_crate { krate } else { instance.def_id().krate };
24
25    let mut symbol = "_RNxC".to_string();
26    v0::push_ident(tcx.crate_name(crate_num).as_str(), &mut symbol);
27
28    let hash = tcx.with_stable_hashing_context(|mut hcx| {
29        let mut hasher = StableHasher::new();
30        full_mangling_name().hash_stable(&mut hcx, &mut hasher);
31        hasher.finish::<Hash64>().as_u64()
32    });
33
34    push_hash64(hash, &mut symbol);
35
36    symbol
37}
38
39// The hash is encoded based on `base-62` and the final terminator `_` is removed because it does
40// not help prevent hash collisions
41fn push_hash64(hash: u64, output: &mut String) {
42    let hash = v0::encode_integer_62(hash);
43    let hash_len = hash.len();
44    let _ = write!(output, "{hash_len}H{}", &hash[..hash_len - 1]);
45}