Skip to content

Commit

Permalink
fix some @nospecializes to allow inlining or static dispatch
Browse files Browse the repository at this point in the history
Similar to #43087.
  • Loading branch information
aviatesk committed Nov 20, 2021
1 parent 1799ae6 commit 99cb5e2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
8 changes: 4 additions & 4 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ struct TypeError <: Exception
# that got a bad value.
func::Symbol
context::Union{AbstractString,Symbol}
expected::Type
expected #Type
got
TypeError(func, context, @nospecialize(expected::Type), @nospecialize(got)) =
TypeError(func, context, @nospecialize(expected), @nospecialize(got)) =
new(func, context, expected, got)
end
TypeError(where, @nospecialize(expected::Type), @nospecialize(got)) =
TypeError(Symbol(where), "", expected, got)
TypeError(context, @nospecialize(expected), @nospecialize(got)) =
TypeError(Symbol(context), "", expected, got)
struct InexactError <: Exception
func::Symbol
T # Type
Expand Down
6 changes: 3 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function find_matching_methods(argtypes::Vector{Any}, @nospecialize(atype), meth
mt = ccall(:jl_method_table_for, Any, (Any,), sig_n)
mt === nothing && return FailedMethodMatch("Could not identify method table for call")
mt = mt::Core.MethodTable
matches = findall(sig_n, method_table; limit = max_methods)
matches = find_all_matches(sig_n, method_table; limit = max_methods)
if matches === missing
return FailedMethodMatch("For one of the union split cases, too many methods matched")
end
Expand Down Expand Up @@ -239,7 +239,7 @@ function find_matching_methods(argtypes::Vector{Any}, @nospecialize(atype), meth
return FailedMethodMatch("Could not identify method table for call")
end
mt = mt::Core.MethodTable
matches = findall(atype, method_table; limit = max_methods)
matches = find_all_matches(atype, method_table; limit = max_methods)
if matches === missing
# this means too many methods matched
# (assume this will always be true, so we don't compute / update valid age in this case)
Expand Down Expand Up @@ -1326,7 +1326,7 @@ function abstract_invoke(interp::AbstractInterpreter, (; fargs, argtypes)::ArgIn
types = rewrap_unionall(Tuple{ft, unwrap_unionall(types).parameters...}, types)::Type
nargtype = Tuple{ft, nargtype.parameters...}
argtype = Tuple{ft, argtype.parameters...}
result = findsup(types, method_table(interp))
result = find_supremum_match(types, method_table(interp))
result === nothing && return CallMeta(Any, false)
method, valid_worlds = result
update_valid_age!(sv, valid_worlds)
Expand Down
16 changes: 8 additions & 8 deletions base/compiler/methodtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ CachedMethodTable(table::T) where T =
table)

"""
findall(sig::Type, view::MethodTableView; limit=typemax(Int))
find_all_matches(sig::Type, view::MethodTableView; limit::Int=Int(typemax(Cint)))
Find all methods in the given method table `view` that are applicable to the
given signature `sig`. If no applicable methods are found, an empty result is
returned. If the number of applicable methods exceeded the specified limit,
`missing` is returned.
"""
function findall(@nospecialize(sig::Type), table::InternalMethodTable; limit::Int=typemax(Int))
function find_all_matches(@nospecialize(sig#=::Type=#), table::InternalMethodTable; limit::Int=Int(typemax(Cint)))
_min_val = RefValue{UInt}(typemin(UInt))
_max_val = RefValue{UInt}(typemax(UInt))
_ambig = RefValue{Int32}(0)
Expand All @@ -72,7 +72,7 @@ function findall(@nospecialize(sig::Type), table::InternalMethodTable; limit::In
return MethodLookupResult(ms::Vector{Any}, WorldRange(_min_val[], _max_val[]), _ambig[] != 0)
end

function findall(@nospecialize(sig::Type), table::OverlayMethodTable; limit::Int=typemax(Int))
function find_all_matches(@nospecialize(sig#=::Type=#), table::OverlayMethodTable; limit::Int=Int(typemax(Cint)))
_min_val = RefValue{UInt}(typemin(UInt))
_max_val = RefValue{UInt}(typemax(UInt))
_ambig = RefValue{Int32}(0)
Expand All @@ -91,15 +91,15 @@ function findall(@nospecialize(sig::Type), table::OverlayMethodTable; limit::Int
return MethodLookupResult(ms::Vector{Any}, WorldRange(_min_val[], _max_val[]), _ambig[] != 0)
end

function findall(@nospecialize(sig::Type), table::CachedMethodTable; limit::Int=typemax(Int))
function find_all_matches(@nospecialize(sig#=::Type=#), table::CachedMethodTable; limit::Int=Int(typemax(Cint)))
box = Core.Box(sig)
return get!(table.cache, sig) do
findall(box.contents, table.table; limit=limit)
find_all_matches(box.contents, table.table; limit=limit)
end
end

"""
findsup(sig::Type, view::MethodTableView)::Union{Tuple{MethodMatch, WorldRange}, Nothing}
find_supremum_match(sig::Type, view::MethodTableView)::Union{Tuple{MethodMatch, WorldRange}, Nothing}
Find the (unique) method `m` such that `sig <: m.sig`, while being more
specific than any other method with the same property. In other words, find
Expand All @@ -112,7 +112,7 @@ Such a method `m` need not exist. It is possible that no method is an
upper bound of `sig`, or it is possible that among the upper bounds, there
is no least element. In both cases `nothing` is returned.
"""
function findsup(@nospecialize(sig::Type), table::InternalMethodTable)
function find_supremum_match(@nospecialize(sig#=::Type=#), table::InternalMethodTable)
min_valid = RefValue{UInt}(typemin(UInt))
max_valid = RefValue{UInt}(typemax(UInt))
result = ccall(:jl_gf_invoke_lookup_worlds, Any, (Any, UInt, Ptr{Csize_t}, Ptr{Csize_t}),
Expand All @@ -122,4 +122,4 @@ function findsup(@nospecialize(sig::Type), table::InternalMethodTable)
end

# This query is not cached
findsup(@nospecialize(sig::Type), table::CachedMethodTable) = findsup(sig, table.table)
find_supremum_match(@nospecialize(sig#=::Type=#), table::CachedMethodTable) = find_supremum_match(sig, table.table)
2 changes: 1 addition & 1 deletion base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pairwise_blocksize(::typeof(abs2), ::typeof(+)) = 4096

# handling empty arrays
_empty_reduce_error() = throw(ArgumentError("reducing over an empty collection is not allowed"))
_empty_reduce_error(@nospecialize(f), @nospecialize(T::Type)) = throw(ArgumentError("""
_empty_reduce_error(@nospecialize(f), @nospecialize(T#=::Type=#)) = throw(ArgumentError("""
reducing with $f over an empty collection of element type $T is not allowed.
You may be able to prevent this error by supplying an `init` value to the reducer."""))

Expand Down
30 changes: 14 additions & 16 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -513,23 +513,21 @@ end
# we're attempting to represent.
# Union{T} where T is a degenerate case and is equal to T.ub, but we don't want
# to print them that way, so filter those out from our aliases completely.
function makeproper(io::IO, x::Type)
properx = x
x = unwrap_unionall(x)
function makeproper(io::IO, @nospecialize(x#=::Type=#))
if io isa IOContext
for (key, val) in io.dict
if key === :unionall_env && val isa TypeVar
properx = UnionAll(val, properx)
x = UnionAll(val, x)
end
end
end
has_free_typevars(properx) && return Any
return properx
has_free_typevars(x) && return Any
return x
end

function make_typealias(@nospecialize(x::Type))
Any === x && return
x <: Tuple && return
function make_typealias(@nospecialize(x#=::Type=#))
Any === x && return nothing
x <: Tuple && return nothing
mods = modulesof!(Set{Module}(), x)
Core in mods && push!(mods, Base)
aliases = Tuple{GlobalRef,SimpleVector}[]
Expand Down Expand Up @@ -674,7 +672,7 @@ function show_typealias(io::IO, name::GlobalRef, x::Type, env::SimpleVector, whe
nothing
end

function make_wheres(io::IO, env::SimpleVector, @nospecialize(x::Type))
function make_wheres(io::IO, env::SimpleVector, @nospecialize(x#=::Type=#))
seen = IdSet()
wheres = TypeVar[]
# record things printed by the context
Expand Down Expand Up @@ -704,12 +702,12 @@ function make_wheres(io::IO, env::SimpleVector, @nospecialize(x::Type))
return wheres
end

function show_wheres(io::IO, wheres::Vector)
function show_wheres(io::IO, wheres::Vector{TypeVar})
isempty(wheres) && return
io = IOContext(io)
n = length(wheres)
for i = 1:n
p = wheres[i]::TypeVar
p = wheres[i]
print(io, n == 1 ? " where " : i == 1 ? " where {" : ", ")
show(io, p)
io = IOContext(io, :unionall_env => p)
Expand All @@ -718,7 +716,7 @@ function show_wheres(io::IO, wheres::Vector)
nothing
end

function show_typealias(io::IO, x::Type)
function show_typealias(io::IO, @nospecialize(x#=::Type=#))
properx = makeproper(io, x)
alias = make_typealias(properx)
alias === nothing && return false
Expand All @@ -728,7 +726,7 @@ function show_typealias(io::IO, x::Type)
return true
end

function make_typealiases(@nospecialize(x::Type))
function make_typealiases(@nospecialize(x#=::Type=#))
Any === x && return Core.svec(), Union{}
x <: Tuple && return Core.svec(), Union{}
mods = modulesof!(Set{Module}(), x)
Expand Down Expand Up @@ -879,7 +877,7 @@ function show(io::IO, ::MIME"text/plain", @nospecialize(x::Type))
end

show(io::IO, @nospecialize(x::Type)) = _show_type(io, inferencebarrier(x))
function _show_type(io::IO, @nospecialize(x::Type))
function _show_type(io::IO, @nospecialize(x#=::Type=#))
if print_without_params(x)
show_type_name(io, unwrap_unionall(x).name)
return
Expand Down Expand Up @@ -985,7 +983,7 @@ function show_type_name(io::IO, tn::Core.TypeName)
nothing
end

function show_datatype(io::IO, @nospecialize(x::DataType), wheres::Vector=TypeVar[])
function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
parameters = x.parameters::SimpleVector
istuple = x.name === Tuple.name
n = length(parameters)
Expand Down

0 comments on commit 99cb5e2

Please sign in to comment.