[PATCH] D27691: [libcxx] [test] Fix a size_t-to-int truncation warning in error_code.pass.cpp.

Stephan T. Lavavej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 12 16:08:15 PST 2016

STL_MSFT created this revision.
STL_MSFT added reviewers: EricWF, mclow.lists.
STL_MSFT added a subscriber: cfe-commits.

[libcxx] [test] Fix a size_t-to-int truncation warning in error_code.pass.cpp.

This test was triggering MSVC x64 warning C4267
"conversion from 'size_t' to 'int', possible loss of data" by taking 0, 2, and 10
as std::size_t, then constructing error_code(int, const error_category&)
from that (N4618 [syserr.errcode.constructors]/3).

The fix is simple: take these ints as int, pass them to the int-taking
constructor, and perform a value-preserving static_cast<std::size_t>
when comparing them to `std::size_t result`.



Index: test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
--- test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
+++ test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
@@ -23,16 +23,16 @@
 #include "test_macros.h"
-test(std::size_t i)
+test(int i)
     typedef std::error_code T;
     typedef std::hash<T> H;
     static_assert((std::is_same<H::argument_type, T>::value), "" );
     static_assert((std::is_same<H::result_type, std::size_t>::value), "" );
     H h;
     T ec(i, std::system_category());
     const std::size_t result = h(ec);
-    LIBCPP_ASSERT(result == i);
+    LIBCPP_ASSERT(result == static_cast<std::size_t>(i));
     ((void)result); // Prevent unused warning

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27691.81158.patch
Type: text/x-patch
Size: 792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161213/4af89824/attachment.bin>

More information about the cfe-commits mailing list