Skip to content

Commit c4eeabf

Browse files
authored
AnnotatedString: add concrete type asserts to isvalid, ncodeunits (#57607)
The type assertions are valid according to the doc strings of these two functions in the case of `AbstractString`. Should prevent some invalidation on loading user code. Fixes #57606
1 parent 6c9c336 commit c4eeabf

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

base/strings/annotated.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ promote_rule(::Type{<:AnnotatedString}, ::Type{<:AbstractString}) = AnnotatedStr
147147

148148
## AbstractString interface ##
149149

150-
ncodeunits(s::AnnotatedString) = ncodeunits(s.string)
150+
ncodeunits(s::AnnotatedString) = ncodeunits(s.string)::Int
151151
codeunits(s::AnnotatedString) = codeunits(s.string)
152152
codeunit(s::AnnotatedString) = codeunit(s.string)
153153
codeunit(s::AnnotatedString, i::Integer) = codeunit(s.string, i)
154-
isvalid(s::AnnotatedString, i::Integer) = isvalid(s.string, i)
154+
isvalid(s::AnnotatedString, i::Integer) = isvalid(s.string, i)::Bool
155155
@propagate_inbounds iterate(s::AnnotatedString, i::Integer=firstindex(s)) =
156156
if i <= lastindex(s.string); (s[i], nextind(s, i)) end
157157
eltype(::Type{<:AnnotatedString{S}}) where {S} = AnnotatedChar{eltype(S)}

test/strings/annotated.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474

7575
a = Base.AnnotatedString("hello", [(1:5, :label, 1)])
7676
@test first(a) == Base.AnnotatedChar('h', [(:label, 1)])
77+
78+
@test Bool === Base.infer_return_type(isvalid, Tuple{Base.AnnotatedString, Vararg})
79+
@test Int === Base.infer_return_type(ncodeunits, Tuple{Base.AnnotatedString})
7780
end
7881

7982
@testset "AnnotatedChar" begin

0 commit comments

Comments
 (0)