Skip to content

Commit

Permalink
add type annotations for non-constant Base globals (JuliaLang#44166)
Browse files Browse the repository at this point in the history
* add type annotations for non-constant Base globals
* remove no-longer needed callsite annotations
  • Loading branch information
aviatesk committed Feb 14, 2022
1 parent ed19129 commit b5f1a19
Show file tree
Hide file tree
Showing 19 changed files with 60 additions and 54 deletions.
1 change: 1 addition & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ replaceproperty!(x, f::Symbol, expected, desired, success_order::Symbol=:notatom
(@inline; Core.replacefield!(x, f, expected, convert(fieldtype(typeof(x), f), desired), success_order, fail_order))

convert(::Type{Any}, Core.@nospecialize x) = x
convert(::Type{T}, x::T) where {T} = x
include("coreio.jl")

eval(x) = Core.eval(Base, x)
Expand Down
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ end
function _global_julia_startup_file()
# If the user built us with a specific Base.SYSCONFDIR, check that location first for a startup.jl file
# If it is not found, then continue on to the relative path based on Sys.BINDIR
BINDIR = Sys.BINDIR::String
SYSCONFDIR = Base.SYSCONFDIR::String
BINDIR = Sys.BINDIR
SYSCONFDIR = Base.SYSCONFDIR
if !isempty(SYSCONFDIR)
p1 = abspath(BINDIR, SYSCONFDIR, "julia", "startup.jl")
isfile(p1) && return p1
Expand Down
1 change: 1 addition & 0 deletions base/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ macro inline() Expr(:meta, :inline) end
macro noinline() Expr(:meta, :noinline) end

convert(::Type{Any}, Core.@nospecialize x) = x
convert(::Type{T}, x::T) where {T} = x

# essential files and libraries
include("essentials.jl")
Expand Down
10 changes: 5 additions & 5 deletions base/coreio.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

print(xs...) = print(stdout::IO, xs...)
println(xs...) = println(stdout::IO, xs...)
print(xs...) = print(stdout, xs...)
println(xs...) = println(stdout, xs...)
println(io::IO) = print(io, '\n')

function show end
Expand Down Expand Up @@ -29,6 +29,6 @@ let CoreIO = Union{Core.CoreSTDOUT, Core.CoreSTDERR}
global wait_readnb(::CoreIO, nb::Int) = nothing
end

stdin = devnull
stdout = Core.stdout
stderr = Core.stderr
stdin::IO = devnull
stdout::IO = Core.stdout
stderr::IO = Core.stderr
1 change: 0 additions & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ See also: [`round`](@ref), [`trunc`](@ref), [`oftype`](@ref), [`reinterpret`](@r
function convert end

convert(::Type{Union{}}, @nospecialize x) = throw(MethodError(convert, (Union{}, x)))
convert(::Type{T}, x::T) where {T} = x
convert(::Type{Type}, x::Type) = x # the ssair optimizer is strongly dependent on this method existing to avoid over-specialization
# in the absence of inlining-enabled
# (due to fields typed as `Type`, which is generally a bad idea)
Expand Down
6 changes: 3 additions & 3 deletions base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const DEPOT_PATH = String[]
function append_default_depot_path!(DEPOT_PATH)
path = joinpath(homedir(), ".julia")
path in DEPOT_PATH || push!(DEPOT_PATH, path)
path = abspath(Sys.BINDIR::String, "..", "local", "share", "julia")
path = abspath(Sys.BINDIR, "..", "local", "share", "julia")
path in DEPOT_PATH || push!(DEPOT_PATH, path)
path = abspath(Sys.BINDIR::String, "..", "share", "julia")
path = abspath(Sys.BINDIR, "..", "share", "julia")
path in DEPOT_PATH || push!(DEPOT_PATH, path)
end

Expand Down Expand Up @@ -251,7 +251,7 @@ function load_path_expand(env::AbstractString)::Union{String, Nothing}
# if you put a `@` in LOAD_PATH manually, it's expanded late
env == "@" && return active_project(false)
env == "@." && return current_project()
env == "@stdlib" && return Sys.STDLIB::String
env == "@stdlib" && return Sys.STDLIB
env = replace(env, '#' => VERSION.major, count=1)
env = replace(env, '#' => VERSION.minor, count=1)
env = replace(env, '#' => VERSION.patch, count=1)
Expand Down
6 changes: 3 additions & 3 deletions base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ function reinit_stdio()
end

"""
stdin
stdin::IO
Global variable referring to the standard input stream.
"""
:stdin

"""
stdout
stdout::IO
Global variable referring to the standard out stream.
"""
:stdout

"""
stderr
stderr::IO
Global variable referring to the standard error stream.
"""
Expand Down
4 changes: 2 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function locate_package(pkg::PkgId)::Union{Nothing,String}
end
# Allow loading of stdlibs if the name/uuid are given
# e.g. if they have been explicitly added to the project/manifest
path = manifest_uuid_path(Sys.STDLIB::String, pkg)
path = manifest_uuid_path(Sys.STDLIB, pkg)
path === nothing || return entry_path(path, pkg.name)
end
return nothing
Expand Down Expand Up @@ -748,7 +748,7 @@ end

function find_source_file(path::AbstractString)
(isabspath(path) || isfile(path)) && return path
base_path = joinpath(Sys.BINDIR::String, DATAROOTDIR, "julia", "base", path)
base_path = joinpath(Sys.BINDIR, DATAROOTDIR, "julia", "base", path)
return isfile(base_path) ? normpath(base_path) : nothing
end

Expand Down
10 changes: 7 additions & 3 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ const methodloc_callback = Ref{Union{Function, Nothing}}(nothing)
function fixup_stdlib_path(path::String)
# The file defining Base.Sys gets included after this file is included so make sure
# this function is valid even in this intermediary state
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB::String
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
path = replace(path, normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB::String))
if isdefined(@__MODULE__, :Sys)
BUILD_STDLIB_PATH = Sys.BUILD_STDLIB_PATH::String
STDLIB = Sys.STDLIB::String
if BUILD_STDLIB_PATH != STDLIB
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
path = replace(path, normpath(BUILD_STDLIB_PATH) => normpath(STDLIB))
end
end
return path
end
Expand Down
2 changes: 1 addition & 1 deletion base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ function print_statement_costs(io::IO, @nospecialize(tt::Type);
end
end

print_statement_costs(args...; kwargs...) = print_statement_costs(stdout::IO, args...; kwargs...)
print_statement_costs(args...; kwargs...) = print_statement_costs(stdout, args...; kwargs...)

function _which(@nospecialize(tt::Type), world=get_world_counter())
min_valid = RefValue{UInt}(typemin(UInt))
Expand Down
4 changes: 2 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ Hello World!
"""
show(io::IO, @nospecialize(x)) = show_default(io, x)

show(x) = show(stdout::IO, x)
show(x) = show(stdout, x)

# avoid inferring show_default on the type of `x`
show_default(io::IO, @nospecialize(x)) = _show_default(io, inferencebarrier(x))
Expand Down Expand Up @@ -2710,7 +2710,7 @@ MyStruct
function dump(arg; maxdepth=DUMP_DEFAULT_MAXDEPTH)
# this is typically used interactively, so default to being in Main
mod = get(stdout, :module, Main)
dump(IOContext(stdout::IO, :limit => true, :module => mod), arg; maxdepth=maxdepth)
dump(IOContext(stdout, :limit => true, :module => mod), arg; maxdepth=maxdepth)
end


Expand Down
31 changes: 16 additions & 15 deletions base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,27 @@ export BINDIR,

import ..Base: show

global BINDIR = ccall(:jl_get_julia_bindir, Any, ())::String
"""
Sys.BINDIR
Sys.BINDIR::String
A string containing the full path to the directory containing the `julia` executable.
"""
:BINDIR
global BINDIR::String = ccall(:jl_get_julia_bindir, Any, ())::String

"""
Sys.STDLIB
Sys.STDLIB::String
A string containing the full path to the directory containing the `stdlib` packages.
"""
STDLIB = "$BINDIR/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)" # for bootstrap
global STDLIB::String = "$BINDIR/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)" # for bootstrap
# In case STDLIB change after julia is built, the variable below can be used
# to update cached method locations to updated ones.
const BUILD_STDLIB_PATH = STDLIB

# helper to avoid triggering precompile warnings

"""
Sys.CPU_THREADS
Sys.CPU_THREADS::Int
The number of logical CPU cores available in the system, i.e. the number of threads
that the CPU can run concurrently. Note that this is not necessarily the number of
Expand All @@ -65,37 +64,39 @@ CPU cores, for example, in the presence of
See Hwloc.jl or CpuId.jl for extended information, including number of physical cores.
"""
CPU_THREADS = 1 # for bootstrap, changed on startup
global CPU_THREADS::Int = 1 # for bootstrap, changed on startup

"""
Sys.ARCH
Sys.ARCH::Symbol
A symbol representing the architecture of the build configuration.
"""
const ARCH = ccall(:jl_get_ARCH, Any, ())
const ARCH = ccall(:jl_get_ARCH, Any, ())::Symbol


"""
Sys.KERNEL
Sys.KERNEL::Symbol
A symbol representing the name of the operating system, as returned by `uname` of the build configuration.
"""
const KERNEL = ccall(:jl_get_UNAME, Any, ())
const KERNEL = ccall(:jl_get_UNAME, Any, ())::Symbol

"""
Sys.MACHINE
Sys.MACHINE::String
A string containing the build triple.
"""
const MACHINE = Base.MACHINE
const MACHINE = Base.MACHINE::String

"""
Sys.WORD_SIZE
Sys.WORD_SIZE::Int
Standard word size on the current machine, in bits.
"""
const WORD_SIZE = Core.sizeof(Int) * 8

global SC_CLK_TCK::Clong, CPU_NAME::String, JIT::String

function __init__()
env_threads = nothing
if haskey(ENV, "JULIA_CPU_THREADS")
Expand All @@ -122,7 +123,7 @@ end
function __init_build()
global BINDIR = ccall(:jl_get_julia_bindir, Any, ())::String
vers = "v$(VERSION.major).$(VERSION.minor)"
global STDLIB = abspath(BINDIR::String, "..", "share", "julia", "stdlib", vers)
global STDLIB = abspath(BINDIR, "..", "share", "julia", "stdlib", vers)
nothing
end

Expand Down
8 changes: 4 additions & 4 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ See also [`print`](@ref), [`println`](@ref), [`show`](@ref).
printstyled(stdout, msg...; bold=bold, underline=underline, blink=blink, reverse=reverse, hidden=hidden, color=color)

"""
Base.julia_cmd(juliapath=joinpath(Sys.BINDIR::String, julia_exename()))
Base.julia_cmd(juliapath=joinpath(Sys.BINDIR, julia_exename()))
Return a julia command similar to the one of the running process.
Propagates any of the `--cpu-target`, `--sysimage`, `--compile`, `--sysimage-native-code`,
Expand All @@ -149,7 +149,7 @@ Among others, `--math-mode`, `--warn-overwrite`, and `--trace-compile` are notab
!!! compat "Julia 1.5"
The flags `--color` and `--startup-file` were added in Julia 1.5.
"""
function julia_cmd(julia=joinpath(Sys.BINDIR::String, julia_exename()))
function julia_cmd(julia=joinpath(Sys.BINDIR, julia_exename()))
opts = JLOptions()
cpu_target = unsafe_string(opts.cpu_target)
image_file = unsafe_string(opts.image_file)
Expand Down Expand Up @@ -569,7 +569,7 @@ to the standard libraries before running the tests.
If a seed is provided via the keyword argument, it is used to seed the
global RNG in the context where the tests are run; otherwise the seed is chosen randomly.
"""
function runtests(tests = ["all"]; ncores::Int = ceil(Int, Sys.CPU_THREADS::Int / 2),
function runtests(tests = ["all"]; ncores::Int = ceil(Int, Sys.CPU_THREADS / 2),
exit_on_error::Bool=false,
revise::Bool=false,
seed::Union{BitInteger,Nothing}=nothing)
Expand All @@ -585,7 +585,7 @@ function runtests(tests = ["all"]; ncores::Int = ceil(Int, Sys.CPU_THREADS::Int
delete!(ENV2, "JULIA_LOAD_PATH")
delete!(ENV2, "JULIA_PROJECT")
try
run(setenv(`$(julia_cmd()) $(joinpath(Sys.BINDIR::String,
run(setenv(`$(julia_cmd()) $(joinpath(Sys.BINDIR,
Base.DATAROOTDIR, "julia", "test", "runtests.jl")) $tests`, ENV2))
nothing
catch
Expand Down
4 changes: 2 additions & 2 deletions contrib/generate_precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Sys.__init_build()
if !isdefined(Base, :uv_eventloop)
Base.reinit_stdio()
end
Base.include(@__MODULE__, joinpath(Sys.BINDIR::String, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
Base.include(@__MODULE__, joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
import .FakePTYs: open_fake_pty
using Base.Meta

Expand Down Expand Up @@ -102,7 +102,7 @@ precompile_script = """
# end
"""

julia_exepath() = joinpath(Sys.BINDIR::String, Base.julia_exename())
julia_exepath() = joinpath(Sys.BINDIR, Base.julia_exename())

have_repl = haskey(Base.loaded_modules,
Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"))
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ default_addprocs_params(::ClusterManager) = default_addprocs_params()
default_addprocs_params() = Dict{Symbol,Any}(
:topology => :all_to_all,
:dir => pwd(),
:exename => joinpath(Sys.BINDIR::String, julia_exename()),
:exename => joinpath(Sys.BINDIR, julia_exename()),
:exeflags => ``,
:enable_threaded_blas => false,
:lazy => true)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/MozillaCACerts_jll/src/MozillaCACerts_jll.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ const LIBPATH_list = String[]
# These get calculated in __init__()
const PATH = Ref("")
const LIBPATH = Ref("")
artifact_dir = ""
cacert = ""
global artifact_dir::String = ""
global cacert::String = ""

function __init__()
global artifact_dir = dirname(Sys.BINDIR)
global cacert = normpath(Sys.BINDIR::String, Base.DATAROOTDIR, "julia", "cert.pem")
global cacert = normpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "cert.pem")
end

# JLLWrappers API compatibility shims. Note that not all of these will really make sense.
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ function short_path(spath::Symbol, filenamecache::Dict{Symbol, String})
end
end
return path
elseif isfile(joinpath(Sys.BINDIR::String, Base.DATAROOTDIR, "julia", "base", path))
elseif isfile(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "base", path))
# do the same mechanic for Base (or Core/Compiler) files as above,
# but they start from a relative path
return joinpath("@Base", normpath(path))
Expand Down
10 changes: 5 additions & 5 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,20 @@ function get_type(T, found::Bool, default_any::Bool)
end

# Method completion on function call expression that look like :(max(1))
MAX_METHOD_COMPLETIONS = 40
MAX_METHOD_COMPLETIONS::Int = 40
function complete_methods(ex_org::Expr, context_module::Module=Main)
out = Completion[]
funct, found = get_type(ex_org.args[1], context_module)::Tuple{Any,Bool}
!found && return out

args_ex, kwargs_ex = complete_methods_args(ex_org.args[2:end], ex_org, context_module, true, true)
push!(args_ex, Vararg{Any})
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_METHOD_COMPLETIONS::Int)
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_METHOD_COMPLETIONS)

return out
end

MAX_ANY_METHOD_COMPLETIONS = 10
MAX_ANY_METHOD_COMPLETIONS::Int = 10
function complete_any_methods(ex_org::Expr, callee_module::Module, context_module::Module, moreargs::Bool, shift::Bool)
out = Completion[]
args_ex, kwargs_ex = try
Expand All @@ -550,7 +550,7 @@ function complete_any_methods(ex_org::Expr, callee_module::Module, context_modul
funct = Core.Typeof(func)
if !in(funct, seen)
push!(seen, funct)
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS::Int)
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS)
end
elseif callee_module === Main && isa(func, Module)
callee_module2 = func
Expand All @@ -561,7 +561,7 @@ function complete_any_methods(ex_org::Expr, callee_module::Module, context_modul
funct = Core.Typeof(func)
if !in(funct, seen)
push!(seen, funct)
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS::Int)
complete_methods!(out, funct, args_ex, kwargs_ex, MAX_ANY_METHOD_COMPLETIONS)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ precompile_test_harness(false) do dir
const layout3 = collect(x.match for x in eachmatch(r"..", "abcdefghijk"))::Vector{SubString{String}}
# create a backedge that includes Type{Union{}}, to ensure lookup can handle that
call_bottom() = show(stdout::IO, Union{})
call_bottom() = show(stdout, Union{})
Core.Compiler.return_type(call_bottom, Tuple{})
# check that @ccallable works from precompiled modules
Expand Down

0 comments on commit b5f1a19

Please sign in to comment.