Skip to content

Commit a53e6d5

Browse files
committed
Fix invalidations from novel Integer conversions
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 pretty horrifying amount of type-annotation.
1 parent d762e8c commit a53e6d5

File tree

16 files changed

+84
-76
lines changed

16 files changed

+84
-76
lines changed

base/int.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ julia> count_ones(7)
369369
3
370370
```
371371
"""
372-
count_ones(x::BitInteger) = ctpop_int(x) % Int
372+
count_ones(x::BitInteger) = (ctpop_int(x) % Int)::Int
373373

374374
"""
375375
leading_zeros(x::Integer) -> Integer
@@ -382,7 +382,7 @@ julia> leading_zeros(Int32(1))
382382
31
383383
```
384384
"""
385-
leading_zeros(x::BitInteger) = ctlz_int(x) % Int
385+
leading_zeros(x::BitInteger) = (ctlz_int(x) % Int)::Int
386386

387387
"""
388388
trailing_zeros(x::Integer) -> Integer
@@ -395,7 +395,7 @@ julia> trailing_zeros(2)
395395
1
396396
```
397397
"""
398-
trailing_zeros(x::BitInteger) = cttz_int(x) % Int
398+
trailing_zeros(x::BitInteger) = (cttz_int(x) % Int)::Int
399399

400400
"""
401401
count_zeros(x::Integer) -> Integer

base/intfuncs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ function bin(x::Unsigned, pad::Integer, neg::Bool)
605605
i = neg + max(pad,sizeof(x)<<3-leading_zeros(x))
606606
a = StringVector(i)
607607
while i > neg
608-
@inbounds a[i] = 48+(x&0x1)
609-
x >>= 1
608+
@inbounds a[i] = (0x30+(x&0x1)::Unsigned)::Unsigned
609+
x = (x >> 1)::Unsigned
610610
i -= 1
611611
end
612612
if neg; @inbounds a[1]=0x2d; end

base/reflection.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ function _methods_by_ftype(@nospecialize(t), lim::Int, world::UInt)
838838
return _methods_by_ftype(t, lim, world, UInt[typemin(UInt)], UInt[typemax(UInt)])
839839
end
840840
function _methods_by_ftype(@nospecialize(t), lim::Int, world::UInt, min::Array{UInt,1}, max::Array{UInt,1})
841-
return ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), t, lim, 0, world, min, max)
841+
return ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), t, lim, 0, world, min, max)::Union{Vector{Any},Bool}
842842
end
843843

844844
# high-level, more convenient method lookup functions
@@ -875,18 +875,22 @@ A list of modules can also be specified as an array.
875875
At least Julia 1.4 is required for specifying a module.
876876
"""
877877
function methods(@nospecialize(f), @nospecialize(t),
878-
@nospecialize(mod::Union{Module,AbstractArray{Module},Nothing}=nothing))
879-
if mod isa Module
880-
mod = (mod,)
881-
end
878+
@nospecialize(mod::Union{Tuple{Module},AbstractArray{Module},Nothing}=nothing))
882879
if isa(f, Core.Builtin)
883880
throw(ArgumentError("argument is not a generic function"))
884881
end
885882
t = to_tuple_type(t)
886883
world = typemax(UInt)
887-
MethodList(Method[m[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module in mod],
888-
typeof(f).name.mt)
884+
ms = Method[]
885+
for sig_params_meth in _methods(f, t, -1, world)
886+
meth = sig_params_meth[3]::Method
887+
if mod === nothing || meth.module mod
888+
push!(ms, meth)
889+
end
890+
end
891+
return MethodList(ms, typeof(f).name.mt)
889892
end
893+
methods(@nospecialize(f), @nospecialize(t), mod::Module) = methods(f, t, (mod,))
890894

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

base/refvalue.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ function unsafe_convert(P::Type{Ptr{T}}, b::RefValue{T}) where T
2222
# which also ensures this returns same pointer as the one rooted in the `RefValue` object.
2323
p = pointerref(Ptr{Ptr{Cvoid}}(pointer_from_objref(b)), 1, Core.sizeof(Ptr{Cvoid}))
2424
end
25-
return convert(P, p)
25+
return convert(P, p)::Ptr{T}
2626
end
2727
function unsafe_convert(P::Type{Ptr{Any}}, b::RefValue{Any})
28-
return convert(P, pointer_from_objref(b))
28+
return convert(P, pointer_from_objref(b))::Ptr{Any}
2929
end
30-
unsafe_convert(::Type{Ptr{Cvoid}}, b::RefValue{T}) where {T} = convert(Ptr{Cvoid}, unsafe_convert(Ptr{T}, b))
30+
unsafe_convert(::Type{Ptr{Cvoid}}, b::RefValue{T}) where {T} = convert(Ptr{Cvoid}, unsafe_convert(Ptr{T}, b))::Ptr{Cvoid}
3131

3232
getindex(b::RefValue) = b.x
3333
setindex!(b::RefValue, x) = (b.x = x; b)

base/regex.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ struct SubstitutionString{T<:AbstractString} <: AbstractString
422422
string::T
423423
end
424424

425-
ncodeunits(s::SubstitutionString) = ncodeunits(s.string)
426-
codeunit(s::SubstitutionString) = codeunit(s.string)
427-
codeunit(s::SubstitutionString, i::Integer) = codeunit(s.string, i)
428-
isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)
429-
iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)
425+
ncodeunits(s::SubstitutionString) = ncodeunits(s.string)::Int
426+
codeunit(s::SubstitutionString) = codeunit(s.string)::Type{<:Union{UInt8, UInt16, UInt32}}
427+
codeunit(s::SubstitutionString, i::Integer) = codeunit(s.string, i)::Union{UInt8, UInt16, UInt32}
428+
isvalid(s::SubstitutionString, i::Integer) = isvalid(s.string, i)::Bool
429+
iterate(s::SubstitutionString, i::Integer...) = iterate(s.string, i...)::Union{Nothing,Tuple{AbstractChar,Int}}
430430

431431
function show(io::IO, s::SubstitutionString)
432432
print(io, "s")

base/shell.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const shell_special = "#{}()[]<>|&*?~;"
77
# strips the end but respects the space when the string ends with "\\ "
88
function rstrip_shell(s::AbstractString)
99
c_old = nothing
10-
for (i, c) in Iterators.reverse(pairs(s))
10+
for (i::Int, c::AbstractChar) in Iterators.reverse(pairs(s))
1111
((c == '\\') && c_old == ' ') && return SubString(s, 1, i+1)
1212
isspace(c) || return SubString(s, 1, i)
1313
c_old = c
@@ -38,16 +38,16 @@ function shell_parse(str::AbstractString, interpolate::Bool=true;
3838
end
3939
end
4040
function consume_upto(j)
41-
update_arg(s[i:prevind(s, j)])
42-
i = something(peek(st), (lastindex(s)+1,'\0'))[1]
41+
update_arg(s[i:prevind(s, j)::Int])
42+
i = something(peek(st), (lastindex(s)::Int+1,'\0'))[1]
4343
end
4444
function append_arg()
4545
if isempty(arg); arg = Any["",]; end
4646
push!(args, arg)
4747
arg = []
4848
end
4949

50-
for (j, c) in st
50+
for (j::Int, c::AbstractChar) in st
5151
if !in_single_quotes && !in_double_quotes && isspace(c)
5252
consume_upto(j)
5353
append_arg()

base/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ function _show_default(io::IO, @nospecialize(x))
395395
show(io, inferencebarrier(t))
396396
print(io, '(')
397397
nf = nfields(x)
398-
nb = sizeof(x)
398+
nb = sizeof(x)::Int
399399
if nf != 0 || nb == 0
400400
if !show_circular(io, x)
401401
recur_io = IOContext(io, Pair{Symbol,Any}(:SHOWN_SET, x),

base/strings/basic.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ julia> sizeof("∀")
174174
3
175175
```
176176
"""
177-
sizeof(s::AbstractString) = ncodeunits(s) * sizeof(codeunit(s))
177+
sizeof(s::AbstractString) = (ncodeunits(s) * sizeof(codeunit(s)))::Int
178178
firstindex(s::AbstractString) = 1
179-
lastindex(s::AbstractString) = thisind(s, ncodeunits(s))
180-
isempty(s::AbstractString) = iszero(ncodeunits(s))
179+
lastindex(s::AbstractString) = thisind(s, ncodeunits(s)::Int)
180+
isempty(s::AbstractString) = iszero(ncodeunits(s)::Int)
181181

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

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

604604
function map(f, s::AbstractString)
605-
out = StringVector(max(4, sizeof(s)÷sizeof(codeunit(s))))
605+
out = StringVector(max(4, sizeof(s)::Int÷sizeof(codeunit(s)::Type{<:Union{UInt8,UInt16,UInt32}})))
606606
index = UInt(1)
607-
for c in s
607+
for c::AbstractChar in s
608608
c′ = f(c)
609609
isa(c′, AbstractChar) || throw(ArgumentError(
610610
"map(f, s::AbstractString) requires f to return AbstractChar; " *

base/strings/io.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ julia> escape_string(string('\\u2135','\\0','0')) # \\0 would be ambiguous
341341
"""
342342
function escape_string(io::IO, s::AbstractString, esc="")
343343
a = Iterators.Stateful(s)
344-
for c in a
344+
for c::AbstractChar in a
345345
if c in esc
346346
print(io, '\\', c)
347347
elseif isascii(c)
348-
c == '\0' ? print(io, escape_nul(peek(a))) :
348+
c == '\0' ? print(io, escape_nul(peek(a)::Union{AbstractChar,Nothing})) :
349349
c == '\e' ? print(io, "\\e") :
350350
c == '\\' ? print(io, "\\\\") :
351351
'\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) :
@@ -354,10 +354,10 @@ function escape_string(io::IO, s::AbstractString, esc="")
354354
elseif !isoverlong(c) && !ismalformed(c)
355355
isprint(c) ? print(io, c) :
356356
c <= '\x7f' ? print(io, "\\x", string(UInt32(c), base = 16, pad = 2)) :
357-
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 4 : 2)) :
358-
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 8 : 4))
357+
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 4 : 2)) :
358+
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 8 : 4))
359359
else # malformed or overlong
360-
u = bswap(reinterpret(UInt32, c))
360+
u = bswap(reinterpret(UInt32, c)::UInt32)
361361
while true
362362
print(io, "\\x", string(u % UInt8, base = 16, pad = 2))
363363
(u >>= 8) == 0 && break

base/strings/search.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ function _searchindex(s::Union{AbstractString,ByteArray},
144144
t::Union{AbstractString,AbstractChar,Int8,UInt8},
145145
i::Integer)
146146
if isempty(t)
147-
return 1 <= i <= nextind(s,lastindex(s)) ? i :
147+
return 1 <= i <= nextind(s,lastindex(s))::Int ? i :
148148
throw(BoundsError(s, i))
149149
end
150150
t1, trest = Iterators.peel(t)
151151
while true
152152
i = findnext(isequal(t1),s,i)
153153
if i === nothing return 0 end
154-
ii = nextind(s, i)
154+
ii = nextind(s, i)::Int
155155
a = Iterators.Stateful(trest)
156156
matched = all(splat(==), zip(SubString(s, ii), a))
157157
(isempty(a) && matched) && return i

base/strings/substring.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ end
4444
SubString(s.string, s.offset+i, s.offset+j)
4545
end
4646

47-
SubString(s::AbstractString) = SubString(s, 1, lastindex(s))
48-
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s))
47+
SubString(s::AbstractString) = SubString(s, 1, lastindex(s)::Int)
48+
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s)::Int)
4949

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

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

6868
function codeunit(s::SubString, i::Integer)
@@ -75,7 +75,7 @@ function iterate(s::SubString, i::Integer=firstindex(s))
7575
@boundscheck checkbounds(s, i)
7676
y = iterate(s.string, s.offset + i)
7777
y === nothing && return nothing
78-
c, i = y
78+
c::AbstractChar, i::Int = y
7979
return c, i - s.offset
8080
end
8181

@@ -87,7 +87,7 @@ end
8787
function isvalid(s::SubString, i::Integer)
8888
ib = true
8989
@boundscheck ib = checkbounds(Bool, s, i)
90-
@inbounds return ib && isvalid(s.string, s.offset + i)
90+
@inbounds return ib && isvalid(s.string, s.offset + i)::Bool
9191
end
9292

9393
byte_string_classify(s::SubString{String}) =

base/strings/util.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ julia> rstrip(a)
255255
```
256256
"""
257257
function rstrip(f, s::AbstractString)
258-
for (i, c) in Iterators.reverse(pairs(s))
258+
for (i::Int, c::AbstractChar) in Iterators.reverse(pairs(s))
259259
f(c) || return @inbounds SubString(s, 1, i)
260260
end
261261
SubString(s, 1, 0)
@@ -389,26 +389,26 @@ function split(str::T, splitter::AbstractChar;
389389
_split(str, isequal(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
390390
end
391391

392-
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
392+
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Vector)
393393
i = 1 # firstindex(str)
394-
n = lastindex(str)
395-
r = findfirst(splitter,str)
394+
n = lastindex(str)::Int
395+
r = findfirst(splitter,str)::Union{Nothing,Int,UnitRange{Int}}
396396
if !isnothing(r)
397-
j, k = first(r), nextind(str,last(r))
397+
j, k = first(r), nextind(str,last(r))::Int
398398
while 0 < j <= n && length(strs) != limit-1
399399
if i < k
400400
if keepempty || i < j
401401
push!(strs, @inbounds SubString(str,i,prevind(str,j)))
402402
end
403403
i = k
404404
end
405-
(k <= j) && (k = nextind(str,j))
406-
r = findnext(splitter,str,k)
405+
(k <= j) && (k = nextind(str,j)::Int)
406+
r = findnext(splitter,str,k)::Union{Nothing,Int,UnitRange{Int}}
407407
isnothing(r) && break
408-
j, k = first(r), nextind(str,last(r))
408+
j, k = first(r), nextind(str,last(r))::Int
409409
end
410410
end
411-
if keepempty || i <= ncodeunits(str)
411+
if keepempty || i <= ncodeunits(str)::Int
412412
push!(strs, @inbounds SubString(str,i))
413413
end
414414
return strs
@@ -464,13 +464,13 @@ function rsplit(str::T, splitter::AbstractChar;
464464
end
465465

466466
function _rsplit(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array)
467-
n = lastindex(str)
468-
r = something(findlast(splitter, str), 0)
467+
n = lastindex(str)::Int
468+
r = something(findlast(splitter, str)::Union{Nothing,Int,UnitRange{Int}}, 0)
469469
j, k = first(r), last(r)
470470
while j > 0 && k > 0 && length(strs) != limit-1
471-
(keepempty || k < n) && pushfirst!(strs, @inbounds SubString(str,nextind(str,k),n))
472-
n = prevind(str, j)
473-
r = something(findprev(splitter,str,n), 0)
471+
(keepempty || k < n) && pushfirst!(strs, @inbounds SubString(str,nextind(str,k)::Int,n))
472+
n = prevind(str, j)::Int
473+
r = something(findprev(splitter,str,n)::Union{Nothing,Int,UnitRange{Int}}, 0)
474474
j, k = first(r), last(r)
475475
end
476476
(keepempty || n > 0) && pushfirst!(strs, SubString(str,1,n))

stdlib/Distributed/src/managers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)
504504
end
505505

506506
function connect_w2w(pid::Int, config::WorkerConfig)
507-
(rhost, rport) = notnothing(config.connect_at)
507+
(rhost, rport) = notnothing(config.connect_at)::Tuple{AbstractString, Int}
508508
config.host = rhost
509509
config.port = rport
510510
(s, bind_addr) = connect_to_worker(rhost, rport)

stdlib/Logging/src/ConsoleLogger.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i
106106
# Generate a text representation of the message and all key value pairs,
107107
# split into lines.
108108
msglines = [(indent=0,msg=l) for l in split(chomp(string(message)), '\n')]
109-
dsize = displaysize(logger.stream)
109+
dsize = displaysize(logger.stream)::Tuple{Int,Int}
110110
if !isempty(kwargs)
111111
valbuf = IOBuffer()
112112
rows_per_value = max(1, dsize[1]÷(length(kwargs)+1))
@@ -127,9 +127,7 @@ function handle_message(logger::ConsoleLogger, level, message, _module, group, i
127127

128128
# Format lines as text with appropriate indentation and with a box
129129
# decoration on the left.
130-
color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line)
131-
color = convert(Symbol, color)::Symbol
132-
prefix, suffix = convert(String, prefix)::String, convert(String, suffix)::String
130+
color,prefix,suffix = logger.meta_formatter(level, _module, group, id, filepath, line)::Tuple{Union{Symbol,Int},String,String}
133131
minsuffixpad = 2
134132
buf = IOBuffer()
135133
iob = IOContext(buf, logger.stream)

stdlib/Markdown/src/render/terminal/formatting.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@ end
99
words(s) = split(s, " ")
1010
lines(s) = split(s, "\n")
1111

12-
# This could really be more efficient
13-
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
14-
if occursin(r"\n", s)
15-
return vcat(map(s->wrapped_lines(io, s, width = width, i = i), split(s, "\n"))...)
16-
end
12+
function wrapped_lines!(lines, io::IO, s::AbstractString, width, i)
1713
ws = words(s)
18-
lines = AbstractString[ws[1]]
19-
i += ws[1] |> ansi_length
20-
for word in ws[2:end]
14+
for word in ws
2115
word_length = ansi_length(word)
2216
if i + word_length + 1 > width
2317
i = word_length
2418
push!(lines, word)
2519
else
2620
i += word_length + 1
27-
lines[end] *= " " * word
21+
lines[end] *= " " * word # this could be more efficient
22+
end
23+
end
24+
return i
25+
end
26+
27+
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
28+
lines = AbstractString[""]
29+
if occursin(r"\n", s)
30+
for ss in split(s, "\n")
31+
i = wrapped_lines!(lines, io, ss, width, i)
2832
end
33+
else
34+
wrapped_lines!(lines, io, s, width, i)
2935
end
3036
return lines
3137
end

0 commit comments

Comments
 (0)