diff --git a/crates/oxide_compiler/src/hash.rs b/crates/oxide_compiler/src/hash.rs index 0ecb7887..120361fc 100644 --- a/crates/oxide_compiler/src/hash.rs +++ b/crates/oxide_compiler/src/hash.rs @@ -1,6 +1,27 @@ use oxide_parser::{ ClassElement, Expression, ForStatementInit, ObjectPropertyKind, PropertyKey, SimpleAssignmentTarget, Statement, }; +use std::hash::Hash; + +#[derive(Hash)] +enum HashDomain { + Statement, + Expression, + ClassElement, + PropertyKey, + ObjectPropertyKind, + SimpleAssignmentTarget, +} + +macro_rules! hash_match { + ($domain:expr, $value:expr, $h:expr, { $($arms:tt)* }) => {{ + $domain.hash($h); + std::mem::discriminant($value).hash($h); + match $value { + $($arms)* + } + }}; +} pub fn structural_hash(program: &oxide_parser::Program) -> u64 { use std::hash::Hasher; @@ -15,11 +36,8 @@ pub fn structural_hash(program: &oxide_parser::Program) -> u64 { } fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { - use std::hash::Hash; - - match stmt { + hash_match!(HashDomain::Statement, stmt, h, { Statement::ExpressionStatement(es) => { - 0u8.hash(h); hash_expression(&es.expression, h); } Statement::VariableDeclaration(decl) => { @@ -32,13 +50,11 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } Statement::ReturnStatement(ret) => { - 2u8.hash(h); if let Some(arg) = &ret.argument { hash_expression(arg, h); } } Statement::IfStatement(ifs) => { - 3u8.hash(h); hash_expression(&ifs.test, h); hash_statement(&ifs.consequent, h); if let Some(alt) = &ifs.alternate { @@ -46,12 +62,10 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } Statement::WhileStatement(wh) => { - 4u8.hash(h); hash_expression(&wh.test, h); hash_statement(&wh.body, h); } Statement::ForStatement(fr) => { - 5u8.hash(h); if let Some(init) = &fr.init { if let Some(expr) = init.as_expression() { hash_expression(expr, h); @@ -74,29 +88,21 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { hash_statement(&fr.body, h); } Statement::BlockStatement(block) => { - 6u8.hash(h); for s in &block.body { hash_statement(s, h); } } - Statement::BreakStatement(_) => { - 7u8.hash(h); - } - Statement::ContinueStatement(_) => { - 8u8.hash(h); - } + Statement::BreakStatement(_) => {} + Statement::ContinueStatement(_) => {} Statement::DoWhileStatement(dw) => { - 9u8.hash(h); hash_statement(&dw.body, h); hash_expression(&dw.test, h); } Statement::ForInStatement(fi) => { - 10u8.hash(h); hash_expression(&fi.right, h); hash_statement(&fi.body, h); } Statement::SwitchStatement(sw) => { - 11u8.hash(h); hash_expression(&sw.discriminant, h); for case in &sw.cases { if let Some(test) = &case.test { @@ -108,7 +114,6 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } Statement::FunctionDeclaration(fd) => { - 12u8.hash(h); if let Some(id) = &fd.id { id.name.as_str().hash(h); } @@ -120,7 +125,6 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } Statement::ClassDeclaration(class) => { - 13u8.hash(h); if let Some(id) = &class.id { id.name.as_str().hash(h); } @@ -133,11 +137,9 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } Statement::ThrowStatement(ts) => { - 14u8.hash(h); hash_expression(&ts.argument, h); } Statement::TryStatement(ts) => { - 34u8.hash(h); for s in &ts.block.body { hash_statement(s, h); } @@ -157,41 +159,28 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { } } } - _ => { - std::mem::discriminant(stmt).hash(h); - } - } + _ => {} + }); } fn hash_property_key(key: &PropertyKey, h: &mut rustc_hash::FxHasher) { - use std::hash::Hash; - - match key { + hash_match!(HashDomain::PropertyKey, key, h, { PropertyKey::StaticIdentifier(ident) => { - 0u8.hash(h); ident.name.as_str().hash(h); } PropertyKey::StringLiteral(s) => { - 1u8.hash(h); s.value.hash(h); } PropertyKey::PrivateIdentifier(pi) => { - 2u8.hash(h); pi.name.as_str().hash(h); } - _ => { - 3u8.hash(h); - std::mem::discriminant(key).hash(h); - } - } + _ => {} + }); } fn hash_class_element(element: &ClassElement, h: &mut rustc_hash::FxHasher) { - use std::hash::Hash; - - match element { + hash_match!(HashDomain::ClassElement, element, h, { ClassElement::MethodDefinition(method) => { - 0u8.hash(h); method.r#static.hash(h); method.computed.hash(h); std::mem::discriminant(&method.kind).hash(h); @@ -203,30 +192,22 @@ fn hash_class_element(element: &ClassElement, h: &mut rustc_hash::FxHasher) { } } } - _ => { - 1u8.hash(h); - std::mem::discriminant(element).hash(h); - } - } + _ => {} + }); } fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { - use std::hash::Hash; - - match expr { + hash_match!(HashDomain::Expression, expr, h, { Expression::BinaryExpression(bin) => { - 0u8.hash(h); std::mem::discriminant(&bin.operator).hash(h); hash_expression(&bin.left, h); hash_expression(&bin.right, h); } Expression::UnaryExpression(un) => { - 1u8.hash(h); std::mem::discriminant(&un.operator).hash(h); hash_expression(&un.argument, h); } Expression::CallExpression(call) => { - 2u8.hash(h); (call.arguments.len() as u32).hash(h); hash_expression(&call.callee, h); for arg in &call.arguments { @@ -236,66 +217,41 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { } } Expression::LogicalExpression(log) => { - 3u8.hash(h); std::mem::discriminant(&log.operator).hash(h); hash_expression(&log.left, h); hash_expression(&log.right, h); } Expression::ConditionalExpression(cond) => { - 4u8.hash(h); hash_expression(&cond.test, h); hash_expression(&cond.consequent, h); hash_expression(&cond.alternate, h); } - Expression::Identifier(_) => { - 5u8.hash(h); - } + Expression::Identifier(_) => {} 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); } Expression::AssignmentExpression(assign) => { - 11u8.hash(h); std::mem::discriminant(&assign.operator).hash(h); hash_expression(&assign.right, h); } Expression::UpdateExpression(update) => { - 12u8.hash(h); std::mem::discriminant(&update.operator).hash(h); update.prefix.hash(h); - match &update.argument { - SimpleAssignmentTarget::AssignmentTargetIdentifier(_) => { - 0u8.hash(h); - } - SimpleAssignmentTarget::StaticMemberExpression(member) => { - 1u8.hash(h); - hash_expression(&member.object, h); - } - SimpleAssignmentTarget::ComputedMemberExpression(member) => { - 2u8.hash(h); - hash_expression(&member.object, h); - hash_expression(&member.expression, h); - } - _ => {} - } + hash_simple_assignment_target(&update.argument, h); } Expression::TemplateLiteral(tl) => { - 16u8.hash(h); (tl.quasis.len() as u32).hash(h); for expr in &tl.expressions { hash_expression(expr, h); } } Expression::TaggedTemplateExpression(tt) => { - 17u8.hash(h); hash_expression(&tt.tag, h); (tt.quasi.quasis.len() as u32).hash(h); for expr in &tt.quasi.expressions { @@ -303,14 +259,12 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { } } Expression::ArrowFunctionExpression(arrow) => { - 15u8.hash(h); (arrow.params.items.len() as u32).hash(h); for s in &arrow.body.statements { hash_statement(s, h); } } Expression::FunctionExpression(fe) => { - 13u8.hash(h); (fe.params.items.len() as u32).hash(h); if let Some(body) = &fe.body { for s in &body.statements { @@ -319,7 +273,6 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { } } Expression::ClassExpression(class) => { - 18u8.hash(h); if let Some(id) = &class.id { id.name.as_str().hash(h); } @@ -332,26 +285,12 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { } } Expression::ObjectExpression(obj) => { - 20u8.hash(h); (obj.properties.len() as u32).hash(h); for prop in &obj.properties { - match prop { - ObjectPropertyKind::ObjectProperty(p) => { - 0u8.hash(h); - std::mem::discriminant(&p.kind).hash(h); - p.method.hash(h); - p.computed.hash(h); - hash_property_key(&p.key, h); - hash_expression(&p.value, h); - } - ObjectPropertyKind::SpreadProperty(_) => { - 1u8.hash(h); - } - } + hash_object_property_kind(prop, h); } } Expression::NewExpression(ne) => { - 14u8.hash(h); hash_expression(&ne.callee, h); (ne.arguments.len() as u32).hash(h); for arg in &ne.arguments { @@ -360,17 +299,39 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { } } } - Expression::Super(_) => { - 19u8.hash(h); - } + Expression::Super(_) => {} Expression::RegExpLiteral(lit) => { if let Some(raw) = &lit.raw { raw.to_string().hash(h); } - std::mem::discriminant(expr).hash(h); } - _ => { - std::mem::discriminant(expr).hash(h); - } - } + _ => {} + }); +} + +fn hash_object_property_kind(prop: &ObjectPropertyKind, h: &mut rustc_hash::FxHasher) { + hash_match!(HashDomain::ObjectPropertyKind, prop, h, { + ObjectPropertyKind::ObjectProperty(p) => { + std::mem::discriminant(&p.kind).hash(h); + p.method.hash(h); + p.computed.hash(h); + hash_property_key(&p.key, h); + hash_expression(&p.value, h); + } + ObjectPropertyKind::SpreadProperty(_) => {} + }); +} + +fn hash_simple_assignment_target(target: &SimpleAssignmentTarget, h: &mut rustc_hash::FxHasher) { + hash_match!(HashDomain::SimpleAssignmentTarget, target, h, { + SimpleAssignmentTarget::AssignmentTargetIdentifier(_) => {} + SimpleAssignmentTarget::StaticMemberExpression(member) => { + hash_expression(&member.object, h); + } + SimpleAssignmentTarget::ComputedMemberExpression(member) => { + hash_expression(&member.object, h); + hash_expression(&member.expression, h); + } + _ => {} + }); } diff --git a/crates/oxide_compiler/tests/structural_hash_tests.rs b/crates/oxide_compiler/tests/structural_hash_tests.rs index 4fa1cda6..f7358e19 100644 --- a/crates/oxide_compiler/tests/structural_hash_tests.rs +++ b/crates/oxide_compiler/tests/structural_hash_tests.rs @@ -36,6 +36,13 @@ fn structural_hash_same_shape() { assert_eq!(hash_a, hash_b); } +#[test] +fn structural_hash_identifier_renaming_remains_normalized() { + let hash_a = parse_to_hash("var left = 1; left + left"); + let hash_b = parse_to_hash("var right = 1; right + right"); + assert_eq!(hash_a, hash_b); +} + #[test] fn structural_hash_different_init_values() { let hash_a = parse_to_hash("var x = 1 + 2;"); @@ -88,3 +95,25 @@ fn hash_compound_vs_simple_assign() { let hash_b = parse_to_hash("x+=1"); assert_ne!(hash_a, hash_b, "x=1 and x+=1 must produce different hashes"); } + +#[test] +fn object_literal_data_get_set_shapes_differ() { + let data = parse_to_hash("var o = { x: 1 };"); + let getter = parse_to_hash("var o = { get x() { return 1; } };"); + let setter = parse_to_hash("var o = { set x(v) { this.y = v; } };"); + + assert_ne!(data, getter, "data and getter properties must hash differently"); + assert_ne!(data, setter, "data and setter properties must hash differently"); + assert_ne!(getter, setter, "getter and setter properties must hash differently"); +} + +#[test] +fn class_method_getter_static_setter_shapes_differ() { + let method = parse_to_hash("class A { x() { return 1; } }"); + let getter = parse_to_hash("class A { get x() { return 1; } }"); + let static_setter = parse_to_hash("class A { static set x(v) { this.y = v; } }"); + + assert_ne!(method, getter, "method and getter must hash differently"); + assert_ne!(method, static_setter, "method and static setter must hash differently"); + assert_ne!(getter, static_setter, "getter and static setter must hash differently"); +}