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

add subtypes() for UnionAll #20084

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 21 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,25 @@ function _subtypes(m::Module, x::DataType, sts=Set{DataType}(), visited=Set{Modu
for s in names(m, true)
if isdefined(m, s) && !isdeprecated(m, s)
t = getfield(m, s)
if isa(t, DataType) && t.name.name == s && supertype(t).name == x.name
if isa(t, DataType) && supertype(t) == x
ti = typeintersect(t, x)
ti != Bottom && push!(sts, ti)
elseif isa(t, UnionAll) && isa(unwrap_unionall(t), DataType)
ti = typeintersect(t, x)
ti != Bottom && supertype(ti) == x && push!(sts, ti)
elseif isa(t, Module) && !in(t, visited)
_subtypes(t, x, sts, visited)
end
end
end
return sts
end
function _subtypes(m::Module, x::UnionAll, sts=Set{UnionAll}(), visited=Set{Module}())
push!(visited, m)
for s in names(m, true)
if isdefined(m, s) && !isdeprecated(m, s)
t = getfield(m, s)
if isa(t, UnionAll) && isa(unwrap_unionall(t), DataType) && supertype(t) == x
ti = typeintersect(t, x)
ti != Bottom && push!(sts, ti)
elseif isa(t, Module) && !in(t, visited)
Expand All @@ -314,6 +332,7 @@ function _subtypes(m::Module, x::DataType, sts=Set{DataType}(), visited=Set{Modu
return sts
end
subtypes(m::Module, x::DataType) = sort(collect(_subtypes(m, x)), by=string)
subtypes(m::Module, x::UnionAll) = sort(collect(_subtypes(m, x)), by=string)

"""
subtypes(T::DataType)
Expand All @@ -331,6 +350,7 @@ julia> subtypes(Integer)
```
"""
subtypes(x::DataType) = subtypes(Main, x)
subtypes(x::UnionAll) = subtypes(Main, x)

function to_tuple_type(t::ANY)
@_pure_meta
Expand Down
7 changes: 7 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,10 @@ end
@generated f18883() = nothing
@test !isempty(sprint(io->code_llvm(io, f18883, Tuple{})))
@test !isempty(sprint(io->code_native(io, f18883, Tuple{})))

# Issue #20086
@test subtypes(Integer) == Type[BigInt, Bool, Signed, Unsigned]
abstract A212323{T}
immutable B212323{T} <: A212323{T} end
@test subtypes(A212323) == Type[B212323]
@test subtypes(A212323{Int}) == Type[B212323{Int}]