# Cthulhu and setindex!

**URL:** <https://discourse.julialang.org/t/cthulhu-and-setindex/104238>\
**Category:** General Usage\
**Tags:** code\_warntype, type-stability, cthulhu\
**Created:** [September 25, 2023, 6:01pm UTC](https://discourse.julialang.org/t/cthulhu-and-setindex/104238 "2023-09-25T18:01:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [September 25, 2023, 6:01pm UTC](https://discourse.julialang.org/t/cthulhu-and-setindex/104238/1 "2023-09-25T18:01:55Z")

</div>

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
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
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?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 25, 2023, 6:42pm UTC](https://discourse.julialang.org/t/cthulhu-and-setindex/104238/2 "2023-09-25T18:42:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 25, 2023, 7:18pm UTC](https://discourse.julialang.org/t/cthulhu-and-setindex/104238/3 "2023-09-25T19:18:54Z")

</div>

> [@Oscar\_Smith](#):
>
> `Cthulhu` should probably do a better job of explaining this though.

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

```julia
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.
