From b03e982ceeff83103fca2de7c387fba0608047aa Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Sat, 6 Jun 2026 14:30:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E7=A0=81=E7=BC=93=E5=AD=98=E6=9C=AA=E5=AD=98=E5=80=BC=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E9=94=99=E8=AF=AF=E5=91=BD=E4=B8=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_compiler/src/hash.rs | 20 ++++++++++++++++ crates/oxide_compiler/tests/compiler_tests.rs | 23 ++++++++++++++++++- crates/oxide_kernel/src/code_forge.rs | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/oxide_compiler/src/hash.rs b/crates/oxide_compiler/src/hash.rs index 92f158c0..96e7909d 100644 --- a/crates/oxide_compiler/src/hash.rs +++ b/crates/oxide_compiler/src/hash.rs @@ -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); } diff --git a/crates/oxide_compiler/tests/compiler_tests.rs b/crates/oxide_compiler/tests/compiler_tests.rs index 83ce8003..d49281c1 100644 --- a/crates/oxide_compiler/tests/compiler_tests.rs +++ b/crates/oxide_compiler/tests/compiler_tests.rs @@ -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"); diff --git a/crates/oxide_kernel/src/code_forge.rs b/crates/oxide_kernel/src/code_forge.rs index 9d7bdf28..61b45992 100644 --- a/crates/oxide_kernel/src/code_forge.rs +++ b/crates/oxide_kernel/src/code_forge.rs @@ -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)); }