Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalidations from novel Integer conversions #36459

Merged
merged 1 commit into from
Aug 5, 2020
Merged
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
23 changes: 12 additions & 11 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,19 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
vdots::AbstractString = "\u22ee",
ddots::AbstractString = " \u22f1 ",
hmod::Integer = 5, vmod::Integer = 5)
hmod, vmod = Int(hmod)::Int, Int(vmod)::Int
if !(get(io, :limit, false)::Bool)
screenheight = screenwidth = typemax(Int)
else
sz = displaysize(io)
sz = displaysize(io)::Tuple{Int,Int}
screenheight, screenwidth = sz[1] - 4, sz[2]
end
screenwidth -= length(pre) + length(post)
presp = repeat(" ", length(pre)) # indent each row to match pre string
screenwidth -= length(pre)::Int + length(post)::Int
presp = repeat(" ", length(pre)::Int) # indent each row to match pre string
postsp = ""
@assert textwidth(hdots) == textwidth(ddots)
sepsize = length(sep)
rowsA, colsA = UnitRange(axes(X,1)), UnitRange(axes(X,2))
sepsize = length(sep)::Int
rowsA, colsA = UnitRange{Int}(axes(X,1)), UnitRange{Int}(axes(X,2))
m, n = length(rowsA), length(colsA)
# To figure out alignments, only need to look at as many rows as could
# fit down screen. If screen has at least as many rows as A, look at A.
Expand All @@ -204,14 +205,14 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
if i != last(rowsA); println(io); end
end
else # rows fit down screen but cols don't, so need horizontal ellipsis
c = div(screenwidth-length(hdots)+1,2)+1 # what goes to right of ellipsis
c = div(screenwidth-length(hdots)::Int+1,2)+1 # what goes to right of ellipsis
Ralign = reverse(alignment(io, X, rowsA, reverse(colsA), c, c, sepsize)) # alignments for right
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)::Int
Lalign = alignment(io, X, rowsA, colsA, c, c, sepsize) # alignments for left of ellipsis
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
print_matrix_row(io, X,Lalign,i,colsA[1:length(Lalign)],sep)
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)))
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)::Int))
print_matrix_row(io, X, Ralign, i, (n - length(Ralign)) .+ colsA, sep)
print(io, i == last(rowsA) ? post : postsp)
if i != last(rowsA); println(io); end
Expand All @@ -231,15 +232,15 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
end
end
else # neither rows nor cols fit, so use all 3 kinds of dots
c = div(screenwidth-length(hdots)+1,2)+1
c = div(screenwidth-length(hdots)::Int+1,2)+1
Ralign = reverse(alignment(io, X, rowsA, reverse(colsA), c, c, sepsize))
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)
c = screenwidth - sum(map(sum,Ralign)) - (length(Ralign)-1)*sepsize - length(hdots)::Int
Lalign = alignment(io, X, rowsA, colsA, c, c, sepsize)
r = mod((length(Ralign)-n+1),vmod) # where to put dots on right half
for i in rowsA
print(io, i == first(rowsA) ? pre : presp)
print_matrix_row(io, X,Lalign,i,colsA[1:length(Lalign)],sep)
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)))
print(io, (i - first(rowsA)) % hmod == 0 ? hdots : repeat(" ", length(hdots)::Int))
print_matrix_row(io, X,Ralign,i,(n-length(Ralign)).+colsA,sep)
print(io, i == last(rowsA) ? post : postsp)
if i != rowsA[end] || i == rowsA[halfheight]; println(io); end
Expand Down
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

This comment was marked as resolved.

This comment was marked as resolved.


"""
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
15 changes: 9 additions & 6 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -846,18 +846,21 @@ 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
t = to_tuple_type(t)
world = typemax(UInt)
MethodList(Method[m.method for m in _methods(f, t, -1, world) if mod === nothing || m.method.module in mod],
typeof(f).name.mt)
# Lack of specialization => a comprehension triggers too many invalidations via _collect, so collect the methods manually
ms = Method[]
for m in _methods(f, t, -1, world)
m::Core.MethodMatch
(mod === nothing || m.method.module ∈ mod) && push!(ms, m.method)
end
MethodList(ms, 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}
Copy link
Member

Choose a reason for hiding this comment

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

I'm pretty sure this is true for all string types in practice, but it's not technically required.

Copy link
Member Author

Choose a reason for hiding this comment

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

Docs say this:

codeunit(s::AbstractString) -> Type{<:Union{UInt8, UInt16, UInt32}}

I've been taking the docs as license to enforce stuff. Should I not?

Copy link
Member

Choose a reason for hiding this comment

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

I guess someone could have a different code unit, but it would be very weird. Not sure all of the logic in base would work correctly with some other code unit.

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
4 changes: 2 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function unique!(f, A::AbstractVector; seen::Union{Nothing,Set}=nothing)
return A
end

i = firstindex(A)
i = firstindex(A)::Int
x = @inbounds A[i]
y = f(x)
if seen === nothing
Expand All @@ -291,7 +291,7 @@ function _unique!(f, A::AbstractVector, seen::Set, current::Integer, i::Integer)
end
i += 1
end
return resize!(A, current - firstindex(A) + 1)::typeof(A)
return resize!(A, current - firstindex(A)::Int + 1)::typeof(A)
end


Expand Down
6 changes: 4 additions & 2 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const shell_special = "#{}()[]<>|&*?~;"
function rstrip_shell(s::AbstractString)
c_old = nothing
for (i, c) in Iterators.reverse(pairs(s))
i::Int; c::AbstractChar
((c == '\\') && c_old == ' ') && return SubString(s, 1, i+1)
isspace(c) || return SubString(s, 1, i)
c_old = c
Expand Down Expand Up @@ -38,8 +39,8 @@ 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
Expand All @@ -48,6 +49,7 @@ function shell_parse(str::AbstractString, interpolate::Bool=true;
end

for (j, c) in st
j::Int; c::AbstractChar
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
22 changes: 11 additions & 11 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)::Int * sizeof(codeunit(s)::Type{<:Union{UInt8,UInt16,UInt32}})
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 All @@ -204,9 +204,9 @@ end
## bounds checking ##

checkbounds(::Type{Bool}, s::AbstractString, i::Integer) =
1 ≤ i ≤ ncodeunits(s)
1 ≤ i ≤ ncodeunits(s)::Int
checkbounds(::Type{Bool}, s::AbstractString, r::AbstractRange{<:Integer}) =
isempty(r) || (1 ≤ minimum(r) && maximum(r) ≤ ncodeunits(s))
isempty(r) || (1 ≤ minimum(r) && maximum(r) ≤ ncodeunits(s)::Int)
checkbounds(::Type{Bool}, s::AbstractString, I::AbstractArray{<:Real}) =
all(i -> checkbounds(Bool, s, i), I)
checkbounds(::Type{Bool}, s::AbstractString, I::AbstractArray{<:Integer}) =
Expand Down Expand Up @@ -382,12 +382,12 @@ julia> length("jμΛIα")
5
```
"""
length(s::AbstractString) = @inbounds return length(s, 1, ncodeunits(s))
length(s::AbstractString) = @inbounds return length(s, 1, ncodeunits(s)::Int)

function length(s::AbstractString, i::Int, j::Int)
@boundscheck begin
0 < i ≤ ncodeunits(s)+1 || throw(BoundsError(s, i))
0 ≤ j < ncodeunits(s)+1 || throw(BoundsError(s, j))
0 < i ≤ ncodeunits(s)::Int+1 || throw(BoundsError(s, i))
0 ≤ j < ncodeunits(s)::Int+1 || throw(BoundsError(s, j))
end
n = 0
for k = i:j
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
10 changes: 5 additions & 5 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 Expand Up @@ -351,21 +351,21 @@ function _rsearchindex(s::AbstractString,
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(Iterators.reverse(t))
while true
i = findprev(isequal(t1), s, i)
i === nothing && return 0
ii = prevind(s, i)
ii = prevind(s, i)::Int
a = Iterators.Stateful(trest)
b = Iterators.Stateful(Iterators.reverse(
pairs(SubString(s, 1, ii))))
matched = all(splat(==), zip(a, (x[2] for x in b)))
if matched && isempty(a)
isempty(b) && return firstindex(s)
return nextind(s, popfirst!(b)[1])
return nextind(s, popfirst!(b)[1])::Int
end
i = ii
end
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, i = y::Tuple{AbstractChar,Int}
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
Loading