Cthulhu and setindex!

Hi everyone,
for today a very simple question: why does Cthulhu seemingly shows
this question is so basic that I feel like I must be missing something.

julia> function overwrite!(arr)
           arr[1] = zero(eltype(arr))
           return arr
       end
overwrite! (generic function with 1 method)

This function is obviously not type unstable, but Cthulhu reports that setindex! returns Any.

julia> @descend overwrite!(zeros(3))
overwrite!(arr) @ Main REPL[3]:1
1 function overwrite!(arr::Vector{Float64})::Vector{Float64}
2     arr::Vector{Float64}[1] = zero(eltype(arr::Vector{Float64})::Type{Float64})::Float64
3     return arr::Vector{Float64}
4 end
Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.
Toggles: [w]arn, [h]ide type-stable statements, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native, [j]ump to source always.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
 • eltype(arr::Vector{Float64})
   zero(eltype(arr::Vector{Float64})::Type{Float64})
   %3 = setindex!(::Vector{Float64},::Float64,::Int64)::Any
   ↩

overwrite!(arr) @ Main REPL[3]:1
1 function overwrite!(arr::Vector{Float64})::Vector{Float64}
2     arr::Vector{Float64}[1] = zero(eltype(arr::Vector{Float64})::Type{Float64})::Float64
3     return arr::Vector{Float64}
4 end
Select a call to descend into or ↩ to ascend. [q]uit. [b]ookmark.
Toggles: [w]arn, [h]ide type-stable statements, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native, [j]ump to source always.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
 • eltype(arr::Vector{Float64})
   zero(eltype(arr::Vector{Float64})::Type{Float64})
   %3 = setindex!(::Vector{Float64},::Float64,::Int64)::Any
   ↩

@code_warntype doesnt show anything (as expected)
Is this expected, or a bug?

1 Like

as an optimization julia doesn’t infer types for results that are unused. Cthulhu should probably do a better job of explaining this though.

7 Likes

Both the compiler & Cthulhu explain that, they just don’t show it by default. Turning on remarks on typed code:

overwrite!(arr) @ Main REPL[2]:1
Variables
  #self#::Core.Const(overwrite!)
  arr::Vector{Float64}

∘ ─ %0 = invoke overwrite!(::Vector{Float64})::Vector{Float64}
    @ REPL[2]:2 within `overwrite!`
1 ─ %1 = Main.eltype(arr)::Core.Const(Float64) [constprop] No more information to be gained
│   %2 = Main.zero(%1)::Core.Const(0.0) [constprop] No more information to be gained
│        Base.setindex!(arr, %2, 1)::Any Call result type was widened because the return value is unused [constprop] Disabled by function heuristic
│   @ REPL[2]:3 within `overwrite!`
└──      return arr

If the remark says the result was widened to Any intentionally due to being unused, Cthulhu IMO just shouldn’t show it in the pretty high level overview at all.

7 Likes