Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix annotated join with non-concrete eltype iters #54919

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,20 @@ function join(io::IO, iterator, delim="")
end

function _join_preserve_annotations(iterator, args...)
if _isannotated(eltype(iterator)) || any(_isannotated, args)
if isconcretetype(eltype(iterator)) && !_isannotated(eltype(iterator)) && !any(_isannotated, args)
sprint(join, iterator, args...)
else
io = AnnotatedIOBuffer()
join(io, iterator, args...)
read(seekstart(io), AnnotatedString{String})
else
sprint(join, iterator, args...)
# If we know (from compile time information, or dynamically in the case
# of iterators with a non-concrete eltype), that the result is annotated
# in nature, we extract an `AnnotatedString`, otherwise we just extract
# a plain `String` from `io`.
if isconcretetype(eltype(iterator)) || !isempty(io.annotations)
read(seekstart(io), AnnotatedString{String})
else
String(take!(io.io))
tecosaur marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ end
[(1:4, :label => 5),
(5:5, :label => 2),
(6:9, :label => 5)])
@test join((String(str1), str1), ' ') ==
Base.AnnotatedString("test test", [(6:9, :label => 5)])
@test repeat(str1, 2) == Base.AnnotatedString("testtest", [(1:8, :label => 5)])
@test repeat(str2, 2) == Base.AnnotatedString("casecase", [(2:3, :label => "oomph"),
(6:7, :label => "oomph")])
Expand Down