Skip to content

Commit 87c93b9

Browse files
committed
Fix annotated join with non-concrete eltype iters
As raised in <JuliaLang/StyledStrings.jl#57 (comment)>, when the eltype of an iterator is non-concrete, _isannotated will return false. To properly check such cases, we need to see if any of the elements of the iterator are annotated. This is a bit of an interesting case, since: - Most of the time it shouldn't be hit, we reasonably expect most iterables to infer as producing concrete types - The eltype of the iterator is (generally) known at compile-time, and so in any case other than the ambiguous non-concrete one, this check remains able to be done at compile-time. - Should the iterator be stateful and non-concrete, the check can consume some amount of the iterator before join is called. With this change, join always preserves annotations. The compromise made is that iterators with non-concrete eltypes can result in join inferring a union return type (i.e. type instability with non-concrete iterators), but that doesn't seem too bad to me. Reported-by: kimikage <kimikage.ceo@gmail.com>
1 parent 36a0da0 commit 87c93b9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

base/strings/io.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,16 @@ function join(io::IO, iterator, delim="")
354354
end
355355

356356
function _join_preserve_annotations(iterator, args...)
357-
if _isannotated(eltype(iterator)) || any(_isannotated, args)
357+
if isconcretetype(eltype(iterator)) && !_isannotated(eltype(iterator)) && !any(_isannotated, args)
358+
sprint(join, iterator, args...)
359+
else
358360
io = AnnotatedIOBuffer()
359361
join(io, iterator, args...)
360-
read(seekstart(io), AnnotatedString{String})
361-
else
362-
sprint(join, iterator, args...)
362+
if isconcretetype(eltype(iterator)) || !isempty(io.annotations)
363+
read(seekstart(io), AnnotatedString{String})
364+
else
365+
String(take!(io.io))
366+
end
363367
end
364368
end
365369

test/strings/annotated.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ end
101101
[(1:4, :label => 5),
102102
(5:5, :label => 2),
103103
(6:9, :label => 5)])
104+
@test join((String(str1), str1), ' ') ==
105+
Base.AnnotatedString("test test", [(6:9, :label => 5)])
104106
@test repeat(str1, 2) == Base.AnnotatedString("testtest", [(1:8, :label => 5)])
105107
@test repeat(str2, 2) == Base.AnnotatedString("casecase", [(2:3, :label => "oomph"),
106108
(6:7, :label => "oomph")])

0 commit comments

Comments
 (0)