From 2c0678a52b3e6a353b4bf99dd66cbc63028b239e Mon Sep 17 00:00:00 2001 From: Andy Dienes Date: Thu, 19 Mar 2026 16:21:02 -0400 Subject: [PATCH] Fix Base.hash to use only the two-arg method Generated as part of an ecosystem-wide audit for one-arg hash methods. `Base.hash` should only be extended via the two-arg method `hash(x, h::UInt)`. Defining a one-arg `hash(x)` method, or giving the second argument a default value, can lead to correctness bugs (hash contract violations when the seed differs from the hard-coded default) and invalidation-related performance issues. This is particularly necessary for Julia 1.13+ where the default hash seed value will change. Co-Authored-By: Claude Opus 4.6 --- src/mesh/cell.jl | 2 +- src/node.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/cell.jl b/src/mesh/cell.jl index efd7191f..a47181bb 100644 --- a/src/mesh/cell.jl +++ b/src/mesh/cell.jl @@ -77,7 +77,7 @@ const Facet=Cell ### Cell methods -Base.hash(cell::Cell) = sum(hash(node) for node in cell.nodes) +Base.hash(cell::Cell, h::UInt) = foldl((h, node) -> hash(node, h), cell.nodes; init=h) Base.isequal(c1::Cell, c2::Cell) = hash(c1)==hash(c2) """ diff --git a/src/node.jl b/src/node.jl index 65e4da48..9e9dadf4 100644 --- a/src/node.jl +++ b/src/node.jl @@ -98,7 +98,7 @@ const null_Node = Node(NaN, NaN, NaN) #Base.hash(n::Node) = hash( (round(n.coord.x, digits=8), round(n.coord.y, digits=8), round(n.coord.z, digits=8)) ) # Base.hash(n::Node) = hash( (n.coord.x, n.coord.y, n.coord.z) ) -Base.hash(n::Node) = hash( (n.coord.x+1.0, n.coord.y+2.0, n.coord.z+3.0) ) # 1,2,3 aim to avoid clash in some arrays of nodes. +Base.hash(n::Node, h::UInt) = hash((n.coord.x+1.0, n.coord.y+2.0, n.coord.z+3.0), h) # 1,2,3 aim to avoid clash in some arrays of nodes. Base.isequal(n1::Node, n2::Node) = hash(n1)==hash(n2)