Skip to content

Commit 66f4375

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 lot of type-annotation.
1 parent 1888e31 commit 66f4375

File tree

15 files changed

+78
-72
lines changed

15 files changed

+78
-72
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/reflection.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,10 +846,7 @@ A list of modules can also be specified as an array.
846846
At least Julia 1.4 is required for specifying a module.
847847
"""
848848
function methods(@nospecialize(f), @nospecialize(t),
849-
@nospecialize(mod::Union{Module,AbstractArray{Module},Nothing}=nothing))
850-
if mod isa Module
851-
mod = (mod,)
852-
end
849+
@nospecialize(mod::Union{Tuple{Module},AbstractArray{Module},Nothing}=nothing))
853850
if isa(f, Core.Builtin)
854851
throw(ArgumentError("argument is not a generic function"))
855852
end
@@ -858,6 +855,7 @@ function methods(@nospecialize(f), @nospecialize(t),
858855
MethodList(Method[m.method for m in _methods(f, t, -1, world) if mod === nothing || m.method.module in mod],
859856
typeof(f).name.mt)
860857
end
858+
methods(@nospecialize(f), @nospecialize(t), mod::Module) = methods(f, t, (mod,))
861859

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

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(s, i, j)
41-
update_arg(s[i:prevind(s, j)])
42-
something(peek(st), (lastindex(s)+1,'\0'))[1]
41+
update_arg(s[i:prevind(s, j)::Int])
42+
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
i = consume_upto(s, i, 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
@@ -343,11 +343,11 @@ julia> escape_string(string('\\u2135','\\0','0')) # \\0 would be ambiguous
343343
"""
344344
function escape_string(io::IO, s::AbstractString, esc="")
345345
a = Iterators.Stateful(s)
346-
for c in a
346+
for c::AbstractChar in a
347347
if c in esc
348348
print(io, '\\', c)
349349
elseif isascii(c)
350-
c == '\0' ? print(io, escape_nul(peek(a))) :
350+
c == '\0' ? print(io, escape_nul(peek(a)::Union{AbstractChar,Nothing})) :
351351
c == '\e' ? print(io, "\\e") :
352352
c == '\\' ? print(io, "\\\\") :
353353
'\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) :
@@ -356,10 +356,10 @@ function escape_string(io::IO, s::AbstractString, esc="")
356356
elseif !isoverlong(c) && !ismalformed(c)
357357
isprint(c) ? print(io, c) :
358358
c <= '\x7f' ? print(io, "\\x", string(UInt32(c), base = 16, pad = 2)) :
359-
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 4 : 2)) :
360-
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 8 : 4))
359+
c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 4 : 2)) :
360+
print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)::Union{AbstractChar,Nothing}) ? 8 : 4))
361361
else # malformed or overlong
362-
u = bswap(reinterpret(UInt32, c))
362+
u = bswap(reinterpret(UInt32, c)::UInt32)
363363
while true
364364
print(io, "\\x", string(u % UInt8, base = 16, pad = 2))
365365
(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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)
443443

444444
# master connecting to workers
445445
if config.io !== nothing
446-
(bind_addr, port) = read_worker_host_port(config.io)
446+
(bind_addr, port::Int) = read_worker_host_port(config.io)
447447
pubhost = something(config.host, bind_addr)
448448
config.host = pubhost
449449
config.port = port
@@ -503,7 +503,7 @@ function connect(manager::ClusterManager, pid::Int, config::WorkerConfig)
503503
end
504504

505505
function connect_w2w(pid::Int, config::WorkerConfig)
506-
(rhost, rport) = notnothing(config.connect_at)
506+
(rhost, rport) = notnothing(config.connect_at)::Tuple{AbstractString, Int}
507507
config.host = rhost
508508
config.port = rport
509509
(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: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,33 @@ 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+
if isempty(lines)
22+
push!(lines, word)
23+
else
24+
lines[end] *= " " * word # this could be more efficient
25+
end
26+
end
27+
end
28+
return i
29+
end
30+
31+
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
32+
lines = AbstractString[]
33+
if occursin(r"\n", s)
34+
for ss in split(s, "\n")
35+
i = wrapped_lines!(lines, io, ss, width, i)
2836
end
37+
else
38+
wrapped_lines!(lines, io, s, width, i)
2939
end
3040
return lines
3141
end

stdlib/Test/src/Test.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,11 +1587,11 @@ with string types besides the standard `String` type.
15871587
struct GenericString <: AbstractString
15881588
string::AbstractString
15891589
end
1590-
Base.ncodeunits(s::GenericString) = ncodeunits(s.string)
1591-
Base.codeunit(s::GenericString) = codeunit(s.string)
1592-
Base.codeunit(s::GenericString, i::Integer) = codeunit(s.string, i)
1593-
Base.isvalid(s::GenericString, i::Integer) = isvalid(s.string, i)
1594-
Base.iterate(s::GenericString, i::Integer=1) = iterate(s.string, i)
1590+
Base.ncodeunits(s::GenericString) = ncodeunits(s.string)::Int
1591+
Base.codeunit(s::GenericString) = codeunit(s.string)::Type{<:Union{UInt8, UInt16, UInt32}}
1592+
Base.codeunit(s::GenericString, i::Integer) = codeunit(s.string, i)::Union{UInt8, UInt16, UInt32}
1593+
Base.isvalid(s::GenericString, i::Integer) = isvalid(s.string, i)::Bool
1594+
Base.iterate(s::GenericString, i::Integer=1) = iterate(s.string, i)::Union{Nothing,Tuple{AbstractChar,Int}}
15951595
Base.reverse(s::GenericString) = GenericString(reverse(s.string))
15961596
Base.reverse(s::SubString{GenericString}) =
15971597
GenericString(typeof(s.string)(reverse(String(s))))

0 commit comments

Comments
 (0)