Skip to content

Commit

Permalink
Revert "clean up identifiers defined in Main (#51411)" (#51825)
Browse files Browse the repository at this point in the history
This reverts commit ab94a24.

Ref
#51411 (comment)
fixes #51823
  • Loading branch information
simeonschaub committed Oct 24, 2023
1 parent b197197 commit 9352d9e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
19 changes: 12 additions & 7 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ function exec_options(opts)
distributed_mode = (opts.worker == 1) || (opts.nprocs > 0) || (opts.machine_file != C_NULL)
if distributed_mode
let Distributed = require(PkgId(UUID((0x8ba89e20_285c_5b6f, 0x9357_94700520ee1b)), "Distributed"))
Core.eval(MainInclude, :(const Distributed = $Distributed))
Core.eval(Main, :(using Base.MainInclude.Distributed))
Core.eval(Main, :(const Distributed = $Distributed))
Core.eval(Main, :(using .Distributed))
end

invokelatest(Main.Distributed.process_opts, opts)
Expand Down Expand Up @@ -384,18 +384,19 @@ _atreplinit(repl) = invokelatest(__atreplinit, repl)

function load_InteractiveUtils(mod::Module=Main)
# load interactive-only libraries
if !isdefined(MainInclude, :InteractiveUtils)
if !isdefined(mod, :InteractiveUtils)
try
let InteractiveUtils = require(PkgId(UUID(0xb77e0a4c_d291_57a0_90e8_8db25a27a240), "InteractiveUtils"))
Core.eval(MainInclude, :(const InteractiveUtils = $InteractiveUtils))
Core.eval(mod, :(const InteractiveUtils = $InteractiveUtils))
Core.eval(mod, :(using .InteractiveUtils))
return InteractiveUtils
end
catch ex
@warn "Failed to import InteractiveUtils into module $mod" exception=(ex, catch_backtrace())
return nothing
end
return nothing
end
Core.eval(mod, :(using Base.MainInclude.InteractiveUtils))
return MainInclude.InteractiveUtils
return getfield(mod, :InteractiveUtils)
end

function load_REPL()
Expand Down Expand Up @@ -512,6 +513,10 @@ A variable referring to the last thrown errors, automatically imported to the in
The thrown errors are collected in a stack of exceptions.
"""
global err = nothing

# weakly exposes ans and err variables to Main
export ans, err

end

"""
Expand Down
7 changes: 4 additions & 3 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Core.include(Main, "Base.jl")

using .Base

# Set up Main module
using Base.MainInclude # ans, err, and sometimes Out
import Base.MainInclude: eval, include

# Ensure this file is also tracked
pushfirst!(Base._included_files, (@__MODULE__, abspath(@__FILE__)))

Expand Down Expand Up @@ -75,10 +79,7 @@ let
empty!(LOAD_PATH)
Base.init_load_path() # want to be able to find external packages in userimg.jl

# Set up Main module
ccall(:jl_clear_implicit_imports, Cvoid, (Any,), Main)
eval(Main, :(using Base.MainInclude: eval, include, ans, err))

tot_time_userimg = @elapsed (isfile("userimg.jl") && Base.include(Main, "userimg.jl"))

tot_time_base = (Base.end_base_include - Base.start_base_include) * 10.0^(-9)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1490,8 +1490,8 @@ function capture_result(n::Ref{Int}, @nospecialize(x))
mod = Base.MainInclude
if !isdefined(mod, :Out)
@eval mod global Out
@eval mod export Out
setglobal!(mod, :Out, Dict{Int, Any}())
@eval Main using Base.MainInclude: Out
end
if x !== getglobal(mod, :Out) && x !== nothing # remove this?
getglobal(mod, :Out)[n] = x
Expand Down
3 changes: 3 additions & 0 deletions stdlib/REPL/test/replcompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2089,3 +2089,6 @@ let t = REPLCompletions.repl_eval_ex(:(`a b`), @__MODULE__; limit_aggressive_inf
@test t isa Core.Const
@test t.val == `a b`
end

# issue #51823
@test "include" in test_complete_context("inc", Main)[1]
5 changes: 5 additions & 0 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ end
ERROR: ErrorException
"""s, err_str)
end

@testset "defining `ans` and `err`" begin
@test eval(:(ans = 1)) == 1
@test eval(:(err = 1)) == 1
end

0 comments on commit 9352d9e

Please sign in to comment.