From 99cb5e2315db796c986dc926d55cc700e907ede1 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Tue, 16 Nov 2021 10:46:34 +0900 Subject: [PATCH] fix some `@nospecialize`s to allow inlining or static dispatch Similar to #43087. --- base/boot.jl | 8 +++---- base/compiler/abstractinterpretation.jl | 6 ++--- base/compiler/methodtable.jl | 16 ++++++------- base/reduce.jl | 2 +- base/show.jl | 30 ++++++++++++------------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/base/boot.jl b/base/boot.jl index b9de755ed8125..3c1c9be6b4367 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -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 diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index b1d4eceefc2cf..95e18bcdcc919 100644 --- a/base/compiler/abstractinterpretation.jl +++ b/base/compiler/abstractinterpretation.jl @@ -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 @@ -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) @@ -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) diff --git a/base/compiler/methodtable.jl b/base/compiler/methodtable.jl index 93020ae6a2639..d0143a5f854e2 100644 --- a/base/compiler/methodtable.jl +++ b/base/compiler/methodtable.jl @@ -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) @@ -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) @@ -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 @@ -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}), @@ -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) diff --git a/base/reduce.jl b/base/reduce.jl index 0581a72fb2862..0a20c973e9bf1 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -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.""")) diff --git a/base/show.jl b/base/show.jl index 4997ecffa9082..95f0ef0ca52bf 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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}[] @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 @@ -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)