Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore method count after redefinition to hide old definition #57837

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Compiler/test/invalidation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ begin
# this redefinition below should invalidate the cache of `pr48932_callee` but not that of `pr48932_caller`
pr48932_callee(x) = (print(GLOBAL_BUFFER, x); nothing)

@test length(Base.methods(pr48932_callee)) == 2
@test Base.only(Base.methods(pr48932_callee, Tuple{Any})) === first(Base.methods(pr48932_callee))
@test length(Base.methods(pr48932_callee)) == 1
@test Base.only(Base.methods(pr48932_callee, Tuple{Any})) === only(Base.methods(pr48932_callee))
@test isempty(Base.specializations(Base.only(Base.methods(pr48932_callee, Tuple{Any}))))
let mi = only(Base.specializations(Base.only(Base.methods(pr48932_caller))))
# Base.method_instance(pr48932_callee, (Any,))
Expand Down
37 changes: 25 additions & 12 deletions base/runtime_internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1348,31 +1348,44 @@ function MethodList(mt::Core.MethodTable)
return MethodList(ms, mt)
end

function matches_to_methods(ms::Array{Any,1}, mt::Core.MethodTable, mod)
# Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
ms = Method[(ms[i]::Core.MethodMatch).method for i in 1:length(ms)]
# Remove shadowed methods with identical type signatures
prev = nothing
filter!(ms) do m
l = prev
repeated = (l isa Method && m.sig == l.sig)
prev = m
return !repeated
end
# Remove methods not part of module (after removing shadowed methods)
mod === nothing || filter!(ms) do m
return parentmodule(m) ∈ mod
end
return MethodList(ms, mt)
end

"""
methods(f, [types], [module])

Return the method table for `f`.

If `types` is specified, return an array of methods whose types match.
If `module` is specified, return an array of methods defined in that module.
A list of modules can also be specified as an array.
A list of modules can also be specified as an array or set.

!!! compat "Julia 1.4"
At least Julia 1.4 is required for specifying a module.

See also: [`which`](@ref), [`@which`](@ref Main.InteractiveUtils.@which) and [`methodswith`](@ref Main.InteractiveUtils.methodswith).
"""
function methods(@nospecialize(f), @nospecialize(t),
mod::Union{Tuple{Module},AbstractArray{Module},Nothing}=nothing)
mod::Union{Tuple{Module},AbstractArray{Module},AbstractSet{Module},Nothing}=nothing)
world = get_world_counter()
world == typemax(UInt) && error("code reflection cannot be used from generated functions")
# Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
ms = Method[]
for m in _methods(f, t, -1, world)::Vector
m = m::Core.MethodMatch
(mod === nothing || parentmodule(m.method) ∈ mod) && push!(ms, m.method)
end
MethodList(ms, typeof(f).name.mt)
ms = _methods(f, t, -1, world)::Vector{Any}
return matches_to_methods(ms, typeof(f).name.mt, mod)
end
methods(@nospecialize(f), @nospecialize(t), mod::Module) = methods(f, t, (mod,))

Expand All @@ -1382,12 +1395,12 @@ function methods_including_ambiguous(@nospecialize(f), @nospecialize(t))
world == typemax(UInt) && error("code reflection cannot be used from generated functions")
min = RefValue{UInt}(typemin(UInt))
max = RefValue{UInt}(typemax(UInt))
ms = _methods_by_ftype(tt, nothing, -1, world, true, min, max, Ptr{Int32}(C_NULL))::Vector
return MethodList(Method[(m::Core.MethodMatch).method for m in ms], typeof(f).name.mt)
ms = _methods_by_ftype(tt, nothing, -1, world, true, min, max, Ptr{Int32}(C_NULL))::Vector{Any}
return matches_to_methods(ms, typeof(f).name.mt, nothing)
end

function methods(@nospecialize(f),
mod::Union{Module,AbstractArray{Module},Nothing}=nothing)
mod::Union{Module,AbstractArray{Module},AbstractSet{Module},Nothing}=nothing)
# return all matches
return methods(f, Tuple{Vararg{Any}}, mod)
end
Expand Down
2 changes: 1 addition & 1 deletion test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7691,7 +7691,7 @@ end
# issue #31696
foo31696(x::Int8, y::Int8) = 1
foo31696(x::T, y::T) where {T <: Int8} = 2
@test length(methods(foo31696)) == 2
@test length(methods(foo31696)) == 1
let T1 = Tuple{Int8}, T2 = Tuple{T} where T<:Int8, a = T1[(1,)], b = T2[(1,)]
b .= a
@test b[1] == (1,)
Expand Down
2 changes: 1 addition & 1 deletion test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ fLargeTable() = 4
fLargeTable(::Union, ::Union) = "a"
@test fLargeTable(Union{Int, Missing}, Union{Int, Missing}) == "a"
fLargeTable(::Union, ::Union) = "b"
@test length(methods(fLargeTable)) == 206
@test length(methods(fLargeTable)) == 205
@test fLargeTable(Union{Int, Missing}, Union{Int, Missing}) == "b"

# issue #15280
Expand Down