Skip to content

@code_warntype shows the following code is type unstable #49970

@atbug

Description

@atbug

Running the following code with julia test.jl shows type instability, but I believe the code is type stable.

test.jl

using LinearAlgebra
using InteractiveUtils

function hey(q)
    A = kron([1 1; 1 1], [1 1; 1 1])
    B = kron([1 1; 1 1], [1 0; 0 1])
    if norm(q) > 1.0
        return A
    else
        return A - B
    end
end

function foo(f)
    return f.([[1.0, 1.0], [1.0, 0.0]])
end

# hey([1.0, 0.0])

@code_warntype foo(hey)

The output is

MethodInstance for foo(::typeof(hey))
  from foo(f) @ Main /path/to/test.jl:14
Arguments
  #self#::Core.Const(foo)
  f::Core.Const(hey)
Body::Any
1 ─ %1 = Base.vect(1.0, 1.0)::Vector{Float64}
│   %2 = Base.vect(1.0, 0.0)::Vector{Float64}
│   %3 = Base.vect(%1, %2)::Vector{Vector{Float64}}
│   %4 = Base.broadcasted(f, %3)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(hey), Tuple{Vector{Vector{Float64}}}}
│   %5 = Base.materialize(%4)::Any
└──      return %5

However, uncomment the line hey([1.0, 0.0]) fix the issue.

This has been tested on 1.9.0, 1.8.5 and 1.6.7.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions