Skip to content

Commit 0d6338e

Browse files
fix: handle explicit ::Any annotation in @cache macro
1 parent 30b0487 commit 0d6338e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/cache.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ macro cache(args...)
245245
argname, Texpr = arg.args
246246
push!(argexprs, argname)
247247

248+
if Texpr == :Any
249+
# if the type is `Any`, branch on it being a `BasicSymbolic`
250+
push!(keyexprs, :($argname isa BasicSymbolic ? $SymbolicKey(objectid($argname)) : $argname))
251+
push!(keytypes, Any)
252+
continue
253+
end
254+
248255
# handle Union types that may contain a `BasicSymbolic`
249256
if Meta.isexpr(Texpr, :curly) && Texpr.args[1] == :Union
250257
Texprs = Texpr.args[2:end]

test/cache_macro.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,31 @@ end
100100
return 2x + 1
101101
end
102102

103-
@testset "::Any" begin
103+
@cache function f3_2(x::Any)::Union{BasicSymbolic, Int}
104+
return 2x + 1
105+
end
106+
107+
@testset "$name" for (name, fn) in [("implicit ::Any", f3), ("explicit ::Any", f3_2)]
104108
@syms x
105-
val = f3(x)
109+
val = fn(x)
106110
@test isequal(val, 2x + 1)
107-
cachestruct = associated_cache(f3)
111+
cachestruct = associated_cache(fn)
108112
cache, stats = cachestruct.tlv[]
109113
@test cache isa Dict{Tuple{Any}, Union{BasicSymbolic, Int}}
110114
@test length(cache) == 1
111115
@test cache[(SymbolicKey(objectid(x)),)] === val
112116
@test stats.hits == 0
113117
@test stats.misses == 1
114-
f3(x)
118+
fn(x)
115119
@test stats.hits == 1
116120
@test stats.misses == 1
117121

118-
val = f3(3)
122+
val = fn(3)
119123
@test val == 7
120124
@test length(cache) == 2
121125
@test stats.misses == 2
122126

123-
clear_cache!(f3)
127+
clear_cache!(fn)
124128
@test length(cache) == 0
125129
@test stats.hits == stats.misses == stats.clears == 0
126130
end

0 commit comments

Comments
 (0)