Skip to content

Commit 10c7bd2

Browse files
committed
check that hashing of types does not foreigncall (jl_type_hash is concrete evaluated) (#58401)
(cherry picked from commit 86a3e1a)
1 parent 1628ac8 commit 10c7bd2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

base/hashing.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ hash(x::Any) = hash(x, zero(UInt))
2929
hash(w::WeakRef, h::UInt) = hash(w.value, h)
3030

3131
# Types can't be deleted, so marking as total allows the compiler to look up the hash
32-
hash(T::Type, h::UInt) = hash_uint(3h - @assume_effects :total ccall(:jl_type_hash, UInt, (Any,), T))
32+
@noinline _jl_type_hash(T::Type) = @assume_effects :total ccall(:jl_type_hash, UInt, (Any,), T)
33+
hash(T::Type, h::UInt) = hash_uint(3h - _jl_type_hash(T))
3334

3435
## hashing general objects ##
3536

test/hashing.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,10 @@ struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end
303303
@test hash(5//3) == hash(big(5)//3)
304304
end
305305

306-
@test Core.Compiler.is_foldable_nothrow(Base.infer_effects(hash, Tuple{Type{Int}, UInt}))
306+
@testset "concrete eval type hash" begin
307+
@test Core.Compiler.is_foldable_nothrow(Base.infer_effects(hash, Tuple{Type{Int}, UInt}))
308+
309+
f(h...) = hash(Char, h...);
310+
src = only(code_typed(f, Tuple{UInt}))[1]
311+
@test count(stmt -> Meta.isexpr(stmt, :foreigncall), src.code) == 0
312+
end

0 commit comments

Comments
 (0)