Skip to content

Commit e3fbf19

Browse files
committed
small changes to make Base more statically compileable (#53778)
This makes it easier to fully-statically-type Base and init methods. The changes are from gb/small-image2.
1 parent d68a04e commit e3fbf19

File tree

13 files changed

+34
-28
lines changed

13 files changed

+34
-28
lines changed

base/boot.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ function is_top_bit_set(x::Union{Int8,UInt8})
769769
end
770770

771771
# n.b. This function exists for CUDA to overload to configure error behavior (see #48097)
772-
throw_inexacterror(args...) = throw(InexactError(args...))
772+
throw_inexacterror(func::Symbol, to, val) = throw(InexactError(func, to, val))
773773

774774
function check_sign_bit(::Type{To}, x) where {To}
775775
@inline

base/compiler/utilities.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,6 @@ end
498498
# options #
499499
###########
500500

501-
is_root_module(m::Module) = false
502-
503501
inlining_enabled() = (JLOptions().can_inline == 1)
504502

505503
function coverage_enabled(m::Module)

base/error.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,15 @@ macro assert(ex, msgs...)
228228
msg = Main.Base.string(msg)
229229
else
230230
# string() might not be defined during bootstrap
231-
msg = quote
232-
msg = $(Expr(:quote,msg))
233-
isdefined(Main, :Base) ? Main.Base.string(msg) :
234-
(Core.println(msg); "Error during bootstrap. See stdout.")
235-
end
231+
msg = :(_assert_tostring($(Expr(:quote,msg))))
236232
end
237233
return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg)))
238234
end
239235

236+
# this may be overridden in contexts where `string(::Expr)` doesn't work
237+
_assert_tostring(msg) = isdefined(Main, :Base) ? Main.Base.string(msg) :
238+
(Core.println(msg); "Error during bootstrap. See stdout.")
239+
240240
struct ExponentialBackOff
241241
n::Int
242242
first_delay::Float64

base/errorshow.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ end
400400

401401
#Show an error by directly calling jl_printf.
402402
#Useful in Base submodule __init__ functions where stderr isn't defined yet.
403-
function showerror_nostdio(err, msg::AbstractString)
403+
function showerror_nostdio(@nospecialize(err), msg::AbstractString)
404404
stderr_stream = ccall(:jl_stderr_stream, Ptr{Cvoid}, ())
405405
ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, msg)
406406
ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, ":\n")

base/initdefs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A string containing the script name passed to Julia from the command line. Note
99
script name remains unchanged from within included files. Alternatively see
1010
[`@__FILE__`](@ref).
1111
"""
12-
global PROGRAM_FILE = ""
12+
global PROGRAM_FILE::String = ""
1313

1414
"""
1515
ARGS
@@ -480,7 +480,7 @@ end
480480

481481
## hook for disabling threaded libraries ##
482482

483-
library_threading_enabled = true
483+
library_threading_enabled::Bool = true
484484
const disable_library_threading_hooks = []
485485

486486
function at_disable_library_threading(f)

base/iostream.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,15 @@ function open(fname::String; lock = true,
292292
if !lock
293293
s._dolock = false
294294
end
295-
systemerror("opening file $(repr(fname))",
296-
ccall(:ios_file, Ptr{Cvoid},
297-
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
298-
s.ios, fname, flags.read, flags.write, flags.create, flags.truncate) == C_NULL)
295+
if ccall(:ios_file, Ptr{Cvoid},
296+
(Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint),
297+
s.ios, fname, flags.read, flags.write, flags.create, flags.truncate) == C_NULL
298+
systemerror("opening file $(repr(fname))")
299+
end
299300
if flags.append
300-
systemerror("seeking to end of file $fname", ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0)
301+
if ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0
302+
systemerror("seeking to end of file \"$fname\"")
303+
end
301304
end
302305
return s
303306
end

base/libuv.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ function uv_asynccb end
134134
function uv_timercb end
135135

136136
function reinit_stdio()
137-
global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ()))
138-
global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ()))
139-
global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ()))
137+
global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ()))::IO
138+
global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ()))::IO
139+
global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ()))::IO
140140
opts = JLOptions()
141141
if opts.color != 0
142142
have_color = (opts.color == 1)

base/loading.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,6 @@ const explicit_loaded_modules = Dict{PkgId,Module}()
22052205
const loaded_modules_order = Vector{Module}()
22062206
const module_keys = IdDict{Module,PkgId}() # the reverse
22072207

2208-
is_root_module(m::Module) = @lock require_lock haskey(module_keys, m)
22092208
root_module_key(m::Module) = @lock require_lock module_keys[m]
22102209

22112210
@constprop :none function register_root_module(m::Module)
@@ -3416,9 +3415,9 @@ function check_clone_targets(clone_targets)
34163415
end
34173416

34183417
# Set by FileWatching.__init__()
3419-
global mkpidlock_hook
3420-
global trymkpidlock_hook
3421-
global parse_pidfile_hook
3418+
global mkpidlock_hook::Any
3419+
global trymkpidlock_hook::Any
3420+
global parse_pidfile_hook::Any
34223421

34233422
# The preferences hash is only known after precompilation so just assume no preferences.
34243423
# Also ignore the active project, which means that if all other conditions are equal,

base/reflection.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Base
2020
"""
2121
parentmodule(m::Module) = ccall(:jl_module_parent, Ref{Module}, (Any,), m)
2222

23+
is_root_module(m::Module) = parentmodule(m) === m || (isdefined(Main, :Base) && m === Main.Base)
24+
2325
"""
2426
moduleroot(m::Module) -> Module
2527

base/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ function show_type_name(io::IO, tn::Core.TypeName)
10561056
# IOContext If :module is not set, default to Main (or current active module).
10571057
# nothing can be used to force printing prefix
10581058
from = get(io, :module, active_module())
1059-
if isdefined(tn, :module) && (from === nothing || !isvisible(sym, tn.module, from))
1059+
if isdefined(tn, :module) && (from === nothing || !isvisible(sym, tn.module, from::Module))
10601060
show(io, tn.module)
10611061
print(io, ".")
10621062
if globfunc && !is_id_start_char(first(string(sym)))

0 commit comments

Comments
 (0)