Skip to content

Commit

Permalink
Fix invalidations from novel Integer conversions
Browse files Browse the repository at this point in the history
Defining

    struct MyInt <: Integer
        x::Int
    end
    (::Type{T})(x::MyInt) where T<:Integer = T(x.x)

triggers about 830 unique method invalidations. This fixes the majority
of them, but it's a lot of type-annotation.
  • Loading branch information
timholy committed Aug 2, 2020
1 parent 1888e31 commit 66f4375
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 72 deletions.
6 changes: 3 additions & 3 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ julia> count_ones(7)
3
```
"""
count_ones(x::BitInteger) = ctpop_int(x) % Int
count_ones(x::BitInteger) = (ctpop_int(x) % Int)::Int

"""
leading_zeros(x::Integer) -> Integer
Expand All @@ -382,7 +382,7 @@ julia> leading_zeros(Int32(1))
31
```
"""
leading_zeros(x::BitInteger) = ctlz_int(x) % Int
leading_zeros(x::BitInteger) = (ctlz_int(x) % Int)::Int

"""
trailing_zeros(x::Integer) -> Integer
Expand All @@ -395,7 +395,7 @@ julia> trailing_zeros(2)
1
```
"""
trailing_zeros(x::BitInteger) = cttz_int(x) % Int
trailing_zeros(x::BitInteger) = (cttz_int(x) % Int)::Int

"""
count_zeros(x::Integer) -> Integer
Expand Down
6 changes: 2 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,7 @@ A list of modules can also be specified as an array.
At least Julia 1.4 is required for specifying a module.
"""
function methods(@nospecialize(f), @nospecialize(t),
@nospecialize(mod::Union{Module,AbstractArray{Module},Nothing}=nothing))
if mod isa Module
mod = (mod,)
end
@nospecialize(mod::Union{Tuple{Module},AbstractArray{Module},Nothing}=nothing))
if isa(f, Core.Builtin)
throw(ArgumentError("argument is not a generic function"))
end
Expand All @@ -858,6 +855,7 @@ function methods(@nospecialize(f), @nospecialize(t),
MethodList(Method[m.method for m in _methods(f, t, -1, world) if mod === nothing || m.method.module in mod],
typeof(f).name.mt)
end
methods(@nospecialize(f), @nospecialize(t), mod::Module) = methods(f, t, (mod,))

methods(f::Core.Builtin) = MethodList(Method[], typeof(f).name.mt)

Expand Down
6 changes: 3 additions & 3 deletions base/refvalue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function unsafe_convert(P::Type{Ptr{T}}, b::RefValue{T}) where T
# which also ensures this returns same pointer as the one rooted in the `RefValue` object.
p = pointerref(Ptr{Ptr{Cvoid}}(pointer_from_objref(b)), 1, Core.sizeof(Ptr{Cvoid}))
end
return convert(P, p)
return convert(P, p)::Ptr{T}
end
function unsafe_convert(P::Type{Ptr{Any}}, b::RefValue{Any})
return convert(P, pointer_from_objref(b))
return convert(P, pointer_from_objref(b))::Ptr{Any}
end
unsafe_convert(::Type{Ptr{Cvoid}}, b::RefValue{T}) where {T} = convert(Ptr{Cvoid}, unsafe_convert(Ptr{T}, b))
unsafe_convert(::Type{Ptr{Cvoid}}, b::RefValue{T}) where {T} = convert(Ptr{Cvoid}, unsafe_convert(Ptr{T}, b))::Ptr{Cvoid}

getindex(b::RefValue) = b.x
setindex!(b::RefValue, x) = (b.x = x; b)
10 changes: 5 additions & 5 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,11 @@ struct SubstitutionString{T<:AbstractString} <: AbstractString
string::T
end

ncodeunits(s::SubstitutionString) = ncodeunits(s.string)
codeunit(s::SubstitutionString) = codeunit(s.string)
codeunit(s::SubstitutionString, i::Integer) = codeunit(s.string, i)
isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)
iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)
ncodeunits(s::SubstitutionString) = ncodeunits(s.string)::Int
codeunit(s::SubstitutionString) = codeunit(s.string)::Type{<:Union{UInt8, UInt16, UInt32}}
codeunit(s::SubstitutionString, i::Integer) = codeunit(s.string, i)::Union{UInt8, UInt16, UInt32}
isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)::Bool
iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)::Union{Nothing,Tuple{AbstractChar,Int}}

function show(io::IO, s::SubstitutionString)
print(io, "s")
Expand Down
8 changes: 4 additions & 4 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const shell_special = "#{}()[]<>|&*?~;"
# strips the end but respects the space when the string ends with "\\ "
function rstrip_shell(s::AbstractString)
c_old = nothing
for (i, c) in Iterators.reverse(pairs(s))
for (i::Int, c::AbstractChar) in Iterators.reverse(pairs(s))
((c == '\\') && c_old == ' ') && return SubString(s, 1, i+1)
isspace(c) || return SubString(s, 1, i)
c_old = c
Expand Down Expand Up @@ -38,16 +38,16 @@ function shell_parse(str::AbstractString, interpolate::Bool=true;
end
end
function consume_upto(s, i, j)
update_arg(s[i:prevind(s, j)])
something(peek(st), (lastindex(s)+1,'\0'))[1]
update_arg(s[i:prevind(s, j)::Int])
something(peek(st), (lastindex(s)::Int+1,'\0'))[1]
end
function append_arg()
if isempty(arg); arg = Any["",]; end
push!(args, arg)
arg = []
end

for (j, c) in st
for (j::Int, c::AbstractChar) in st
if !in_single_quotes && !in_double_quotes && isspace(c)
i = consume_upto(s, i, j)
append_arg()
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ function _show_default(io::IO, @nospecialize(x))
show(io, inferencebarrier(t))
print(io, '(')
nf = nfields(x)
nb = sizeof(x)
nb = sizeof(x)::Int
if nf != 0 || nb == 0
if !show_circular(io, x)
recur_io = IOContext(io, Pair{Symbol,Any}(:SHOWN_SET, x),
Expand Down
12 changes: 6 additions & 6 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ julia> sizeof("∀")
3
```
"""
sizeof(s::AbstractString) = ncodeunits(s) * sizeof(codeunit(s))
sizeof(s::AbstractString) = (ncodeunits(s) * sizeof(codeunit(s)))::Int
firstindex(s::AbstractString) = 1
lastindex(s::AbstractString) = thisind(s, ncodeunits(s))
isempty(s::AbstractString) = iszero(ncodeunits(s))
lastindex(s::AbstractString) = thisind(s, ncodeunits(s)::Int)
isempty(s::AbstractString) = iszero(ncodeunits(s)::Int)

function getindex(s::AbstractString, i::Integer)
@boundscheck checkbounds(s, i)
Expand Down Expand Up @@ -434,7 +434,7 @@ ERROR: BoundsError: attempt to access 2-codeunit String at index [-1]
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i))

function thisind(s::AbstractString, i::Int)
z = ncodeunits(s) + 1
z = ncodeunits(s)::Int + 1
i == z && return i
@boundscheck 0  i z || throw(BoundsError(s, i))
@inbounds while 1 < i && !(isvalid(s, i)::Bool)
Expand Down Expand Up @@ -602,9 +602,9 @@ isascii(c::AbstractChar) = UInt32(c) < 0x80
## string map, filter ##

function map(f, s::AbstractString)
out = StringVector(max(4, sizeof(s)÷sizeof(codeunit(s))))
out = StringVector(max(4, sizeof(s)::Int÷sizeof(codeunit(s)::Type{<:Union{UInt8,UInt16,UInt32}})))
index = UInt(1)
for c in s
for c::AbstractChar in s
c′ = f(c)
isa(c′, AbstractChar) || throw(ArgumentError(
"map(f, s::AbstractString) requires f to return AbstractChar; " *
Expand Down
10 changes: 5 additions & 5 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ julia> escape_string(string('\\u2135','\\0','0')) # \\0 would be ambiguous
"""
function escape_string(io::IO, s::AbstractString, esc="")
a = Iterators.Stateful(s)
for c in a
for c::AbstractChar in a
if c in esc
print(io, '\\', c)
elseif isascii(c)
c == '\0' ? print(io, escape_nul(peek(a))) :
c == '\0' ? print(io, escape_nul(peek(a)::Union{AbstractChar,Nothing})) :
c == '\e' ? print(io, "\\e") :
c == '\\' ? print(io, "\\\\") :
'\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) :
Expand All @@ -356,10 +356,10 @@ function escape_string(io::IO, s::AbstractString, esc="")
elseif !isoverlong(c) && !ismalformed(c)
isprint(c) ? print(io, c) :
c <= '\x7f' ? print(io, "\\x", string(UInt32(c), base = 16, pad = 2)) :
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 4 : 2)) :
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 8 : 4))
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 4 : 2)) :
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 8 : 4))
else # malformed or overlong
u = bswap(reinterpret(UInt32, c))
u = bswap(reinterpret(UInt32, c)::UInt32)
while true
print(io, "\\x", string(u % UInt8, base = 16, pad = 2))
(u >>= 8) == 0 && break
Expand Down
4 changes: 2 additions & 2 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ function _searchindex(s::Union{AbstractString,ByteArray},
t::Union{AbstractString,AbstractChar,Int8,UInt8},
i::Integer)
if isempty(t)
return 1 <= i <= nextind(s,lastindex(s)) ? i :
return 1 <= i <= nextind(s,lastindex(s))::Int ? i :
throw(BoundsError(s, i))
end
t1, trest = Iterators.peel(t)
while true
i = findnext(isequal(t1),s,i)
if i === nothing return 0 end
ii = nextind(s, i)
ii = nextind(s, i)::Int
a = Iterators.Stateful(trest)
matched = all(splat(==), zip(SubString(s, ii), a))
(isempty(a) && matched) && return i
Expand Down
10 changes: 5 additions & 5 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ end
SubString(s.string, s.offset+i, s.offset+j)
end

SubString(s::AbstractString) = SubString(s, 1, lastindex(s))
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s))
SubString(s::AbstractString) = SubString(s, 1, lastindex(s)::Int)
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s)::Int)

@propagate_inbounds view(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)
@propagate_inbounds maybeview(s::AbstractString, r::AbstractUnitRange{<:Integer}) = view(s, r)
Expand All @@ -62,7 +62,7 @@ function String(s::SubString{String})
end

ncodeunits(s::SubString) = s.ncodeunits
codeunit(s::SubString) = codeunit(s.string)
codeunit(s::SubString) = codeunit(s.string)::Type{<:Union{UInt8, UInt16, UInt32}}
length(s::SubString) = length(s.string, s.offset+1, s.offset+s.ncodeunits)

function codeunit(s::SubString, i::Integer)
Expand All @@ -75,7 +75,7 @@ function iterate(s::SubString, i::Integer=firstindex(s))
@boundscheck checkbounds(s, i)
y = iterate(s.string, s.offset + i)
y === nothing && return nothing
c, i = y
c::AbstractChar, i::Int = y
return c, i - s.offset
end

Expand All @@ -87,7 +87,7 @@ end
function isvalid(s::SubString, i::Integer)
ib = true
@boundscheck ib = checkbounds(Bool, s, i)
@inbounds return ib && isvalid(s.string, s.offset + i)
@inbounds return ib && isvalid(s.string, s.offset + i)::Bool
end

byte_string_classify(s::SubString{String}) =
Expand Down
28 changes: 14 additions & 14 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ julia> rstrip(a)
```
"""
function rstrip(f, s::AbstractString)
for (i, c) in Iterators.reverse(pairs(s))
for (i::Int, c::AbstractChar) in Iterators.reverse(pairs(s))
f(c) || return @inbounds SubString(s, 1, i)
end
SubString(s, 1, 0)
Expand Down Expand Up @@ -389,26 +389,26 @@ function split(str::T, splitter::AbstractChar;
_split(str, isequal(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
end

function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Vector)
i = 1 # firstindex(str)
n = lastindex(str)
r = findfirst(splitter,str)
n = lastindex(str)::Int
r = findfirst(splitter,str)::Union{Nothing,Int,UnitRange{Int}}
if !isnothing(r)
j, k = first(r), nextind(str,last(r))
j, k = first(r), nextind(str,last(r))::Int
while 0 < j <= n && length(strs) != limit-1
if i < k
if keepempty || i < j
push!(strs, @inbounds SubString(str,i,prevind(str,j)))
end
i = k
end
(k <= j) && (k = nextind(str,j))
r = findnext(splitter,str,k)
(k <= j) && (k = nextind(str,j)::Int)
r = findnext(splitter,str,k)::Union{Nothing,Int,UnitRange{Int}}
isnothing(r) && break
j, k = first(r), nextind(str,last(r))
j, k = first(r), nextind(str,last(r))::Int
end
end
if keepempty || i <= ncodeunits(str)
if keepempty || i <= ncodeunits(str)::Int
push!(strs, @inbounds SubString(str,i))
end
return strs
Expand Down Expand Up @@ -464,13 +464,13 @@ function rsplit(str::T, splitter::AbstractChar;
end

function _rsplit(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
n = lastindex(str)
r = something(findlast(splitter, str), 0)
n = lastindex(str)::Int
r = something(findlast(splitter, str)::Union{Nothing,Int,UnitRange{Int}}, 0)
j, k = first(r), last(r)
while j > 0 && k > 0 && length(strs) != limit-1
(keepempty || k < n) && pushfirst!(strs, @inbounds SubString(str,nextind(str,k),n))
n = prevind(str, j)
r = something(findprev(splitter,str,n), 0)
(keepempty || k < n) && pushfirst!(strs, @inbounds SubString(str,nextind(str,k)::Int,n))
n = prevind(str, j)::Int
r = something(findprev(splitter,str,n)::Union{Nothing,Int,UnitRange{Int}}, 0)
j, k = first(r), last(r)
end
(keepempty || n > 0) && pushfirst!(strs, SubString(str,1,n))
Expand Down
4 changes: 2 additions & 2 deletions stdlib/Distributed/src/managers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)

# master connecting to workers
if config.io !== nothing
(bind_addr, port) = read_worker_host_port(config.io)
(bind_addr, port::Int) = read_worker_host_port(config.io)
pubhost = something(config.host, bind_addr)
config.host = pubhost
config.port = port
Expand Down Expand Up @@ -503,7 +503,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)
end

function connect_w2w(pid::Int, config::WorkerConfig)
(rhost, rport) = notnothing(config.connect_at)
(rhost, rport) = notnothing(config.connect_at)::Tuple{AbstractString, Int}
config.host = rhost
config.port = rport
(s, bind_addr) = connect_to_worker(rhost, rport)
Expand Down
6 changes: 2 additions & 4 deletions stdlib/Logging/src/ConsoleLogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i
# Generate a text representation of the message and all key value pairs,
# split into lines.
msglines = [(indent=0,msg=l) for l in split(chomp(string(message)), '\n')]
dsize = displaysize(logger.stream)
dsize = displaysize(logger.stream)::Tuple{Int,Int}
if !isempty(kwargs)
valbuf = IOBuffer()
rows_per_value = max(1, dsize[1]÷(length(kwargs)+1))
Expand All @@ -127,9 +127,7 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i

# Format lines as text with appropriate indentation and with a box
# decoration on the left.
color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line)
color = convert(Symbol, color)::Symbol
prefix, suffix = convert(String, prefix)::String, convert(String, suffix)::String
color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line)::Tuple{Union{Symbol,Int},String,String}
minsuffixpad = 2
buf = IOBuffer()
iob = IOContext(buf, logger.stream)
Expand Down
28 changes: 19 additions & 9 deletions stdlib/Markdown/src/render/terminal/formatting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ end
words(s) = split(s, " ")
lines(s) = split(s, "\n")

# This could really be more efficient
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
if occursin(r"\n", s)
return vcat(map(s->wrapped_lines(io, s, width = width, i = i), split(s, "\n"))...)
end
function wrapped_lines!(lines, io::IO, s::AbstractString, width, i)
ws = words(s)
lines = AbstractString[ws[1]]
i += ws[1] |> ansi_length
for word in ws[2:end]
for word in ws
word_length = ansi_length(word)
if i + word_length + 1 > width
i = word_length
push!(lines, word)
else
i += word_length + 1
lines[end] *= " " * word
if isempty(lines)
push!(lines, word)
else
lines[end] *= " " * word # this could be more efficient
end
end
end
return i
end

function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
lines = AbstractString[]
if occursin(r"\n", s)
for ss in split(s, "\n")
i = wrapped_lines!(lines, io, ss, width, i)
end
else
wrapped_lines!(lines, io, s, width, i)
end
return lines
end
Expand Down
10 changes: 5 additions & 5 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1587,11 +1587,11 @@ with string types besides the standard `String` type.
struct GenericString <: AbstractString
string::AbstractString
end
Base.ncodeunits(s::GenericString) = ncodeunits(s.string)
Base.codeunit(s::GenericString) = codeunit(s.string)
Base.codeunit(s::GenericString, i::Integer) = codeunit(s.string, i)
Base.isvalid(s::GenericString, i::Integer) = isvalid(s.string, i)
Base.iterate(s::GenericString, i::Integer=1) = iterate(s.string, i)
Base.ncodeunits(s::GenericString) = ncodeunits(s.string)::Int
Base.codeunit(s::GenericString) = codeunit(s.string)::Type{<:Union{UInt8, UInt16, UInt32}}
Base.codeunit(s::GenericString, i::Integer) = codeunit(s.string, i)::Union{UInt8, UInt16, UInt32}
Base.isvalid(s::GenericString, i::Integer) = isvalid(s.string, i)::Bool
Base.iterate(s::GenericString, i::Integer=1) = iterate(s.string, i)::Union{Nothing,Tuple{AbstractChar,Int}}
Base.reverse(s::GenericString) = GenericString(reverse(s.string))
Base.reverse(s::SubString{GenericString}) =
GenericString(typeof(s.string)(reverse(String(s))))
Expand Down

0 comments on commit 66f4375

Please sign in to comment.