Skip to content

Commit 798b589

Browse files
topolaritysjkelly
andauthored
Avoid unnecessary Docs.META initializations (#48469)
If the target module does not have a Docs.META dict (e.g. if `--strip-metadata` is used), `Docs.meta()` has the side effect of creating a new IdDict and initializing the Docs.META field of the target module. We need to avoid eval'ing into modules after they've been closed, so for methods that do not mutate the new IdDict we should avoid the init. Resolves #48390. Co-authored-by: Steve Kelly <kd2cca@gmail.com>
1 parent c18909d commit 798b589

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

base/docs/Docs.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ const modules = Module[]
7373
const META = gensym(:meta)
7474
const METAType = IdDict{Any,Any}
7575

76-
function meta(m::Module)
76+
function meta(m::Module; autoinit::Bool=true)
7777
if !isdefined(m, META) || getfield(m, META) === nothing
78-
initmeta(m)
78+
autoinit ? initmeta(m) : return nothing
7979
end
8080
return getfield(m, META)::METAType
8181
end
@@ -161,7 +161,8 @@ end
161161
function docstr(binding::Binding, typesig = Union{})
162162
@nospecialize typesig
163163
for m in modules
164-
dict = meta(m)
164+
dict = meta(m; autoinit=false)
165+
isnothing(dict) && continue
165166
if haskey(dict, binding)
166167
docs = dict[binding].docs
167168
if haskey(docs, typesig)

stdlib/REPL/src/docview.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ function doc(binding::Binding, sig::Type = Union{})
164164
results, groups = DocStr[], MultiDoc[]
165165
# Lookup `binding` and `sig` for matches in all modules of the docsystem.
166166
for mod in modules
167-
dict = meta(mod)
167+
dict = meta(mod; autoinit=false)
168+
isnothing(dict) && continue
168169
if haskey(dict, binding)
169170
multidoc = dict[binding]
170171
push!(groups, multidoc)
@@ -565,7 +566,8 @@ Return documentation for a particular `field` of a type if it exists.
565566
"""
566567
function fielddoc(binding::Binding, field::Symbol)
567568
for mod in modules
568-
dict = meta(mod)
569+
dict = meta(mod; autoinit=false)
570+
isnothing(dict) && continue
569571
if haskey(dict, binding)
570572
multidoc = dict[binding]
571573
if haskey(multidoc.docs, Union{})
@@ -834,7 +836,9 @@ function apropos(io::IO, needle::Regex)
834836
for mod in modules
835837
# Module doc might be in README.md instead of the META dict
836838
docsearch(doc(mod), needle) && println(io, mod)
837-
for (k, v) in meta(mod)
839+
dict = meta(mod; autoinit=false)
840+
isnothing(dict) && continue
841+
for (k, v) in dict
838842
docsearch(v, needle) && println(io, k)
839843
end
840844
end

0 commit comments

Comments
 (0)