# Code\_warntype errors on a function

**URL:** <https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 8, 2025, 10:42am UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171 "2025-09-08T10:42:11Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [September 8, 2025, 10:42am UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/1 "2025-09-08T10:42:11Z")

</div>

I am wondering if the following is a bug, or something I am missing. MWE:

```julia
struct Foo{T}
    a::T
    b::T
end

function struct2nt(x::T) where T
    @assert isstructtype(T)
    N = fieldnames(T)
    NamedTuple{N}(getfield(x, n) for n in N)
end

struct2nt(Foo(2, 3)) # works fine

@code_warntype struct2nt(Foo(2, 3)) # errors

```

On 1.11 and 1.12rc, errors with

```julia
julia> @code_warntype struct2nt(Foo(2, 3))
MethodInstance for struct2nt(::Foo{Int64})
  from struct2nt(x::T) where T @ Main REPL[1]:1
Static Parameters
  T = Foo{Int64}
Arguments
  #self#::Core.Const(Main.struct2nt)
  x::Foo{Int64}
Locals
  #3::var"#3#4"{Foo{Int64}}
  N::Tuple{Symbol, Symbol}
BodyERROR: BoundsError: attempt to access Core.SimpleVector at index [2]
Stacktrace:
  [1] getindex
    @ ./essentials.jl:932 [inlined]
  [2] show_at_namedtuple(io::IOContext{IOBuffer}, syms::Tuple{Symbol, Symbol}, types::DataType)
    @ Base ./show.jl:1191
  [3] show_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar})
    @ Base ./show.jl:1167
  [4] _show_type(io::IOBuffer, x::Type)
    @ Base ./show.jl:1007
  [5] show(io::IOBuffer, x::Type)
    @ Base ./show.jl:965
  [6] print(io::IOBuffer, x::Type)
    @ Base ./strings/io.jl:35
  [7] print_to_string(::String, ::Vararg{Any})
    @ Base ./strings/io.jl:148
  [8] string
    @ ./strings/io.jl:189 [inlined]
  [9] warntype_type_printer(io::Base.TTY; type::Any, used::Bool, show_type::Bool, ::@Kwargs{})
    @ InteractiveUtils ~/.julia/juliaup/julia-1.11.6+0.x64.linux.gnu/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:32
 [10] code_warntype(io::Base.TTY, f::Any, t::Any; debuginfo::Symbol, optimize::Bool, kwargs::@Kwargs{})
    @ InteractiveUtils ~/.julia/juliaup/julia-1.11.6+0.x64.linux.gnu/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:151
 [11] code_warntype(io::Base.TTY, f::Any, t::Any)
    @ InteractiveUtils ~/.julia/juliaup/julia-1.11.6+0.x64.linux.gnu/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:77
 [12] code_warntype(::Any, ::Vararg{Any}; kwargs...)
    @ InteractiveUtils ~/.julia/juliaup/julia-1.11.6+0.x64.linux.gnu/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:159
 [13] top-level scope
    @ REPL[5]:1

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 8, 2025, 12:25pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/2 "2025-09-08T12:25:45Z")

</div>

I’d expect it to work, and based on it managing to print `Body`, it’s going wrong at the inferred return type here:

> <https://github.com/JuliaLang/julia/blob/9615af0f269df4d371b8010e9507ed5bae86103b/stdlib/InteractiveUtils/src/codeview.jl#L151>

Thing is I haven’t been able to find the exact call that goes wrong. The assumption is:

```julia-auto
julia> InteractiveUtils.warntype_type_printer(stdout;type=typeof(struct2nt(Foo(2,3))), used=true)
::@NamedTuple{a::Int64, b::Int64}

```

which works fine. I tried Debugger.jl on `@code_warntype` and the expanded call, but trying to advance by 1 step on much earlier calls apparently jumps to executing the `code_warntype` call, which of course errors.

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [September 8, 2025, 12:31pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/3 "2025-09-08T12:31:24Z")

</div>

I’ve found out the following: The function

```julia-auto
function Base.show_at_namedtuple(io::IO, syms::Tuple, types::DataType)
    first = true
    for i in eachindex(syms)
        if !first
            print(io, ", ")
        end
        print(io, syms[i])
        typ = types.parameters[i] # here the error occurs
        if typ !== Any
            print(io, "::")
            show(io, typ)
        end
        first = false
    end
end

```

is called with `types` set to `NTuple{N, Int}`. I guess this is a fragment of some `NTuple{N, Int} where N`.

---

<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:** [September 8, 2025, 1:15pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/4 "2025-09-08T13:15:30Z")

</div>

I tried the devious

```julia
global X = nothing

function Base.show(io::IO, @nospecialize(x::Type))
    try
        Base._show_type(io, Base.inferencebarrier(x))
    catch e
        global X = x
        rethrow(e)
    end
end

```

and indeed there seems to be something missing (`X` is unprintable):

```julia
julia> X.name
typename(NamedTuple)

julia> X.parameters
svec((:a, :b), NTuple{N, Int64})

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [September 8, 2025, 1:20pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/5 "2025-09-08T13:20:00Z")

</div>

Still strange because that `SimpleVector` DOES have `index [2]`:

```julia-auto
julia> Core.svec((:a, :b), (NTuple{N, Int64} where N).body)[2]
NTuple{N, Int64}

```

It goes wrong in the 2nd iteration’s type:

```julia-auto
julia> Base.show_at_namedtuple(stdout, (:a,:b), (NTuple{N, Int64} where N).body)
a::Vararg{Int64, N}, bERROR: BoundsError: attempt to access Core.SimpleVector at index [2]
Stacktrace:
 [1] getindex
   @ .\essentials.jl:932 [inlined]
 [2] show_at_namedtuple(io::Base.TTY, syms::Tuple{Symbol, Symbol}, types::DataType)
   @ Base .\show.jl:1191

```

---

<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:** [September 8, 2025, 1:26pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/6 "2025-09-08T13:26:43Z")

</div>

It appears to work fine in 1.6, so I guess we should be able to bisect it. (I don’t have time right now, so if anyone starts doing it, please let me know so I don’t duplicate the effort)

---

<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:** [September 8, 2025, 3:24pm UTC](https://discourse.julialang.org/t/code-warntype-errors-on-a-function/132171/7 "2025-09-08T15:24:21Z")

</div>

I found an existing issue already:

> <https://github.com/JuliaLang/julia/issues/58161>
>
> It's possible to write code such that \`@code\_warntype\` infers things like \`Named…Tuple{(:a, :b),Tuple{Vararg{T}}}\`, even though such a type can't be constructed directly. When the printing of this type reaches \`show\_at\_namedtuple\`, it breaks the assumption that \`length(types.parameters) == length(syms)\`. The result is that \`@code\_warntype\` throws in the middle of printing its output.
> 
> MWE:
> 
> \`\`\`julia-repl
> julia\> f(::Val{vars}, mat) where {vars} = (; zip(vars, eachrow(mat))...)
> f (generic function with 1 method)
> 
> julia\> f(Val((:a, :b)), rand(2, 1))
> (a = \[0.6820962296161969\], b = \[0.6568417470938385\])
> 
> julia\> @code\_warntype f(Val((:a, :b)), rand(2, 1))
> MethodInstance for f(::Val{(:a, :b)}, ::Matrix{Float64})
> from f(::Val{vars}, mat) where vars @ Main REPL\[1\]:1
> Static Parameters
> vars = (:a, :b)
> Arguments
> #self#::Core.Const(Main.f)
> \_::Core.Const(Val{(:a, :b)}())
> mat::Matrix{Float64}
> BodyERROR: BoundsError: attempt to access Core.SimpleVector at index \[2\]
> Stacktrace:
> \[1\] getindex
> @ ./essentials.jl:932 \[inlined\]
> \[2\] show\_at\_namedtuple(io::IOContext{IOBuffer}, syms::Tuple{Symbol, Symbol}, types::DataType)
> @ Base ./show.jl:1191
> \[3\] show\_datatype(io::IOContext{IOBuffer}, x::DataType, wheres::Vector{TypeVar})
> @ Base ./show.jl:1167
> \[4\] \_show\_type(io::IOBuffer, x::Type)
> @ Base ./show.jl:1007
> \[5\] show(io::IOBuffer, x::Type)
> @ Base ./show.jl:965
> \[6\] print(io::IOBuffer, x::Type)
> @ Base ./strings/io.jl:35
> \[7\] print\_to\_string(::String, ::Vararg{Any})
> @ Base ./strings/io.jl:148
> \[8\] string
> @ ./strings/io.jl:189 \[inlined\]
> \[9\] warntype\_type\_printer(io::Base.TTY; type::Any, used::Bool, show\_type::Bool, ::@Kwargs{})
> @ InteractiveUtils ~/.julia/juliaup/julia-1.11.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:32
> \[10\] code\_warntype(io::Base.TTY, f::Any, t::Any; debuginfo::Symbol, optimize::Bool, kwargs::@Kwargs{})
> @ InteractiveUtils ~/.julia/juliaup/julia-1.11.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:151
> \[11\] code\_warntype(io::Base.TTY, f::Any, t::Any)
> @ InteractiveUtils ~/.julia/juliaup/julia-1.11.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:77
> \[12\] code\_warntype(::Any, ::Vararg{Any}; kwargs...)
> @ InteractiveUtils ~/.julia/juliaup/julia-1.11.5+0.aarch64.apple.darwin14/share/julia/stdlib/v1.11/InteractiveUtils/src/codeview.jl:159
> \[13\] top-level scope
> @ REPL\[3\]:1
> \`\`\`
