[libcxx] r289512 - [libcxx] [test] Fix size_t-to-int truncation warnings in syserr.hash.

Stephan T. Lavavej via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 12 17:54:58 PST 2016


Author: stl_msft
Date: Mon Dec 12 19:54:58 2016
New Revision: 289512

URL: http://llvm.org/viewvc/llvm-project?rev=289512&view=rev
Log:
[libcxx] [test] Fix size_t-to-int truncation warnings in syserr.hash.

After r289363, these tests were 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&) or
error_condition(int, const error_category&) from that (N4618 19.5.3.2
[syserr.errcode.constructors]/3, 19.5.4.2 [syserr.errcondition.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`.

Fixes D27691.

Modified:
    libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
    libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_condition.pass.cpp

Modified: libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp?rev=289512&r1=289511&r2=289512&view=diff
==============================================================================
--- libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp (original)
+++ libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_code.pass.cpp Mon Dec 12 19:54:58 2016
@@ -23,7 +23,7 @@
 #include "test_macros.h"
 
 void
-test(std::size_t i)
+test(int i)
 {
     typedef std::error_code T;
     typedef std::hash<T> H;
@@ -32,7 +32,7 @@ test(std::size_t i)
     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
 }
 

Modified: libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_condition.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_condition.pass.cpp?rev=289512&r1=289511&r2=289512&view=diff
==============================================================================
--- libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_condition.pass.cpp (original)
+++ libcxx/trunk/test/std/diagnostics/syserr/syserr.hash/error_condition.pass.cpp Mon Dec 12 19:54:58 2016
@@ -23,7 +23,7 @@
 #include "test_macros.h"
 
 void
-test(std::size_t i)
+test(int i)
 {
     typedef std::error_condition T;
     typedef std::hash<T> H;
@@ -32,7 +32,7 @@ test(std::size_t i)
     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
 }
 




More information about the cfe-commits mailing list