diff --git a/base/int.jl b/base/int.jl index 26735fb725cac..b3acd29c6492f 100644 --- a/base/int.jl +++ b/base/int.jl @@ -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 @@ -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 @@ -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 diff --git a/base/intfuncs.jl b/base/intfuncs.jl index b99bbb6f7410b..ffd87da6cfc9e 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -605,8 +605,8 @@ function bin(x::Unsigned, pad::Integer, neg::Bool) i = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) a = StringVector(i) while i > neg - @inbounds a[i] = 48+(x&0x1) - x >>= 1 + @inbounds a[i] = (0x30+(x&0x1)::Unsigned)::Unsigned + x = (x >> 1)::Unsigned i -= 1 end if neg; @inbounds a[1]=0x2d; end diff --git a/base/reflection.jl b/base/reflection.jl index 9b6ae00e153f3..ef6769be8d7a0 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -838,7 +838,7 @@ function _methods_by_ftype(@nospecialize(t), lim::Int, world::UInt) return _methods_by_ftype(t, lim, world, UInt[typemin(UInt)], UInt[typemax(UInt)]) end function _methods_by_ftype(@nospecialize(t), lim::Int, world::UInt, min::Array{UInt,1}, max::Array{UInt,1}) - return ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), t, lim, 0, world, min, max) + return ccall(:jl_matching_methods, Any, (Any, Cint, Cint, UInt, Ptr{UInt}, Ptr{UInt}), t, lim, 0, world, min, max)::Union{Vector{Any},Bool} end # high-level, more convenient method lookup functions @@ -875,18 +875,22 @@ 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[3] for m in _methods(f, t, -1, world) if mod === nothing || m[3].module in mod], - typeof(f).name.mt) + ms = Method[] + for sig_params_meth in _methods(f, t, -1, world) + meth = sig_params_meth[3]::Method + if mod === nothing || meth.module ∈ mod + push!(ms, meth) + end + end + return 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) diff --git a/base/refvalue.jl b/base/refvalue.jl index 6803ef8314355..3defb49a7fcae 100644 --- a/base/refvalue.jl +++ b/base/refvalue.jl @@ -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) diff --git a/base/regex.jl b/base/regex.jl index 62b23008eb07d..42bff4658d35c 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -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") diff --git a/base/shell.jl b/base/shell.jl index fcb8b2d2888b6..4e6d747921d5b 100644 --- a/base/shell.jl +++ b/base/shell.jl @@ -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 @@ -38,8 +38,8 @@ function shell_parse(str::AbstractString, interpolate::Bool=true; end end function consume_upto(j) - update_arg(s[i:prevind(s, j)]) - i = something(peek(st), (lastindex(s)+1,'\0'))[1] + update_arg(s[i:prevind(s, j)::Int]) + i = something(peek(st), (lastindex(s)::Int+1,'\0'))[1] end function append_arg() if isempty(arg); arg = Any["",]; end @@ -47,7 +47,7 @@ function shell_parse(str::AbstractString, interpolate::Bool=true; arg = [] end - for (j, c) in st + for (j::Int, c::AbstractChar) in st if !in_single_quotes && !in_double_quotes && isspace(c) consume_upto(j) append_arg() diff --git a/base/show.jl b/base/show.jl index e99786c02218a..ed27eebeb1b09 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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), diff --git a/base/strings/basic.jl b/base/strings/basic.jl index 2d9ff43dfcf19..a2885c0803f54 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -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) @@ -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) @@ -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; " * diff --git a/base/strings/io.jl b/base/strings/io.jl index e9037ed02d44d..9cc047041f9c1 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -341,11 +341,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]) : @@ -354,10 +354,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 diff --git a/base/strings/search.jl b/base/strings/search.jl index 9d2fdf73afff4..eef7a97b41469 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -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 diff --git a/base/strings/substring.jl b/base/strings/substring.jl index abb22b412f993..f3e3d5ca6c280 100644 --- a/base/strings/substring.jl +++ b/base/strings/substring.jl @@ -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) @@ -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) @@ -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 @@ -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}) = diff --git a/base/strings/util.jl b/base/strings/util.jl index e7abfa0bf3f98..71538a11c83f9 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -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) @@ -389,12 +389,12 @@ 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 @@ -402,13 +402,13 @@ function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, 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 @@ -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)) diff --git a/stdlib/Distributed/src/managers.jl b/stdlib/Distributed/src/managers.jl index 45c26e95d5197..0541bd109ff4a 100644 --- a/stdlib/Distributed/src/managers.jl +++ b/stdlib/Distributed/src/managers.jl @@ -444,7 +444,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 @@ -504,7 +504,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) diff --git a/stdlib/Logging/src/ConsoleLogger.jl b/stdlib/Logging/src/ConsoleLogger.jl index 86cb355206756..4a73f10932dc7 100644 --- a/stdlib/Logging/src/ConsoleLogger.jl +++ b/stdlib/Logging/src/ConsoleLogger.jl @@ -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)) @@ -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) diff --git a/stdlib/Markdown/src/render/terminal/formatting.jl b/stdlib/Markdown/src/render/terminal/formatting.jl index 02aebde2467ea..5fa4eae249f3f 100644 --- a/stdlib/Markdown/src/render/terminal/formatting.jl +++ b/stdlib/Markdown/src/render/terminal/formatting.jl @@ -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 diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 2081425563780..90517def6b540 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -1582,11 +1582,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))))