Skip to content

Commit 1c0fe1e

Browse files
authored
Merge pull request #8 from nsajko/infer_return_type
safer access to Julia's type inference
2 parents 80d8be7 + cfa00d3 commit 1c0fe1e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/internals.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ module Internals
22

33
import StableTasks: @spawn, @spawnat, @fetch, @fetchfrom, StableTask, AtomicRef
44

5+
if (
6+
(@isdefined Core) &&
7+
(Core isa Module) &&
8+
isdefined(Core, :Compiler) &&
9+
(Core.Compiler isa Module) &&
10+
isdefined(Core.Compiler, :return_type) &&
11+
applicable(Core.Compiler.return_type, identity, Tuple{})
12+
)
13+
infer_return_type(@nospecialize f::Any) = Core.Compiler.return_type(f, Tuple{})
14+
else
15+
# safe conservative fallback to `Any`, which is subtyped by each type
16+
infer_return_type(@nospecialize f::Any) = Any
17+
end
18+
519
Base.getindex(r::AtomicRef) = @atomic r.x
620
Base.setindex!(r::AtomicRef{T}, x) where {T} = @atomic r.x = convert(T, x)
721

@@ -96,7 +110,7 @@ macro spawn(args...)
96110
quote
97111
let $(letargs...)
98112
f = $thunk
99-
T = Core.Compiler.return_type(f, Tuple{})
113+
T = infer_return_type(f)
100114
ref = AtomicRef{T}()
101115
f_wrap = () -> (ref[] = f(); nothing)
102116
task = Task(f_wrap)
@@ -136,7 +150,7 @@ macro spawnat(thrdid, ex)
136150
end
137151
let $(letargs...)
138152
thunk = $thunk
139-
RT = Core.Compiler.return_type(thunk, Tuple{})
153+
RT = infer_return_type(thunk)
140154
ret = AtomicRef{RT}()
141155
thunk_wrap = () -> (ret[] = thunk(); nothing)
142156
local task = Task(thunk_wrap)

0 commit comments

Comments
 (0)