Skip to content

Error while printing certain NamedTuple types from @code_warntype #58161

Open
@danielwe

Description

@danielwe

It's possible to write code such that @code_warntype infers things like NamedTuple{(: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> 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorcompiler:inferenceType inferencedisplay and printingAesthetics and correctness of printed representations of objects.types and dispatchTypes, subtyping and method dispatch

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions