Skip to content

Commit 6c9c336

Browse files
authored
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
1 parent a802faf commit 6c9c336

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

509509
function prevind(s::AbstractString, i::Int, n::Int)
510510
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
511-
z = ncodeunits(s) + 1
511+
z = ncodeunits(s)::Int + 1
512512
@boundscheck 0 < i z || throw(BoundsError(s, i))
513-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
513+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
514514
while n > 0 && 1 < i
515-
@inbounds n -= isvalid(s, i -= 1)
515+
@inbounds n -= isvalid(s, i -= 1)::Bool
516516
end
517517
return i - n
518518
end
@@ -567,11 +567,11 @@ nextind(s::AbstractString, i::Int) = nextind(s, i, 1)
567567

568568
function nextind(s::AbstractString, i::Int, n::Int)
569569
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
570-
z = ncodeunits(s)
570+
z = ncodeunits(s)::Int
571571
@boundscheck 0 i z || throw(BoundsError(s, i))
572-
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
572+
n == 0 && return thisind(s, i)::Int == i ? i : string_index_err(s, i)
573573
while n > 0 && i < z
574-
@inbounds n -= isvalid(s, i += 1)
574+
@inbounds n -= isvalid(s, i += 1)::Bool
575575
end
576576
return i + n
577577
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)