Skip to content

Commit ddecfe7

Browse files
authored
fix overlapping definitions of Base.active_module and REPL.active_module (#55316)
also avoid calling `active_module` from low-level printing functions fix #54888
1 parent 5230d27 commit ddecfe7

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

base/Enums.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Base.print(io::IO, x::Enum) = print(io, _symbol(x))
4444
function Base.show(io::IO, x::Enum)
4545
sym = _symbol(x)
4646
if !(get(io, :compact, false)::Bool)
47-
from = get(io, :module, Base.active_module())
47+
from = get(io, :module, Main)
4848
def = parentmodule(typeof(x))
4949
if from === nothing || !Base.isvisible(sym, def, from)
5050
show(io, def)

base/show.jl

+18-26
Original file line numberDiff line numberDiff line change
@@ -514,24 +514,16 @@ function _show_default(io::IO, @nospecialize(x))
514514
end
515515

516516
function active_module()
517-
REPL = REPL_MODULE_REF[]
518-
REPL === Base && return Main
519-
return invokelatest(REPL.active_module)::Module
517+
if ccall(:jl_is_in_pure_context, Bool, ())
518+
error("active_module() should not be called from a pure context")
519+
end
520+
if !@isdefined(active_repl) || active_repl === nothing
521+
return Main
522+
end
523+
return invokelatest(active_module, active_repl)::Module
520524
end
521525

522-
# Check if a particular symbol is exported from a standard library module
523-
function is_exported_from_stdlib(name::Symbol, mod::Module)
524-
!isdefined(mod, name) && return false
525-
orig = getfield(mod, name)
526-
while !(mod === Base || mod === Core)
527-
activemod = active_module()
528-
parent = parentmodule(mod)
529-
if mod === activemod || mod === parent || parent === activemod
530-
return false
531-
end
532-
mod = parent
533-
end
534-
return isexported(mod, name) && isdefined(mod, name) && !isdeprecated(mod, name) && getfield(mod, name) === orig
526+
module UsesCoreAndBaseOnly
535527
end
536528

537529
function show_function(io::IO, f::Function, compact::Bool, fallback::Function)
@@ -544,13 +536,13 @@ function show_function(io::IO, f::Function, compact::Bool, fallback::Function)
544536
print(io, mt.name)
545537
elseif isdefined(mt, :module) && isdefined(mt.module, mt.name) &&
546538
getfield(mt.module, mt.name) === f
547-
mod = active_module()
548-
if is_exported_from_stdlib(mt.name, mt.module) || mt.module === mod
549-
show_sym(io, mt.name)
550-
else
539+
# this used to call the removed internal function `is_exported_from_stdlib`, which effectively
540+
# just checked for exports from Core and Base.
541+
mod = get(io, :module, UsesCoreAndBaseOnly)
542+
if !(isvisible(mt.name, mt.module, mod) || mt.module === mod)
551543
print(io, mt.module, ".")
552-
show_sym(io, mt.name)
553544
end
545+
show_sym(io, mt.name)
554546
else
555547
fallback(io, f)
556548
end
@@ -737,9 +729,9 @@ end
737729
function show_typealias(io::IO, name::GlobalRef, x::Type, env::SimpleVector, wheres::Vector)
738730
if !(get(io, :compact, false)::Bool)
739731
# Print module prefix unless alias is visible from module passed to
740-
# IOContext. If :module is not set, default to Main (or current active module).
732+
# IOContext. If :module is not set, default to Main.
741733
# nothing can be used to force printing prefix.
742-
from = get(io, :module, active_module())
734+
from = get(io, :module, Main)
743735
if (from === nothing || !isvisible(name.name, name.mod, from))
744736
show(io, name.mod)
745737
print(io, ".")
@@ -1053,9 +1045,9 @@ function show_type_name(io::IO, tn::Core.TypeName)
10531045
quo = false
10541046
if !(get(io, :compact, false)::Bool)
10551047
# Print module prefix unless type is visible from module passed to
1056-
# IOContext If :module is not set, default to Main (or current active module).
1048+
# IOContext If :module is not set, default to Main.
10571049
# nothing can be used to force printing prefix
1058-
from = get(io, :module, active_module())
1050+
from = get(io, :module, Main)
10591051
if isdefined(tn, :module) && (from === nothing || !isvisible(sym, tn.module, from::Module))
10601052
show(io, tn.module)
10611053
print(io, ".")
@@ -2535,7 +2527,7 @@ function show_signature_function(io::IO, @nospecialize(ft), demangle=false, farg
25352527
uw = unwrap_unionall(ft)
25362528
if ft <: Function && isa(uw, DataType) && isempty(uw.parameters) && _isself(uw)
25372529
uwmod = parentmodule(uw)
2538-
if qualified && !is_exported_from_stdlib(uw.name.mt.name, uwmod) && uwmod !== Main
2530+
if qualified && !isexported(uwmod, uw.name.mt.name) && uwmod !== Main
25392531
print_within_stacktrace(io, uwmod, '.', bold=true)
25402532
end
25412533
s = sprint(show_sym, (demangle ? demangle_function_name : identity)(uw.name.mt.name), context=io)

stdlib/REPL/src/REPL.jl

+8-13
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function warn_on_non_owning_accesses(current_mod, ast)
329329
end
330330
return ast
331331
end
332-
warn_on_non_owning_accesses(ast) = warn_on_non_owning_accesses(REPL.active_module(), ast)
332+
warn_on_non_owning_accesses(ast) = warn_on_non_owning_accesses(Base.active_module(), ast)
333333

334334
const repl_ast_transforms = Any[softscope, warn_on_non_owning_accesses] # defaults for new REPL backends
335335

@@ -497,7 +497,7 @@ end
497497
function display(d::REPLDisplay, mime::MIME"text/plain", x)
498498
x = Ref{Any}(x)
499499
with_repl_linfo(d.repl) do io
500-
io = IOContext(io, :limit => true, :module => active_module(d)::Module)
500+
io = IOContext(io, :limit => true, :module => Base.active_module(d)::Module)
501501
if d.repl isa LineEditREPL
502502
mistate = d.repl.mistate
503503
mode = LineEdit.mode(mistate)
@@ -527,7 +527,7 @@ show_repl(io::IO, ::MIME"text/plain", ex::Expr) =
527527
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
528528
repl.waserror = response[2]
529529
with_repl_linfo(repl) do io
530-
io = IOContext(io, :module => active_module(repl)::Module)
530+
io = IOContext(io, :module => Base.active_module(repl)::Module)
531531
print_response(io, response, show_value, have_color, specialdisplay(repl))
532532
end
533533
return nothing
@@ -628,7 +628,7 @@ function run_repl(repl::AbstractREPL, @nospecialize(consumer = x -> nothing); ba
628628
Core.println(Core.stderr, e)
629629
Core.println(Core.stderr, catch_backtrace())
630630
end
631-
get_module = () -> active_module(repl)
631+
get_module = () -> Base.active_module(repl)
632632
if backend_on_current_task
633633
t = @async run_frontend(repl, backend_ref)
634634
errormonitor(t)
@@ -760,14 +760,9 @@ REPLCompletionProvider() = REPLCompletionProvider(LineEdit.Modifiers())
760760
mutable struct ShellCompletionProvider <: CompletionProvider end
761761
struct LatexCompletions <: CompletionProvider end
762762

763-
function active_module() # this method is also called from Base
764-
isdefined(Base, :active_repl) || return Main
765-
Base.active_repl === nothing && return Main
766-
return active_module(Base.active_repl::AbstractREPL)
767-
end
768-
active_module((; mistate)::LineEditREPL) = mistate === nothing ? Main : mistate.active_module
769-
active_module(::AbstractREPL) = Main
770-
active_module(d::REPLDisplay) = active_module(d.repl)
763+
Base.active_module((; mistate)::LineEditREPL) = mistate === nothing ? Main : mistate.active_module
764+
Base.active_module(::AbstractREPL) = Main
765+
Base.active_module(d::REPLDisplay) = Base.active_module(d.repl)
771766

772767
setmodifiers!(c::CompletionProvider, m::LineEdit.Modifiers) = nothing
773768

@@ -1206,7 +1201,7 @@ enable_promptpaste(v::Bool) = JL_PROMPT_PASTE[] = v
12061201

12071202
function contextual_prompt(repl::LineEditREPL, prompt::Union{String,Function})
12081203
function ()
1209-
mod = active_module(repl)
1204+
mod = Base.active_module(repl)
12101205
prefix = mod == Main ? "" : string('(', mod, ") ")
12111206
pr = prompt isa String ? prompt : prompt()
12121207
prefix * pr

0 commit comments

Comments
 (0)