Hi, new to Julia, still working stuff out. I get the same feedback from code_warntype in a larger code base, and can reproduce it with the below.
I executed the below via: ~/julia/julia --color=yes -O0 -g 2 test.jl
I understood it was a mistake to use code_warntype with optimised. The output of my code (see below at the base of the code) is next where the first two Any’s are in red (I can’t change the colour in the post):
Body::Any
18 1 ─ %1 = (Base.getfield)(x, f)::Any │
└── return %1 │
CodeInfo(
18 1 ─ %1 = (Base.getfield)(x, f)::Any │
└── return %1 │
) => Any
Int64
My question simply is should I expect output in red and the type to be Any? I had thought that with the code as below the compiler knew at compile time the types. In my larger code I also get a similar report from code_warntype and was wondered whether as I’m new to Julia I’ve miss-understood the type system and not provided enough type info for the dispatch to be fast and type stable. Could anyone tell me if I should be concerned with this output (red=type unstable was my understanding)?
Interestingly if you remove MMatrix the Any below goes to a Union, still in red however (again, can’t reproduce that here in the post):
Body::Union{Bool, Int64, Point2D, String}
18 1 ─ %1 = (Base.getfield)(x, f)::Union{Bool, Int64, Point2D, String} │
└── return %1 │
I suppose my central question is that as a newby I’ve been writing units tests, basically printing type information with things like code_warntype of relatively complex collections of composite types and looking to assure the compiler knows all the types and is type stable: I don’t know if I should be ignoring this and it’s just to be expected for a type of this nature, or I should be looking at it and fixing something…help really appreciated.
General comments also welcome if you spot something unrelated to my central question.
Thanks,
Andy
module test
using InteractiveUtils: @code_warntype, @code_typed, @code_lowered
using StaticArrays
struct Point2D <: FieldVector{2, Float64}
x::Float64
y::Float64
end
Zero(p::Point2D) = Point2D((0.0, 0.0))
# {P} here because it could be a Point1D/Point2D/Point3D
mutable struct Vert{P}
index::Int64
pos::P
previousPos::P
meta::String
active::Bool
lhs::MMatrix{2, 2, Float64}
# many other fields
Vert{P}(i::Int64, p::P) where {P} = new(i, p, Zero(p), "", true, zeros(MMatrix{2, 2, Float64}))
end
const Vert2D = Vert{Point2D}
v = Vert2D(1, Point2D(1.0, 2.0))
const Verts = Vector{Vert2D}
verts = Verts()
push!(verts, v)
@code_warntype verts[end].index
println(@code_typed verts[end].index)
println(typeof(verts[end].index))
end