From 7cc81f87254eaa0857ce15c9eaa6558359e464a7 Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Tue, 11 Feb 2025 15:13:05 +0100 Subject: [PATCH] add a missing type assert to `OncePerTask` callable also test that the difference `OncePerX` infer --- base/lock.jl | 4 +++- test/threads.jl | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/base/lock.jl b/base/lock.jl index 869e61599b443..79a2d1491bc00 100644 --- a/base/lock.jl +++ b/base/lock.jl @@ -933,4 +933,6 @@ mutable struct OncePerTask{T, F} <: Function OncePerTask{T,F}(initializer::F) where {T, F} = new{T,F}(initializer) OncePerTask(initializer) = new{Base.promote_op(initializer), typeof(initializer)}(initializer) end -@inline (once::OncePerTask)() = get!(once.initializer, task_local_storage(), once) +@inline function (once::OncePerTask{T})() where {T} + get!(once.initializer, task_local_storage(), once)::T +end diff --git a/test/threads.jl b/test/threads.jl index a1da408252fc4..52d0546f0e31b 100644 --- a/test/threads.jl +++ b/test/threads.jl @@ -439,7 +439,7 @@ end let once = OncePerProcess(() -> return [nothing]) @test typeof(once) <: OncePerProcess{Vector{Nothing}} - x = once() + x = @inferred once() @test x === once() @atomic once.state = 0xff @test_throws ErrorException("invalid state for OncePerProcess") once() @@ -456,7 +456,7 @@ let e = Base.Event(true), started = Channel{Int16}(Inf), finish = Channel{Nothing}(Inf), exiting = Channel{Nothing}(Inf), - starttest2 = Event(), + starttest2 = Base.Event(), once = OncePerThread() do push!(started, threadid()) take!(finish) @@ -468,7 +468,7 @@ let e = Base.Event(true), @test typeof(once) <: OncePerThread{Vector{Nothing}} push!(finish, nothing) @test_throws ArgumentError once[0] - x = once() + x = @inferred once() @test_throws ArgumentError once[0] @test x === once() === fetch(@async once()) === once[threadid()] @test take!(started) == threadid() @@ -558,7 +558,7 @@ end let once = OncePerTask(() -> return [nothing]) @test typeof(once) <: OncePerTask{Vector{Nothing}} - x = once() + x = @inferred once() @test x === once() !== fetch(@async once()) delete!(task_local_storage(), once) @test x !== once() === once()