fix: 修复字节码缓存未存值导致错误命中问题

This commit is contained in:
爱特每羊队
2026-06-06 14:30:31 +08:00
parent 1615f739d1
commit bca9f8995b
3 changed files with 43 additions and 2 deletions
+20
View File
@@ -23,6 +23,11 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) {
Statement::VariableDeclaration(decl) => {
1u8.hash(h);
(decl.declarations.len() as u32).hash(h);
for d in &decl.declarations {
if let Some(init) = &d.init {
hash_expression(init, h);
}
}
}
Statement::ReturnStatement(ret) => {
2u8.hash(h);
@@ -108,6 +113,21 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) {
hash_expression(&cond.consequent, h);
hash_expression(&cond.alternate, h);
}
Expression::Identifier(_) => {
5u8.hash(h);
}
Expression::NumericLiteral(num) => {
6u8.hash(h);
num.value.to_bits().hash(h);
}
Expression::StringLiteral(s) => {
7u8.hash(h);
s.value.hash(h);
}
Expression::BooleanLiteral(b) => {
8u8.hash(h);
b.value.hash(h);
}
_ => {
std::mem::discriminant(expr).hash(h);
}
+22 -1
View File
@@ -103,10 +103,31 @@ fn structural_hash_hit() {
#[test]
fn structural_hash_same_shape() {
let hash_a = parse_to_hash("var x = 1 + 2; var y = x;");
let hash_b = parse_to_hash("var a = 3 + 4; var b = a;");
let hash_b = parse_to_hash("var a = 1 + 2; var b = a;");
assert_eq!(hash_a, hash_b);
}
#[test]
fn structural_hash_different_init_values() {
let hash_a = parse_to_hash("var x = 1 + 2;");
let hash_b = parse_to_hash("var y = 3 + 4;");
assert_ne!(hash_a, hash_b);
}
#[test]
fn structural_hash_different_string_literals() {
let hash_a = parse_to_hash(r#"var x = "hello";"#);
let hash_b = parse_to_hash(r#"var x = "world";"#);
assert_ne!(hash_a, hash_b);
}
#[test]
fn structural_hash_different_booleans() {
let hash_a = parse_to_hash("var x = true;");
let hash_b = parse_to_hash("var x = false;");
assert_ne!(hash_a, hash_b);
}
#[test]
fn structural_hash_different_shape() {
let hash_a = parse_to_hash("1 + 2");
+1 -1
View File
@@ -61,7 +61,7 @@ mod tests {
let a1 = Allocator::default();
let a2 = Allocator::default();
let p1 = oxide_parser::parse(&a1, "var x = 1; var y = 2;").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "var a = 3; var b = 4;").expect("parse failed");
let p2 = oxide_parser::parse(&a2, "var a = 1; var b = 2;").expect("parse failed");
assert_eq!(structural_hash(&p1), structural_hash(&p2));
}