Skip to content

Commit 38644b3

Browse files
nsajkoKristofferC
authored andcommitted
strings: type assert in the generic nextind, prevind methods (#57608)
The type assertions are valid according to the doc strings of these functions in the case of `AbstractString`. Should prevent some invalidation on loading user code. Fixes #57605 (cherry picked from commit 6c9c336)
1 parent e9126d7 commit 38644b3

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

base/strings/basic.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,11 @@ prevind(s::AbstractString, i::Int) = prevind(s, i, 1)
513513

514514
function prevind(s::AbstractString, i::Int, n::Int)
515515
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
516-
z = ncodeunits(s) + 1
516+
z = ncodeunits(s)::Int + 1
517517
@boundscheck 0 < i z || throw(BoundsError(s, i))
518-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
518+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
519519
while n > 0 && 1 < i
520-
@inbounds n -= isvalid(s, i -= 1)
520+
@inbounds n -= isvalid(s, i -= 1)::Bool
521521
end
522522
return i - n
523523
end
@@ -572,11 +572,11 @@ nextind(s::AbstractString, i::Int) = nextind(s, i, 1)
572572

573573
function nextind(s::AbstractString, i::Int, n::Int)
574574
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
575-
z = ncodeunits(s)
575+
z = ncodeunits(s)::Int
576576
@boundscheck 0 i z || throw(BoundsError(s, i))
577-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
577+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
578578
while n > 0 && i < z
579-
@inbounds n -= isvalid(s, i += 1)
579+
@inbounds n -= isvalid(s, i += 1)::Bool
580580
end
581581
return i + n
582582
end

test/strings/basic.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ end
877877
end
878878
end
879879
end
880+
881+
@testset "return type infers to `Int`" begin
882+
@test Int === Base.infer_return_type(prevind, Tuple{AbstractString, Vararg})
883+
@test Int === Base.infer_return_type(nextind, Tuple{AbstractString, Vararg})
884+
end
880885
end
881886

882887
@testset "first and last" begin

0 commit comments

Comments
 (0)