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

effects: annotate nameof(::Module) + parentmodule(::Module) for constant folding #54223

Merged
merged 1 commit into from
Apr 24, 2024
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
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
aviatesk marked this conversation as resolved.
Show resolved Hide resolved
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