This is an archive of the discontinued LLVM Phabricator instance.

[libc++][format] Fixes usage of contiguous ranges.
ClosedPublic

Authored by Mordante on Jan 22 2023, 4:34 AM.

Details

Reviewers
ldionne
Group Reviewers
Restricted Project
Commits
rGa6c43e8ca8f6: [libc++][format] Fixes usage of contiguous ranges.
Summary

The contiguous range made incorrect assumptions for certain input
ranges.

Fixes llvm.org/PR60164

Diff Detail

Event Timeline

Mordante created this revision.Jan 22 2023, 4:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2023, 4:34 AM
Mordante requested review of this revision.Jan 22 2023, 4:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2023, 4:34 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript

We have iterators of all categories (and sized/unsized sentinels) in test_iterators.h. Why don't you use them?

Mordante updated this revision to Diff 491153.Jan 22 2023, 5:38 AM

Fixes compilation with older compilers.

JMazurkiewicz added inline comments.
libcxx/include/__format/range_formatter.h
177

How about using ranges::distance (http://eel.is/c++draft/range.iter.op.distance#4) here instead of ranges::size? We could get rid of ranges::sized_range<_Rp> constraint.

Also, I think we need extra includes: <__ranges/data.h> and <__ranges/size.h> (or <__iterator/distance.h>).

Mordante marked an inline comment as done.Jan 22 2023, 10:09 AM
Mordante added a subscriber: ldionne.

Thanks for the reviews!

We have iterators of all categories (and sized/unsized sentinels) in test_iterators.h. Why don't you use them?

In the tests for sets and maps @ldionne suggested to use adaptor classes with a minimal interface in the tests. So I thought it would be good to do the same here. I already use some the test iterators, just prior to the new hunk.

libcxx/include/__format/range_formatter.h
177

I expected ranges::distance to require a sized_range, but I didn't check it. But you're right distance would work too and have less requirements.

Indeed these headers should be included. I'm surprised it passes the modular tests without them.

huixie90 added inline comments.
libcxx/include/__format/range_formatter.h
177

I disagree. Although it is really rare to see a contiguous_range that is not sized_range. But when this happens, I think we do want to constrain it to sized_range, otherwise ranges::distance is going to be a linear operation. so using ranges::size is the right thing

Mordante updated this revision to Diff 491185.Jan 22 2023, 10:16 AM
Mordante marked an inline comment as done.

Adresses review comments.

Mordante marked an inline comment as done.Jan 22 2023, 10:49 AM

Thanks for the feedback!

libcxx/include/__format/range_formatter.h
177

I don't think the linear part will be a big issue; it's still faster than the alternative of copying the range in a temporary buffer. Still I will change it; distance returns a signed value, which runs into narrowing.

(I consider having a contiguous_range that is not a sized_range too rare to add a special case. This entire optimization isn't Standard mandated.)

Mordante updated this revision to Diff 491191.Jan 22 2023, 10:56 AM
Mordante marked an inline comment as done.

Addresses review comments.

ldionne accepted this revision.Jan 24 2023, 8:36 AM
This revision is now accepted and ready to land.Jan 24 2023, 8:36 AM
This revision was automatically updated to reflect the committed changes.