Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Is there a way to apply constant folding with `nameof` for a simple `UnionAll`?

$
0
0

nameof(::UnionAll) does not perform constant folding.

julia> @code_typed nameof(Complex{Float32})
CodeInfo(
1 ─     return :Complex
) => Symbol

julia> @code_typed nameof(Complex)
CodeInfo(
1 ─       nothing::Nothing
2 ┄ %2  = φ (#1 => _2, #3 => %6)::Any
│   %3  = (%2 isa Base.UnionAll)::Bool
└──       goto #4 if not %3
3 ─ %5  = π (%2, UnionAll)
│   %6  = Base.getfield(%5, :body)::Any
└──       goto #2
4 ─       goto #5
5 ─ %9  = Base.nameof(%2)::Any
│         Core.typeassert(%9, Base.Symbol)::Symbol
│   %11 = π (%9, Symbol)
└──       return %11
) => Symbol

At first I thought it was because @nospecialize was specified in Base.unwrap_unionall, but that doesn’t seem to be the case. Of course, the body field of UnionAll is boxed, so it should require special handling.

7 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795