Skip to content

Commit 6b4e426

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 738b0ae commit 6b4e426

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
@@ -512,11 +512,11 @@ prevind(s::AbstractString, i::Int) = prevind(s, i, 1)
512512

513513
function prevind(s::AbstractString, i::Int, n::Int)
514514
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
515-
z = ncodeunits(s) + 1
515+
z = ncodeunits(s)::Int + 1
516516
@boundscheck 0 < i z || throw(BoundsError(s, i))
517-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
517+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
518518
while n > 0 && 1 < i
519-
@inbounds n -= isvalid(s, i -= 1)
519+
@inbounds n -= isvalid(s, i -= 1)::Bool
520520
end
521521
return i - n
522522
end
@@ -571,11 +571,11 @@ nextind(s::AbstractString, i::Int) = nextind(s, i, 1)
571571

572572
function nextind(s::AbstractString, i::Int, n::Int)
573573
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
574-
z = ncodeunits(s)
574+
z = ncodeunits(s)::Int
575575
@boundscheck 0 i z || throw(BoundsError(s, i))
576-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
576+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
577577
while n > 0 && i < z
578-
@inbounds n -= isvalid(s, i += 1)
578+
@inbounds n -= isvalid(s, i += 1)::Bool
579579
end
580580
return i + n
581581
end

test/strings/basic.jl

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

883888
@testset "first and last" begin

0 commit comments

Comments
 (0)