Skip to content

Commit cd3434d

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 cd3434d

File tree

16 files changed

+83
-74
lines changed

16 files changed

+83
-74
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

0 commit comments

Comments
 (0)