Hello Julia Community,
I’ve just came across a small code example which isn’t type stable according to @code_warntype
but clearly should be, at least according to my understanding of refs.
Here is the example:
julia> struct Foo
bar :: Ref{Int}
end
julia> increment(foo) = foo.bar[] += 1
increment (generic function with 1 method)
julia> @code_warntype increment(Foo(Ref(0)))
Variables
#self#::Core.Const(increment)
foo::Foo
Body::Any
1 ─ %1 = Base.getproperty(foo, :bar)::Ref{Int64}
│ %2 = Base.getindex(%1)::Any
│ %3 = (%2 + 1)::Any
│ %4 = Base.getproperty(foo, :bar)::Ref{Int64}
│ Base.setindex!(%4, %3)
└── return %3
Is this intended and a misunderstanding on my side or this an unintended bug?
Using @edit getindex(Ref(0))
shows that the definition of getindex doesnt specialize on the eltype.
So I could just roll my own Ref implementation, but is this really intended? Was this a decision to reduce compilation times?
(I was using 1.6.0 but it still seems to be there in 1.7.2)