Skip to content

Commit

Permalink
effects: annotate nameof(::Module) + parentmodule(::Module) for c…
Browse files Browse the repository at this point in the history
…onstant folding (#54223)

These effects can be important for us to statically resolve
Module-handling logic that, e.g., inspects a loaded list of modules
looking for a particular name.

Co-authored-by: @gbaraldi

Co-authored-by: gbaraldi <baraldigabriel@gmail.com>
Co-authored-by: Cody Tapscott <topolarity@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent c38e7cd commit 227fa7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
28 changes: 15 additions & 13 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,11 @@ macro isdefined(s::Symbol)
return Expr(:escape, Expr(:isdefined, s))
end

"""
nameof(m::Module) -> Symbol
Get the name of a `Module` as a [`Symbol`](@ref).
# Examples
```jldoctest
julia> nameof(Base.Broadcast)
:Broadcast
```
"""
nameof(m::Module) = ccall(:jl_module_name, Ref{Symbol}, (Any,), m)
_nameof(m::Module) = ccall(:jl_module_name, Ref{Symbol}, (Any,), m)

function _is_internal(__module__)
if ccall(:jl_base_relative_to, Any, (Any,), __module__)::Module === Core.Compiler ||
nameof(__module__) === :Base
_nameof(__module__) === :Base
return true
end
return false
Expand Down Expand Up @@ -375,6 +364,19 @@ getindex(A::GenericMemory, i::Int) = (@_noub_if_noinbounds_meta;
memoryrefget(memoryref(memoryref(A), i, @_boundscheck), default_access_order(A), false))
getindex(A::GenericMemoryRef) = memoryrefget(A, default_access_order(A), @_boundscheck)

"""
nameof(m::Module) -> Symbol
Get the name of a `Module` as a [`Symbol`](@ref).
# Examples
```jldoctest
julia> nameof(Base.Broadcast)
:Broadcast
```
"""
nameof(m::Module) = (@_total_meta; ccall(:jl_module_name, Ref{Symbol}, (Any,), m))

function iterate end

"""
Expand Down
4 changes: 3 additions & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ julia> parentmodule(Base.Broadcast)
Base
```
"""
parentmodule(m::Module) = ccall(:jl_module_parent, Ref{Module}, (Any,), m)
parentmodule(m::Module) = (@_total_meta; ccall(:jl_module_parent, Ref{Module}, (Any,), m))

is_root_module(m::Module) = parentmodule(m) === m || (isdefined(Main, :Base) && m === Main.Base)

Expand All @@ -30,6 +30,7 @@ parent modules of `m` which is either a registered root module or which is its
own parent module.
"""
function moduleroot(m::Module)
@_total_meta
while true
is_root_module(m) && return m
p = parentmodule(m)
Expand Down Expand Up @@ -63,6 +64,7 @@ julia> fullname(Main)
```
"""
function fullname(m::Module)
@_total_meta
mn = nameof(m)
if m === Main || m === Base || m === Core
return (mn,)
Expand Down

0 comments on commit 227fa7b

Please sign in to comment.