Skip to content

Commit 9cde595

Browse files
committed
Reunify include() implementation using @ hide_in_stacktrace
This reunifies the versions of include() in MainInclude and Base which have started diverging after being duplicated for improved stack traces. @ hide_in_backtrace provides a solution where the implementation is marked as internal.
1 parent c3d2533 commit 9cde595

File tree

4 files changed

+37
-50
lines changed

4 files changed

+37
-50
lines changed

base/Base.jl

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -365,31 +365,8 @@ for m in methods(include)
365365
end
366366
# These functions are duplicated in client.jl/include(::String) for
367367
# nicer stacktraces. Modifications here have to be backported there
368-
include(mod::Module, _path::AbstractString) = include(identity, mod, _path)
369-
function include(mapexpr::Function, mod::Module, _path::AbstractString)
370-
path, prev = _include_dependency(mod, _path)
371-
for callback in include_callbacks # to preserve order, must come before Core.include
372-
invokelatest(callback, mod, path)
373-
end
374-
tls = task_local_storage()
375-
tls[:SOURCE_PATH] = path
376-
local result
377-
try
378-
# result = Core.include(mod, path)
379-
if mapexpr === identity
380-
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
381-
else
382-
result = ccall(:jl_load_rewrite, Any, (Any, Cstring, Any), mod, path, mapexpr)
383-
end
384-
finally
385-
if prev === nothing
386-
delete!(tls, :SOURCE_PATH)
387-
else
388-
tls[:SOURCE_PATH] = prev
389-
end
390-
end
391-
return result
392-
end
368+
include(mod::Module, _path::AbstractString) = _include(identity, mod, _path)
369+
include(mapexpr::Function, mod::Module, _path::AbstractString) = _include(mapexpr, mod, _path)
393370

394371
end_base_include = time_ns()
395372

base/client.jl

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -423,30 +423,12 @@ end
423423
# MainInclude exists to hide Main.include and eval from `names(Main)`.
424424
baremodule MainInclude
425425
using ..Base
426-
include(mapexpr::Function, fname::AbstractString) = Base.include(mapexpr, Main, fname)
427-
# We inline the definition of include from loading.jl/include_relative to get one-frame stacktraces
428-
# for the common case of include(fname). Otherwise we would use:
429-
# include(fname::AbstractString) = Base.include(Main, fname)
426+
# These definitions calls Base._include rather than Base.include to get
427+
# one-frame stacktraces for the common case of using include(fname) in Main.
428+
include(mapexpr::Function, fname::AbstractString) = Base._include(mapexpr, Main, fname)
430429
function include(fname::AbstractString)
431-
mod = Main
432430
isa(fname, String) || (fname = Base.convert(String, fname)::String)
433-
path, prev = Base._include_dependency(mod, fname)
434-
for callback in Base.include_callbacks # to preserve order, must come before Core.include
435-
Base.invokelatest(callback, mod, path)
436-
end
437-
tls = Base.task_local_storage()
438-
tls[:SOURCE_PATH] = path
439-
local result
440-
try
441-
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
442-
finally
443-
if prev === nothing
444-
Base.delete!(tls, :SOURCE_PATH)
445-
else
446-
tls[:SOURCE_PATH] = prev
447-
end
448-
end
449-
return result
431+
Base._include(identity, Main, fname)
450432
end
451433
eval(x) = Core.eval(Main, x)
452434
end

base/loading.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,34 @@ The optional first argument `mapexpr` can be used to transform the included code
11231123
it is evaluated: for each parsed expression `expr` in `path`, the `include` function
11241124
actually evaluates `mapexpr(expr)`. If it is omitted, `mapexpr` defaults to [`identity`](@ref).
11251125
"""
1126-
Base.include # defined in sysimg.jl
1126+
Base.include # defined in Base.jl
1127+
1128+
# Full include() implementation which is used after bootstrap
1129+
# Hidden for nicer backtraces in include()
1130+
@hide_in_stacktrace function _include(mapexpr::Function, mod::Module, _path::AbstractString)
1131+
path, prev = _include_dependency(mod, _path)
1132+
for callback in include_callbacks # to preserve order, must come before Core.include
1133+
invokelatest(callback, mod, path)
1134+
end
1135+
tls = task_local_storage()
1136+
tls[:SOURCE_PATH] = path
1137+
local result
1138+
try
1139+
# result = Core.include(mod, path)
1140+
if mapexpr === identity
1141+
result = ccall(:jl_load, Any, (Any, Cstring), mod, path)
1142+
else
1143+
result = ccall(:jl_load_rewrite, Any, (Any, Cstring, Any), mod, path, mapexpr)
1144+
end
1145+
finally
1146+
if prev === nothing
1147+
delete!(tls, :SOURCE_PATH)
1148+
else
1149+
tls[:SOURCE_PATH] = prev
1150+
end
1151+
end
1152+
return result
1153+
end
11271154

11281155
"""
11291156
evalfile(path::AbstractString, args::Vector{String}=String[])

test/precompile.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,11 @@ try
386386
write(FooBar2_file,
387387
"""
388388
module FooBar2
389-
error("break me")
389+
some_error() = error("break me")
390+
some_error()
390391
end
391392
""")
392-
@test_warn "ERROR: LoadError: break me\nStacktrace:\n [1] error" try
393+
@test_warn "ERROR: LoadError: break me\nStacktrace:\n [1] some_error" try
393394
Base.require(Main, :FooBar2)
394395
error("the \"break me\" test failed")
395396
catch exc

0 commit comments

Comments
 (0)