Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/Base_compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ end
setfield!(typeof(invoke_in_world).name, :max_args, Int32(3), :monotonic) # invoke_in_world, world, f, args...

# core operations & types
include("constructingfunctions.jl")
include("promotion.jl")
include("tuple.jl")
include("expr.jl")
Expand Down
10 changes: 5 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ If multiple arguments are passed, equivalent to `has_offset_axes(A) || has_offse
See also [`require_one_based_indexing`](@ref).
"""
has_offset_axes() = false
has_offset_axes(A) = _any_tuple(x->Int(first(x))::Int != 1, false, axes(A)...)
has_offset_axes(A::AbstractVector) = Int(firstindex(A))::Int != 1 # improve performance of a common case (ranges)
has_offset_axes(A) = _any_tuple(!=(1) ∘ _Int ∘ first, false, axes(A)...)
has_offset_axes(A::AbstractVector) = _Int(firstindex(A)) != 1 # improve performance of a common case (ranges)
has_offset_axes(::Colon) = false
has_offset_axes(::Array) = false
# note: this could call `any` directly if the compiler can infer it. We don't use _any_tuple
Expand Down Expand Up @@ -1269,7 +1269,7 @@ end
pointer(x::AbstractArray{T}) where {T} = unsafe_convert(Ptr{T}, cconvert(Ptr{T}, x))
function pointer(x::AbstractArray{T}, i::Integer) where T
@inline
pointer(x) + Int(_memory_offset(x, i))::Int
pointer(x) + _Int(_memory_offset(x, i))
end

# The distance from pointer(x) to the element at x[I...] in bytes
Expand Down Expand Up @@ -1702,9 +1702,9 @@ _typed_vcat(::Type{T}, V::AbstractVecOrTuple{AbstractVector}) where T =

function _typed_vcat!(a::AbstractVector{T}, V::AbstractVecOrTuple{AbstractVector}) where T
pos = 1
for k=1:Int(length(V))::Int
for k=1:_Int(length(V))
Vk = V[k]
p1 = pos + Int(length(Vk))::Int - 1
p1 = pos + _Int(length(Vk)) - 1
a[pos:p1] = Vk
pos = p1+1
end
Expand Down
2 changes: 1 addition & 1 deletion base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ max_values(::Type{Bool}) = 2
max_values(::Type{Nothing}) = 1

function union!(s::AbstractSet{T}, itr) where T
haslength(itr) && sizehint!(s, length(s) + Int(length(itr))::Int; shrink = false)
haslength(itr) && sizehint!(s, length(s) + _Int(length(itr)); shrink = false)
for x in itr
push!(s, x)
length(s) == max_values(T) && break
Expand Down
10 changes: 5 additions & 5 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function vect(X...)
return T[X...]
end

size(a::Array, d::Integer) = size(a, Int(d)::Int)
size(a::Array, d::Integer) = size(a, _Int(d))
function size(a::Array, d::Int)
d < 1 && error("arraysize: dimension out of range")
sz = getfield(a, :size)
Expand Down Expand Up @@ -1372,7 +1372,7 @@ function append! end

function append!(a::Vector{T}, items::Union{AbstractVector{<:T},Tuple}) where T
items isa Tuple && (items = map(x -> convert(T, x), items))
n = Int(length(items))::Int
n = _Int(length(items))
_growend!(a, n)
copyto!(a, length(a)-n+1, items, firstindex(items), n)
return a
Expand All @@ -1383,7 +1383,7 @@ push!(a::AbstractVector, iter...) = append!(a, iter)
append!(a::AbstractVector, iter...) = (foreach(v -> append!(a, v), iter); a)

function _append!(a::AbstractVector, ::Union{HasLength,HasShape}, iter)
n = Int(length(iter))::Int
n = _Int(length(iter))
i = lastindex(a)
sizehint!(a, length(a) + n; shrink=false)
for item in iter
Expand Down Expand Up @@ -1446,7 +1446,7 @@ prepend!(a::AbstractVector, iter...) = (for v = reverse(iter); prepend!(a, v); e
function _prepend!(a::Vector, ::Union{HasLength,HasShape}, iter)
@_terminates_locally_meta
require_one_based_indexing(a)
n = Int(length(iter))::Int
n = _Int(length(iter))
sizehint!(a, length(a) + n; first=true, shrink=false)
n = 0
for item in iter
Expand Down Expand Up @@ -1497,7 +1497,7 @@ julia> a[1:6]
```
"""
function resize!(a::Vector, nl_::Integer)
nl = Int(nl_)::Int
nl = _Int(nl_)
l = length(a)
if nl > l
_growend!(a, nl-l)
Expand Down
2 changes: 1 addition & 1 deletion base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
end

function _print_matrix(io, @nospecialize(X::AbstractVecOrMat), pre, sep, post, hdots, vdots, ddots, hmod, vmod, rowsA, colsA)
hmod, vmod = Int(hmod)::Int, Int(vmod)::Int
hmod, vmod = _Int(hmod), _Int(vmod)
ncols, idxlast = length(colsA), last(colsA)
if !(get(io, :limit, false)::Bool)
screenheight = screenwidth = typemax(Int)
Expand Down
3 changes: 2 additions & 1 deletion base/binaryplatforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export AbstractPlatform, Platform, HostPlatform, platform_dlext, tags, arch, os,
detect_libstdcxx_version, detect_cxxstring_abi, call_abi, wordsize, triplet,
select_platform, platforms_match, platform_name
import .Libc.Libdl
using .._ConstructingFunctions

### Submodule with information about CPU features
include("cpuid.jl")
Expand Down Expand Up @@ -116,7 +117,7 @@ function Platform(arch::String, os::String;
validate_strict::Bool = false,
compare_strategies::Dict{String,<:Function} = Dict{String,Function}(),
kwargs...)
tags = Dict{String,Any}(String(tag)::String=>tagvalue(value) for (tag, value) in kwargs)
tags = Dict{String,Any}(_String(tag)=>tagvalue(value) for (tag, value) in kwargs)
return Platform(arch, os, tags; validate_strict, compare_strategies)
end

Expand Down
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ function vcat(A::BitMatrix...)
end

# general case, specialized for BitArrays and Integers
_cat(dims::Integer, X::Union{BitArray, Bool}...) = _cat(Int(dims)::Int, X...)
_cat(dims::Integer, X::Union{BitArray, Bool}...) = _cat(_Int(dims), X...)
function _cat(dims::Int, X::Union{BitArray, Bool}...)
dims = Int(dims)
catdims = dims2cat(dims)
Expand Down
3 changes: 2 additions & 1 deletion base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using .Base.Cartesian
using .Base: Indices, OneTo, tail, to_shape, isoperator, promote_typejoin, promote_typejoin_union,
_msk_end, unsafe_bitgetindex, bitcache_chunks, bitcache_size, dumpbitcache, unalias, negate
import .Base: copy, copyto!, axes
using .._ConstructingFunctions
export broadcast, broadcast!, BroadcastStyle, broadcast_axes, broadcastable, dotview, @__dot__, BroadcastFunction

## Computing the result's axes: deprecated name
Expand Down Expand Up @@ -1291,7 +1292,7 @@ function __dot__(x::Expr)
tmp = x.head === :(<:) ? :.<: : :.>:
Expr(:call, tmp, dotargs...)
else
head = String(x.head)::String
head = _String(x.head)
if last(head) == '=' && first(head) != '.' || head == "&&" || head == "||"
Expr(Symbol('.', head), dotargs...)
else
Expand Down
2 changes: 1 addition & 1 deletion base/cmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function cstr(s)
if Base.containsnul(s)
throw(ArgumentError("strings containing NUL cannot be passed to spawned processes"))
end
return String(s)::String
return _String(s)
end

# convert various env representations into an array of "key=val" strings
Expand Down
36 changes: 36 additions & 0 deletions base/constructingfunctions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Constructing functions for some concrete types.
#
# Meant to be used to improve inference, when the input arguments are not necessarily
# precisely inferred. Partial workaround for issue #42372.
module _ConstructingFunctions
export
_Bool,
_Int, _Int8, _Int16, _Int32, _Int64, _Int128,
_UInt, _UInt8, _UInt16, _UInt32, _UInt64, _UInt128,
_Char, _String
struct ConstructorStrict{F} <: Function end
# Keyword arguments are not available at this point in bootstrap, so they're not
# supported.
function (::ConstructorStrict{F})(args::Vararg{Any, N}) where {F, N}
@inline
r = F(args...)
r::F
end
Comment on lines +11 to +18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a historical note, soon after closing this PR I realized there's a nicer way of doing this, based on preexisting functionality and more generic: Base.Fix2(typeassert, F) ∘ F. Registered package based on this idea:

const _Bool = ConstructorStrict{Bool}()
const _Int = ConstructorStrict{Int}()
const _Int8 = ConstructorStrict{Int8}()
const _Int16 = ConstructorStrict{Int16}()
const _Int32 = ConstructorStrict{Int32}()
const _Int64 = ConstructorStrict{Int64}()
const _Int128 = ConstructorStrict{Int128}()
const _UInt = ConstructorStrict{UInt}()
const _UInt8 = ConstructorStrict{UInt8}()
const _UInt16 = ConstructorStrict{UInt16}()
const _UInt32 = ConstructorStrict{UInt32}()
const _UInt64 = ConstructorStrict{UInt64}()
const _UInt128 = ConstructorStrict{UInt128}()
const _Char = ConstructorStrict{Char}()
const _String = ConstructorStrict{String}()
end

using ._ConstructingFunctions
6 changes: 3 additions & 3 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mutable struct Dict{K,V} <: AbstractDict{K,V}
end
function Dict{K,V}(kv) where V where K
h = Dict{K,V}()
haslength(kv) && sizehint!(h, Int(length(kv))::Int)
haslength(kv) && sizehint!(h, _Int(length(kv)))
for (k,v) in kv
h[k] = v
end
Expand Down Expand Up @@ -125,7 +125,7 @@ _shorthash7(hsh::UInt) = (hsh >> (8sizeof(UInt)-7))%UInt8 | 0x80
# idx - optimal position in the hash table
# sh::UInt8 - short hash (7 highest hash bits)
function hashindex(key, sz::Integer)
sz = Int(sz)::Int
sz = _Int(sz)
hsh = hash(key)::UInt
idx = ((hsh % Int) & (sz-1)) + 1
return idx, _shorthash7(hsh)
Expand Down Expand Up @@ -192,7 +192,7 @@ end
end

function sizehint!(d::Dict{T}, newsz::Integer; shrink::Bool=true) where T
newsz = Int(newsz)::Int
newsz = _Int(newsz)
oldsz = length(d.slots)
# limit new element count to max_values of the key type
newsz = min(max(newsz, length(d)), max_values(T)::Int)
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ function cptree(src::String, dst::String; force::Bool=false,
end
end
cptree(src::AbstractString, dst::AbstractString; kwargs...) =
cptree(String(src)::String, String(dst)::String; kwargs...)
cptree(_String(src), _String(dst); kwargs...)

"""
cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false)
Expand Down
2 changes: 1 addition & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function exponent_raw_max end
"""
IEEE 754 definition of the minimum exponent.
"""
ieee754_exponent_min(::Type{T}) where {T<:IEEEFloat} = Int(1 - exponent_max(T))::Int
ieee754_exponent_min(::Type{T}) where {T<:IEEEFloat} = _Int(1 - exponent_max(T))

exponent_min(::Type{Float16}) = ieee754_exponent_min(Float16)
exponent_min(::Type{Float32}) = ieee754_exponent_min(Float32)
Expand Down
16 changes: 8 additions & 8 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mutable struct GenericIOBuffer{T<:AbstractVector{UInt8}} <: IO
append::Bool,
maxsize::Int,
) where T<:AbstractVector{UInt8}
len = Int(length(data))::Int
len = _Int(length(data))
return new{T}(data, false, readable, writable, seekable, append, len, maxsize, 1, 0, -1)
end
end
Expand All @@ -117,8 +117,8 @@ function GenericIOBuffer{T}(
truncate::Bool,
) where T<:AbstractVector{UInt8}
require_one_based_indexing(data)
mz = Int(maxsize)::Int
len = Int(length(data))::Int
mz = _Int(maxsize)
len = _Int(length(data))
if !truncate && mz < len
throw(ArgumentError("maxsize must not be smaller than data length"))
end
Expand All @@ -145,7 +145,7 @@ function GenericIOBuffer(data::Vector{UInt8}, readable::Bool, writable::Bool, se
offset = memoryrefoffset(ref) - 1
# The user may pass a vector of length <= maxsize, but where the underlying memory
# is larger than maxsize. Don't throw an error in that case.
mz = Int(maxsize)::Int
mz = _Int(maxsize)
if !truncate && mz < length(data)
throw(ArgumentError("maxsize must not be smaller than data length"))
end
Expand Down Expand Up @@ -250,13 +250,13 @@ function IOBuffer(;
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing,
)
mz = Int(maxsize)::Int
mz = _Int(maxsize)
if mz < 0
throw(ArgumentError("negative maxsize"))
end
size = if sizehint !== nothing
# Allow negative sizehint, just like `sizehint!` does
min(mz, max(0, Int(sizehint)::Int))
min(mz, max(0, _Int(sizehint)))
else
min(mz, 32)
end
Expand Down Expand Up @@ -561,7 +561,7 @@ function truncate(io::GenericIOBuffer, n::Integer)
io.seekable || throw(ArgumentError("truncate failed, IOBuffer is not seekable"))
n < 0 && throw(ArgumentError("truncate failed, n bytes must be ≥ 0, got $n"))
n > io.maxsize && throw(ArgumentError("truncate failed, $(n) bytes is exceeds IOBuffer maxsize $(io.maxsize)"))
n = Int(n)::Int
n = _Int(n)
offset = get_offset(io)
current_size = io.size - offset
if io.reinit
Expand Down Expand Up @@ -898,7 +898,7 @@ function unsafe_write(to::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
append = to.append
ptr = append ? size+1 : to.ptr
data = to.data
to_write = min(nb, (min(Int(length(data))::Int, to.maxsize + get_offset(to)) - ptr + 1) % UInt) % Int
to_write = min(nb, (min(_Int(length(data)), to.maxsize + get_offset(to)) - ptr + 1) % UInt) % Int
# Dispatch based on the type of data, to possibly allow using memcpy
_unsafe_write(data, p, ptr, to_write % UInt)
# Update to.size only if the ptr has advanced to higher than
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt128)
try
modpath = locate_package(modkey)
isnothing(modpath) && error("Cannot locate source for $(repr("text/plain", modkey))")
modpath = String(modpath)::String
modpath = _String(modpath)
set_pkgorigin_version_path(modkey, modpath)
loaded = _require_search_from_serialized(modkey, modpath, build_id, true)
finally
Expand Down
2 changes: 1 addition & 1 deletion base/logging/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
maxlog = get(kwargs, :maxlog, nothing)
if maxlog isa Core.BuiltinInts
@lock logger.lock begin
remaining = get!(logger.message_limits, id, Int(maxlog)::Int)
remaining = get!(logger.message_limits, id, _Int(maxlog))
remaining == 0 && return
logger.message_limits[id] = remaining - 1
end
Expand Down
3 changes: 2 additions & 1 deletion base/logging/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module CoreLogging

import Base: isless, +, -, convert, show
import Base.ScopedValues: ScopedValue, with, @with
using .._ConstructingFunctions

export
AbstractLogger,
Expand Down Expand Up @@ -696,7 +697,7 @@ function handle_message(logger::SimpleLogger, level::LogLevel, message, _module,
maxlog = get(kwargs, :maxlog, nothing)
if maxlog isa Core.BuiltinInts
@lock logger.lock begin
remaining = get!(logger.message_limits, id, Int(maxlog)::Int)
remaining = get!(logger.message_limits, id, _Int(maxlog))
remaining == 0 && return
logger.message_limits[id] = remaining - 1
end
Expand Down
4 changes: 3 additions & 1 deletion base/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public parse

import Base: isexpr

using .._ConstructingFunctions

## AST decoding helpers ##

is_id_start_char(c::AbstractChar) = ccall(:jl_id_start_char, Cint, (UInt32,), c) != 0
Expand Down Expand Up @@ -115,7 +117,7 @@ julia> Meta.ispostfixoperator(Symbol("'")), Meta.ispostfixoperator(Symbol("'ᵀ"
```
"""
function ispostfixoperator(s::Union{Symbol,AbstractString})
s = String(s)::String
s = _String(s)
return startswith(s, '\'') && all(is_op_suffix_char, SubString(s, 2))
end

Expand Down
3 changes: 2 additions & 1 deletion base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module PCRE

import ..RefValue
using .._ConstructingFunctions

# include($BUILDROOT/base/pcre_h.jl)
include(string(Base.BUILDROOT, "pcre_h.jl"))
Expand Down Expand Up @@ -199,7 +200,7 @@ end
exec(re, subject::Union{String,SubString{String}}, offset, options, match_data) =
_exec(re, subject, offset, options, match_data)
exec(re, subject, offset, options, match_data) =
_exec(re, String(subject)::String, offset, options, match_data)
_exec(re, _String(subject), offset, options, match_data)

function _exec(re, subject, offset, options, match_data)
rc = ccall((:pcre2_match_8, PCRE_LIB), Cint,
Expand Down
2 changes: 1 addition & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ end

Run `command` and return the resulting output as a `String`.
"""
read(cmd::AbstractCmd, ::Type{String}) = String(read(cmd))::String
read(cmd::AbstractCmd, ::Type{String}) = _String(read(cmd))

"""
run(command, args...; wait::Bool = true)
Expand Down
2 changes: 1 addition & 1 deletion base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mutable struct Regex <: AbstractPattern

function Regex(pattern::AbstractString, compile_options::Integer,
match_options::Integer)
pattern = String(pattern)::String
pattern = _String(pattern)
compile_options = UInt32(compile_options)
match_options = UInt32(match_options)
if (compile_options & ~PCRE.COMPILE_MASK) != 0
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
end
print(io, ")")
else
escape_string(io, String(x)::String, "\"\$")
escape_string(io, _String(x), "\"\$")
end
end
print(io, '"')
Expand Down
Loading