Skip to content

Commit e5c2c51

Browse files
authored
fix(REPL): using/import statements should on top-level, #49041 (#49098)
1 parent 329f92c commit e5c2c51

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

stdlib/REPL/src/REPL.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,12 +1407,24 @@ function repl_eval_counter(hp)
14071407
end
14081408

14091409
function out_transform(@nospecialize(x), n::Ref{Int})
1410-
return quote
1410+
return Expr(:toplevel, get_usings!([], x)..., quote
14111411
let __temp_val_a72df459 = $x
14121412
$capture_result($n, __temp_val_a72df459)
14131413
__temp_val_a72df459
14141414
end
1415+
end)
1416+
end
1417+
1418+
function get_usings!(usings, ex)
1419+
# get all `using` and `import` statements which are at the top level
1420+
for (i, arg) in enumerate(ex.args)
1421+
if Base.isexpr(arg, :toplevel)
1422+
get_usings!(usings, arg)
1423+
elseif Base.isexpr(arg, [:using, :import])
1424+
push!(usings, popat!(ex.args, i))
1425+
end
14151426
end
1427+
return usings
14161428
end
14171429

14181430
function capture_result(n::Ref{Int}, @nospecialize(x))

stdlib/REPL/test/repl.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,11 @@ fake_repl() do stdin_write, stdout_read, repl
16471647
s = sendrepl2("x_47878 = range(-1; stop = 1)\n", "-1:1")
16481648
@test contains(s, "Out[11]: -1:1")
16491649

1650+
# Test for https://github.com/JuliaLang/julia/issues/49041
1651+
s = sendrepl2("using Test; @test true", "In [14]")
1652+
@test !contains(s, "ERROR")
1653+
@test contains(s, "Test Passed")
1654+
16501655
write(stdin_write, '\x04')
16511656
Base.wait(repltask)
16521657
end

0 commit comments

Comments
 (0)