# @code\_warntype does not warn about Array{Any}

**URL:** <https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191>\
**Category:** General Usage\
**Created:** [March 15, 2021, 12:00pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191 "2021-03-15T12:00:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 15, 2021, 12:00pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/1 "2021-03-15T12:00:08Z")

</div>

Shouldn’t `@code_warntype` flag (in red) the fact that the return value of this function is an array of `Any`? The macro notes the `Any`, but does not make it red.

```julia
struct T
    x::Int32
    y::Int32
end

function test_warntype()
    x = []
    for i in 1:2
       push!(x, T(i,i))
    end
    return x
end

```

```julia
julia> @code_warntype test_warntype()
Variables
  #self#::Core.Compiler.Const(test_warntype, false)
  x::Array{Any,1}
  @_3::Union{Nothing, Tuple{Int64,Int64}}
  i::Int64

Body::Array{Any,1}
1 ─ (x = Base.vect())
│ %2 = (1:2)::Core.Compiler.Const(1:2, false)
│ (@_3 = Base.iterate(%2))
│ %4 = (@_3::Core.Compiler.Const((1, 1), false) === nothing)::Core.Compiler.Const(false, false)
│ %5 = Base.not_int(%4)::Core.Compiler.Const(true, false)
└── goto #4 if not %5
2 ┄ %7 = @_3::Tuple{Int64,Int64}::Tuple{Int64,Int64}
│ (i = Core.getfield(%7, 1))
│ %9 = Core.getfield(%7, 2)::Int64
│ %10 = x::Array{Any,1} # it is Any but is green ??
│ %11 = Main.T(i, i)::T
│ Main.push!(%10, %11)
│ (@_3 = Base.iterate(%2, %9))
│ %14 = (@_3 === nothing)::Bool
│ %15 = Base.not_int(%14)::Bool
└── goto #4 if not %15
3 ─ goto #2
4 ┄ return x

```

(the example reproduces a common mistake of not initializing the array as `T[]`).

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 15, 2021, 12:30pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/2 "2021-03-15T12:30:10Z")

</div>

The function is type stable and the return type is correctly inferred to `Vector{Any}`, so why should it warn?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 15, 2021, 12:35pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/3 "2021-03-15T12:35:07Z")

</div>

I am not sure, that is why I am asking. In this particular case putting a red flag on that `Any` would allow the identification of a common mistake which is simple to address, with important performance implications.

Perhaps the question should be if any `Any` should be highlighted somehow? For example in the minimal, but artificial example,

```julia
julia> function f()
          x = Any[1,2,3]
       end
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Array{Any,1}

Body::Array{Any,1}
1 ─ %1 = Base.getindex(Main.Any, 1, 2, 3)::Array{Any,1}
│ (x = %1)
└── return %1

```

It wouldn’t harm if the `Any` were yellow, or purple…

---

<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:** [March 15, 2021, 1:27pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/4 "2021-03-15T13:27:17Z")

</div>

The problem is that cases where the user is explicitly asking for `Array{Any,1}` (like both examples you posted) and cases where inference says “ok, this is the best I can do” have to be distinguished. It’s perfectly valid to have `x = []`, if you already know beforehand that you’ll push large amounts of differently typed objects into that array. If those intended uses were all marked red, it would add to the noise.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 15, 2021, 1:38pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/5 "2021-03-15T13:38:49Z")

</div>

> [@Sukera](#):
>
> If those intended uses were all marked red, it would add to the noise.

I agree. Maybe bold, at least 🙂 - I don’t know which is the color palete available. We already get harmless `Union{...,nothing}` in `for` loops in yellow (true is that causes confusion to new people).

Maybe even for experienced users, having all the non-concrete types highlighted somehow will not be bad.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 15, 2021, 1:54pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/6 "2021-03-15T13:54:23Z")

</div>

> [@lmiq](#):
>
> Maybe even for experienced users, having all the non-concrete types highlighted somehow will not be bad.

That’s the point, though-- `Array{Any}` _is_ a concrete type. If there is a type instability later on, it will come when a user tries to access an element of that array, which `@code_warntype` will correctly warn about already:

```julia
julia> function f()
         x = []
         push!(x, 1)
         push!(x, 2)
         return x[2]
       end
f (generic function with 1 method)

julia> @code_warntype f()
Variables
  #self#::Core.Compiler.Const(f, false)
  x::Array{Any,1}

Body::Any
1 ─ (x = Base.vect())
│ Main.push!(x, 1)
│ Main.push!(x, 2)
│ %4 = Base.getindex(x, 2)::Any
└── return %4

julia> 

```

`x` is correctly marked as `Array{Any}` and not highlighted because it is a concrete type and is in fact exactly what the user asked for. But `return x[2]` cannot be inferred, so it is marked as `Any` and highlighted in red.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 15, 2021, 2:00pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/7 "2021-03-15T14:00:42Z")

</div>

Ah, I see. We didn’t do nothing with those `Any` elements except containing them. Probably if that is a problem later in the code the type instability would be detected somewhere else.

Thus, reformulating the question. Isn’t a good idea to somewhat highlight (even if moderately) containers of mixed or abstract types?

---

<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:** [March 15, 2021, 2:14pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/8 "2021-03-15T14:14:17Z")

</div>

I think this would be a good place to use yellow (or a new color purple?) highlighting. This is a really common new user trap, so making it more visible would be good.

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [March 15, 2021, 3:03pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/9 "2021-03-15T15:03:33Z")

</div>

I think Unions are marked in yellow. Similar to this, explicitly wished-for unperformant types could also be marked in yellow. Don’t know if the benefits from having this warning would outweigh the visual noise but it could very well be.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 17, 2021, 7:36am UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/10 "2021-03-17T07:36:27Z")

</div>

I think that the main problem is that new users may not be aware that by using `[]` or similar, they are _explicitly asking for_ `Any[]`.

If this was Julia pre-1.0, I would suggest disallowing `[]` (ie forcing users to use `Any[]`), and it is maybe something to consider for 2.0, but given the current situation highlighting could be a mitigation.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 17, 2021, 8:54am UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/11 "2021-03-17T08:54:51Z")

</div>

I tried something like that pre-1.0: [JuliaLang/julia#24974](https://github.com/JuliaLang/julia/pull/24974), [JuliaLang/julia#25032](https://github.com/JuliaLang/julia/pull/25032).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 17, 2021, 1:00pm UTC](https://discourse.julialang.org/t/code-warntype-does-not-warn-about-array-any/57191/12 "2021-03-17T13:00:06Z")

</div>

In retrospect I think these were dismissed a bit too hastily. Generally, if an argument has a default (here, types defaulting to `Any`), it should ideally be the most frequently used value, not the least common. But given that it was discussed I am hesitant to open an issue about it.

In any case, I think that cases of relying on these (eg `[]`) is a good candidate for being highlighted by a linter and/or discouraged in coding style guides. My impression is that neither [BlueStyle](https://github.com/invenia/BlueStyle) nor [YASGuide](https://github.com/jrevels/YASGuide) mention it.
