Skip to content

Commit dbb4257

Browse files
committed
feat: add hash field for SerFunction
1 parent a6512d5 commit dbb4257

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/functions/serialization.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use rustc_middle::ty::TyCtxt;
22
use rustc_span::source_map::SourceMap;
3+
use rustc_stable_hash::{FromStableHash, SipHasher128Hash, StableHasher, hashers::SipHasher128};
34
use serde::Serialize;
45
use stable_mir::{CrateDef, DefId, mir::mono::Instance};
6+
use std::hash::Hasher;
57

68
/// A Rust funtion with its file source, attributes, and raw function content.
79
#[derive(Debug, Serialize)]
810
pub struct SerFunction {
11+
hash: String,
912
/// DefId in stable_mir.
1013
def_id: String,
1114
/// Every funtion must be declared in a specific file, even for those
@@ -22,15 +25,40 @@ pub struct SerFunction {
2225

2326
impl SerFunction {
2427
pub fn new(fun: super::Function, tcx: TyCtxt, src_map: &SourceMap) -> Self {
25-
SerFunction {
26-
def_id: format_def_id(&fun.def_id),
27-
file: fun.file,
28-
attrs: fun.attrs.iter().map(|a| a.as_str().to_owned()).collect(),
29-
func: fun.func,
30-
callees: fun.callees.iter().map(|x| Callee::new(x, tcx, src_map)).collect(),
31-
}
28+
let def_id = format_def_id(&fun.def_id);
29+
let file = fun.file;
30+
let attrs: Vec<_> = fun.attrs.iter().map(|a| a.as_str().to_owned()).collect();
31+
let func = fun.func;
32+
let callees: Vec<_> = fun.callees.iter().map(|x| Callee::new(x, tcx, src_map)).collect();
33+
34+
// Hash
35+
let mut hasher = StableHasher::<SipHasher128>::new();
36+
hasher.write_str(&file);
37+
hasher.write_str(&func);
38+
hasher.write_length_prefix(attrs.len());
39+
attrs.iter().for_each(|a| hasher.write_str(a));
40+
hasher.write_length_prefix(callees.len());
41+
callees.iter().for_each(|c| {
42+
hasher.write_str(&c.file);
43+
hasher.write_str(&c.func);
44+
});
45+
let Hash128(hash) = hasher.finish();
46+
47+
SerFunction { hash, def_id, file, attrs, func, callees }
48+
}
49+
}
50+
51+
// ************* hash *************
52+
struct Hash128(String);
53+
54+
impl FromStableHash for Hash128 {
55+
type Hash = SipHasher128Hash;
56+
57+
fn from(SipHasher128Hash([a, b]): SipHasher128Hash) -> Hash128 {
58+
Hash128(format!("{a}{b}"))
3259
}
3360
}
61+
// ************* hash *************
3462

3563
fn format_def_id(def_id: &DefId) -> String {
3664
format!("{def_id:?}")

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
#![feature(rustc_private, let_chains, hash_set_entry)]
1+
#![feature(rustc_private, let_chains, hash_set_entry, hasher_prefixfree_extras)]
22

33
extern crate rustc_driver;
44
extern crate rustc_hir;
55
extern crate rustc_interface;
66
extern crate rustc_middle;
77
extern crate rustc_smir;
88
extern crate rustc_span;
9+
extern crate rustc_stable_hash;
910
extern crate stable_mir;
1011

1112
use eyre::{Context, Ok};

0 commit comments

Comments
 (0)