Skip to content

Commit 6d353e4

Browse files
fix: clear cached hash in setproperties
1 parent 79da1be commit 6d353e4

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/types.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ function ConstructionBase.setproperties(obj::BasicSymbolic{T}, patch::NamedTuple
9797
# Call outer constructor because hash consing cannot be applied in inner constructor
9898
@compactified obj::BasicSymbolic begin
9999
Sym => Sym{T}(nt_new.name; nt_new...)
100-
Term => Term{T}(nt_new.f, nt_new.arguments; nt_new...)
101-
Add => Add(T, nt_new.coeff, nt_new.dict; nt_new...)
102-
Mul => Mul(T, nt_new.coeff, nt_new.dict; nt_new...)
103-
Div => Div{T}(nt_new.num, nt_new.den, nt_new.simplified; nt_new...)
104-
Pow => Pow{T}(nt_new.base, nt_new.exp; nt_new...)
100+
Term => Term{T}(nt_new.f, nt_new.arguments; nt_new..., hash = RefValue(UInt(0)))
101+
Add => Add(T, nt_new.coeff, nt_new.dict; nt_new..., hash = RefValue(UInt(0)))
102+
Mul => Mul(T, nt_new.coeff, nt_new.dict; nt_new..., hash = RefValue(UInt(0)))
103+
Div => Div{T}(nt_new.num, nt_new.den, nt_new.simplified; nt_new..., hash = RefValue(UInt(0)))
104+
Pow => Pow{T}(nt_new.base, nt_new.exp; nt_new..., hash = RefValue(UInt(0)))
105105
_ => Unityper.rt_constructor(obj){T}(;nt_new...)
106106
end
107107
end

test/basics.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using SymbolicUtils: Symbolic, Sym, FnType, Term, Add, Mul, Pow, symtype, operation, arguments, issym, isterm, BasicSymbolic, term, isequal_with_metadata
22
using SymbolicUtils
3+
using ConstructionBase: setproperties
34
using IfElse: ifelse
45
using Setfield
56
using Test, ReferenceTests
@@ -393,3 +394,28 @@ end
393394
@test ax === x
394395
@test isequal(adjoint(y), conj(y))
395396
end
397+
398+
@testset "`setproperties` clears hash" begin
399+
@syms a b c
400+
hash(a)
401+
hash(b)
402+
hash(c)
403+
@test hash(a) != hash(setproperties(a; name = :A))
404+
function foo end
405+
function bar end
406+
var = term(foo, [a, b]; type = Real)
407+
hash(var)
408+
@test hash(var) != hash(setproperties(var; f = bar))
409+
var = a + b
410+
hash(var)
411+
@test hash(var) != hash(setproperties(var; coeff = 2))
412+
var = a * b
413+
hash(var)
414+
@test hash(var) != hash(setproperties(var; coeff = 2))
415+
var = a / b
416+
hash(var)
417+
@test hash(var) != hash(setproperties(var; num = c))
418+
var = a ^ b
419+
hash(var)
420+
@test hash(var) != hash(setproperties(var; exp = c))
421+
end

0 commit comments

Comments
 (0)