From 227fa7b5e6f4b46bfb73b1f2861084a3e15422b2 Mon Sep 17 00:00:00 2001 From: Cody Tapscott <84105208+topolarity@users.noreply.github.com> Date: Wed, 24 Apr 2024 16:18:22 -0400 Subject: [PATCH] effects: annotate `nameof(::Module)` + `parentmodule(::Module)` for constant 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 Co-authored-by: Cody Tapscott --- base/essentials.jl | 28 +++++++++++++++------------- base/reflection.jl | 4 +++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/base/essentials.jl b/base/essentials.jl index fdc66e94d1a00..4e8a22fd229a9 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -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 @@ -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 """ diff --git a/base/reflection.jl b/base/reflection.jl index 2df713a531fae..f27da6d4df32e 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -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) @@ -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) @@ -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,)