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 truncation warnings in string/vector iterator subtraction and ranges::is_permutation #4237

Merged
merged 2 commits into from Dec 7, 2023

Conversation

StephanTLavavej
Copy link
Member

Test coverage will be provided by std/strings/basic.string/string.cons/from_range.pass.cpp, std/containers/unord/unord.multimap/unord.multimap.cnstr/from_range.pass.cpp, and more in the upcoming libcxx update that I'm working on. They have allocators with custom size/difference types (e.g. 32-bit types even on 64-bit platforms) that reveal these issues.

When subtracting string/vector iterators, we can get truncation warnings like xstring(1972): warning C4244: 'return': conversion from '__int64' to 'int', possible loss of data. Because we're subtracting iterators into the same string/vector, casting to the allocator's difference_type is always safe.

In _Is_permutation_sized powering ranges::is_permutation, we can get algorithm(940): warning C4244: 'argument': conversion from '__int64' to 'const int', possible loss of data. This happens when the ranges have different difference types. We call _Is_permutation_sized after checking that the ranges are the same length, so we only pass the first count, and casting that to the second range's difference type is guaranteed to be value-preserving. This asymmetry looked strange enough that I thought it deserved a comment (even without introducing using _Diff2, the line would get long enough to wrap, so we have space for a comment anyways).

… iterators.

Found by std/strings/basic.string/string.cons/from_range.pass.cpp.

xstring(1972): warning C4244: 'return': conversion from '__int64' to 'int', possible loss of data

Because we're subtracting iterators into the same string, the allocator's difference_type is always safe.

Same issue with vectors.
std/containers/unord/unord.multimap/unord.multimap.cnstr/from_range.pass.cpp

algorithm(940): warning C4244: 'argument': conversion from '__int64' to 'const int', possible loss of data
@StephanTLavavej StephanTLavavej added bug Something isn't working ranges C++20/23 ranges labels Dec 4, 2023
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner December 4, 2023 22:42
@github-actions github-actions bot added this to Initial Review in Code Reviews Dec 4, 2023
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in Code Reviews Dec 4, 2023
@CaseyCarter CaseyCarter moved this from Final Review to Ready To Merge in Code Reviews Dec 5, 2023
@StephanTLavavej StephanTLavavej self-assigned this Dec 7, 2023
@StephanTLavavej
Copy link
Member Author

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit ae50dff into microsoft:main Dec 7, 2023
37 checks passed
Code Reviews automation moved this from Ready To Merge to Done Dec 7, 2023
@StephanTLavavej StephanTLavavej deleted the truncation-warnings branch December 7, 2023 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ranges C++20/23 ranges
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants