Repository: JuliaLang/julia
File: Compiler/src/verifytrim.jl (Julia)

Code before:
    color = get(io, :color, false)::Bool
    if !color
        print(io, typ)
    elseif typ === Union{}
        printstyled(io, typ; color=:red)
    elseif typ == Core.Box
        printstyled(io, typ; color=:red, bold=true)
    elseif typ isa Union && is_expected_union(typ)
        printstyled(io, typ; color=:yellow)
    elseif isdispatchelem(typ)
        printstyled(io, typ; color=:light_black)
    else
        printstyled(io, typ; color=:red, bold=true)
    end
end

# Print with light_black if stable, normal otherwise
function print_styled(io::IO, @nospecialize(x), stable::Bool)
    stable ? printstyled(io, x; color=:light_black) : print(io, x)
end

# Check if we should elide the type annotation for a statement
function should_elide_type(newstmt, @nospecialize(typ))
    newstmt isa Expr && newstmt.head ∈ (:quote, :inert) && return true
    newstmt isa GlobalRef && isdispatchelem(typ) && return true
    newstmt isa Union{Int, UInt8, UInt16, UInt32, UInt64, Float16, Float32, Float64, String, QuoteNode} && return true
    newstmt isa Callable && return true
    return false
end

# Unwrap SSAValue and PiNode to get the underlying statement
function unwrap_stmt(codeinfo::CodeInfo, @nospecialize(stmt))
    while true
        if stmt isa SSAValue
            stmt = codeinfo.code[stmt.id]
            isexpr(stmt, :(=)) && (stmt = stmt.args[2])
        elseif stmt isa PiNode
            stmt = stmt.val
        else
            return stmt
        end
    end
end

# Convert Core.Argument to slot name symbol if available
function argument_name(codeinfo::CodeInfo, arg::Core.Argument)
    slotnames = codeinfo.slotnames
    if slotnames !== nothing && arg.n <= length(slotnames)
        return slotnames[arg.n]
    end
    return arg

Review comment (suggestion):
This is a really random set of types - Any justification for it?

I thought this might be either:
  - Base types that already print their type (e.g. `Float16`) or are supported as literals
  - Objects that are self-quoting

but it doesn't seem to match either

Code after:
    end
end


# Print a value with optional stable coloring (light_black if stable)
function print_value(io::IO, @nospecialize(x), stable::Bool)
    stable ? printstyled(io, x; color=:light_black) : print(io, x)
end

# Unwrap SSAValue and PiNode to get the underlying statement
function unwrap_stmt(codeinfo::CodeInfo, @nospecialize(stmt))
    while true
        if stmt isa SSAValue
            stmt = codeinfo.code[stmt.id]
            isexpr(stmt, :(=)) && (stmt = stmt.args[2])
        elseif stmt isa PiNode
            stmt = stmt.val
        else
            return stmt
        end
    end
end

# Convert Core.Argument to slot name symbol if available
function argument_name(codeinfo::CodeInfo, arg::Core.Argument)
    slotnames = codeinfo.slotnames
    if slotnames !== nothing && arg.n <= length(slotnames)
        return slotnames[arg.n]
    end
    return arg
end

const MAX_NESTING_DEPTH = 1

function is_call_expr(codeinfo::CodeInfo, @nospecialize(stmt))
    stmt = unwrap_stmt(codeinfo, stmt)
    return stmt isa Expr && stmt.head ∈ (:call, :invoke, :foreigncall, :new)
end

function has_unstable_arg(codeinfo::CodeInfo, sptypes::Vector{VarState}, args, startidx::Int)
    for i in (startidx + 1):length(args)
        is_type_stable(widenconst(argextype(args[i], codeinfo, sptypes))) || return true
    end
    return false
end

function print_value(io::IO, codeinfo::CodeInfo, @nospecialize(stmt), stable::Bool)
    stmt = unwrap_stmt(codeinfo, stmt)
    if stmt isa GlobalRef
        # TODO: This is not correct.
        print_value(io, stmt.name, stable)