From bb24d21fd8a83956d584e41217e904084a566e54 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 19 Mar 2024 20:38:51 -0400 Subject: [PATCH] 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. --- base/boot.jl | 2 +- base/compiler/utilities.jl | 2 -- base/error.jl | 10 +++++----- base/errorshow.jl | 2 +- base/initdefs.jl | 4 ++-- base/iostream.jl | 13 ++++++++----- base/libuv.jl | 6 +++--- base/loading.jl | 7 +++---- base/reflection.jl | 2 ++ base/show.jl | 2 +- base/threadingconstructs.jl | 2 +- stdlib/FileWatching/src/FileWatching.jl | 5 +++++ stdlib/LinearAlgebra/src/blas.jl | 5 ++--- 13 files changed, 34 insertions(+), 28 deletions(-) diff --git a/base/boot.jl b/base/boot.jl index fc00f571171c9..a3c5c03bdf721 100644 --- a/base/boot.jl +++ b/base/boot.jl @@ -769,7 +769,7 @@ function is_top_bit_set(x::Union{Int8,UInt8}) end # n.b. This function exists for CUDA to overload to configure error behavior (see #48097) -throw_inexacterror(args...) = throw(InexactError(args...)) +throw_inexacterror(func::Symbol, to, val) = throw(InexactError(func, to, val)) function check_sign_bit(::Type{To}, x) where {To} @inline diff --git a/base/compiler/utilities.jl b/base/compiler/utilities.jl index 00c4c9d4e3ec4..1e002f8dad5a6 100644 --- a/base/compiler/utilities.jl +++ b/base/compiler/utilities.jl @@ -498,8 +498,6 @@ end # options # ########### -is_root_module(m::Module) = false - inlining_enabled() = (JLOptions().can_inline == 1) function coverage_enabled(m::Module) diff --git a/base/error.jl b/base/error.jl index fc294b3cb3eb0..07782fce5f52e 100644 --- a/base/error.jl +++ b/base/error.jl @@ -228,15 +228,15 @@ macro assert(ex, msgs...) msg = Main.Base.string(msg) else # string() might not be defined during bootstrap - msg = quote - msg = $(Expr(:quote,msg)) - isdefined(Main, :Base) ? Main.Base.string(msg) : - (Core.println(msg); "Error during bootstrap. See stdout.") - end + msg = :(_assert_tostring($(Expr(:quote,msg)))) end return :($(esc(ex)) ? $(nothing) : throw(AssertionError($msg))) end +# this may be overridden in contexts where `string(::Expr)` doesn't work +_assert_tostring(msg) = isdefined(Main, :Base) ? Main.Base.string(msg) : + (Core.println(msg); "Error during bootstrap. See stdout.") + struct ExponentialBackOff n::Int first_delay::Float64 diff --git a/base/errorshow.jl b/base/errorshow.jl index abc8083f38266..b06052433ffc4 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -400,7 +400,7 @@ end #Show an error by directly calling jl_printf. #Useful in Base submodule __init__ functions where stderr isn't defined yet. -function showerror_nostdio(err, msg::AbstractString) +function showerror_nostdio(@nospecialize(err), msg::AbstractString) stderr_stream = ccall(:jl_stderr_stream, Ptr{Cvoid}, ()) ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, msg) ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, ":\n") diff --git a/base/initdefs.jl b/base/initdefs.jl index 56c2c0c587272..2b2916fc804ad 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -9,7 +9,7 @@ A string containing the script name passed to Julia from the command line. Note script name remains unchanged from within included files. Alternatively see [`@__FILE__`](@ref). """ -global PROGRAM_FILE = "" +global PROGRAM_FILE::String = "" """ ARGS @@ -480,7 +480,7 @@ end ## hook for disabling threaded libraries ## -library_threading_enabled = true +library_threading_enabled::Bool = true const disable_library_threading_hooks = [] function at_disable_library_threading(f) diff --git a/base/iostream.jl b/base/iostream.jl index 5d972945e00e0..0ee51b9b98efd 100644 --- a/base/iostream.jl +++ b/base/iostream.jl @@ -292,12 +292,15 @@ function open(fname::String; lock = true, if !lock s._dolock = false end - systemerror("opening file $(repr(fname))", - ccall(:ios_file, Ptr{Cvoid}, - (Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint), - s.ios, fname, flags.read, flags.write, flags.create, flags.truncate) == C_NULL) + if ccall(:ios_file, Ptr{Cvoid}, + (Ptr{UInt8}, Cstring, Cint, Cint, Cint, Cint), + s.ios, fname, flags.read, flags.write, flags.create, flags.truncate) == C_NULL + systemerror("opening file $(repr(fname))") + end if flags.append - systemerror("seeking to end of file $fname", ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0) + if ccall(:ios_seek_end, Int64, (Ptr{Cvoid},), s.ios) != 0 + systemerror("seeking to end of file $fname") + end end return s end diff --git a/base/libuv.jl b/base/libuv.jl index 66dfcfb3414ad..35424ad3eda3e 100644 --- a/base/libuv.jl +++ b/base/libuv.jl @@ -134,9 +134,9 @@ function uv_asynccb end function uv_timercb end function reinit_stdio() - global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ())) - global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ())) - global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ())) + global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ()))::IO + global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ()))::IO + global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ()))::IO opts = JLOptions() if opts.color != 0 have_color = (opts.color == 1) diff --git a/base/loading.jl b/base/loading.jl index 883e980b4000a..986efc1e1e5fb 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -2205,7 +2205,6 @@ const explicit_loaded_modules = Dict{PkgId,Module}() const loaded_modules_order = Vector{Module}() const module_keys = IdDict{Module,PkgId}() # the reverse -is_root_module(m::Module) = @lock require_lock haskey(module_keys, m) root_module_key(m::Module) = @lock require_lock module_keys[m] @constprop :none function register_root_module(m::Module) @@ -3416,9 +3415,9 @@ function check_clone_targets(clone_targets) end # Set by FileWatching.__init__() -global mkpidlock_hook -global trymkpidlock_hook -global parse_pidfile_hook +global mkpidlock_hook::Any +global trymkpidlock_hook::Any +global parse_pidfile_hook::Any # The preferences hash is only known after precompilation so just assume no preferences. # Also ignore the active project, which means that if all other conditions are equal, diff --git a/base/reflection.jl b/base/reflection.jl index b1d41b4d68621..63909b4159877 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -20,6 +20,8 @@ Base """ parentmodule(m::Module) = ccall(:jl_module_parent, Ref{Module}, (Any,), m) +is_root_module(m::Module) = parentmodule(m) === m || (isdefined(Main, :Base) && m === Main.Base) + """ moduleroot(m::Module) -> Module diff --git a/base/show.jl b/base/show.jl index f7846e316c330..8f52fefdd7152 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1056,7 +1056,7 @@ function show_type_name(io::IO, tn::Core.TypeName) # IOContext If :module is not set, default to Main (or current active module). # nothing can be used to force printing prefix from = get(io, :module, active_module()) - if isdefined(tn, :module) && (from === nothing || !isvisible(sym, tn.module, from)) + if isdefined(tn, :module) && (from === nothing || !isvisible(sym, tn.module, from::Module)) show(io, tn.module) print(io, ".") if globfunc && !is_id_start_char(first(string(sym))) diff --git a/base/threadingconstructs.jl b/base/threadingconstructs.jl index 60aea04ddba64..acab7cf7b8299 100644 --- a/base/threadingconstructs.jl +++ b/base/threadingconstructs.jl @@ -79,7 +79,7 @@ function _sym_to_tpid(tp::Symbol) elseif tp == :foreign return Int8(-1) else - throw(ArgumentError("Unrecognized threadpool name `$(repr(tp))`")) + throw(ArgumentError("Unrecognized threadpool name `$tp`")) end end diff --git a/stdlib/FileWatching/src/FileWatching.jl b/stdlib/FileWatching/src/FileWatching.jl index 0ee572ec47d92..0c987ad01c828 100644 --- a/stdlib/FileWatching/src/FileWatching.jl +++ b/stdlib/FileWatching/src/FileWatching.jl @@ -468,6 +468,11 @@ function uv_fspollcb(handle::Ptr{Cvoid}, status::Int32, prev::Ptr, curr::Ptr) nothing end +global uv_jl_pollcb::Ptr{Cvoid} +global uv_jl_fspollcb::Ptr{Cvoid} +global uv_jl_fseventscb_file::Ptr{Cvoid} +global uv_jl_fseventscb_folder::Ptr{Cvoid} + function __init__() global uv_jl_pollcb = @cfunction(uv_pollcb, Cvoid, (Ptr{Cvoid}, Cint, Cint)) global uv_jl_fspollcb = @cfunction(uv_fspollcb, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid})) diff --git a/stdlib/LinearAlgebra/src/blas.jl b/stdlib/LinearAlgebra/src/blas.jl index 8903aaddcc11b..413b7866c5444 100644 --- a/stdlib/LinearAlgebra/src/blas.jl +++ b/stdlib/LinearAlgebra/src/blas.jl @@ -159,9 +159,8 @@ function check() interface = USE_BLAS64 ? :ilp64 : :lp64 if !any(lib.interface == interface for lib in config.loaded_libs) interfacestr = uppercase(string(interface)) - @error("No loaded BLAS libraries were built with $(interfacestr) support") - println("Quitting.") - exit() + @error("No loaded BLAS libraries were built with $interfacestr support.") + exit(1) end end