[llvm-branch-commits] [libcxx] 0c14843 - Reland [libc++] [LWG3321] Mark "year_month_day_last::day() specification does not cover !ok() values" issue as "Nothing to do", but add assertion.

Marek Kurdej via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu May 21 12:56:00 PDT 2020


Author: Marek Kurdej
Date: 2020-05-21T21:55:38+02:00
New Revision: 0c148430cf612762ca243aafe23b3fb9670d2797

URL: https://github.com/llvm/llvm-project/commit/0c148430cf612762ca243aafe23b3fb9670d2797
DIFF: https://github.com/llvm/llvm-project/commit/0c148430cf612762ca243aafe23b3fb9670d2797.diff

LOG: Reland [libc++] [LWG3321] Mark "year_month_day_last::day() specification does not cover !ok() values" issue as "Nothing to do", but add assertion.

Summary:
This LWG issue states that the result of `year_month_day_last::day()` is implementation defined if `ok()` is `false`.
However, from user perspective, calling `day()` in this situation will lead to a (possibly difficult to find) crash.
Hence, I have added an assertion to warn user at least when assertions are enabled.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D70346

Added: 
    

Modified: 
    libcxx/include/chrono
    libcxx/test/libcxx/algorithms/debug_less.pass.cpp
    libcxx/www/cxx2a_status.html

Removed: 
    


################################################################################
diff  --git a/libcxx/include/chrono b/libcxx/include/chrono
index 6e5de398b72f..9913dc1d06c9 100644
--- a/libcxx/include/chrono
+++ b/libcxx/include/chrono
@@ -824,6 +824,7 @@ constexpr chrono::year                                  operator ""y(unsigned lo
 */
 
 #include <__config>
+#include <__debug>
 #include <ctime>
 #include <type_traits>
 #include <ratio>
@@ -2454,6 +2455,7 @@ chrono::day year_month_day_last::day() const noexcept
         chrono::day(31), chrono::day(31), chrono::day(30),
         chrono::day(31), chrono::day(30), chrono::day(31)
     };
+    _LIBCPP_ASSERT(ok(), "year_month_day_last::day(): year_month_day_last is invalid");
     return month() != February || !__y.is_leap() ?
         __d[static_cast<unsigned>(month()) - 1] : chrono::day{29};
 }

diff  --git a/libcxx/test/libcxx/algorithms/debug_less.pass.cpp b/libcxx/test/libcxx/algorithms/debug_less.pass.cpp
index 6dd56955001d..8a38a8053d42 100644
--- a/libcxx/test/libcxx/algorithms/debug_less.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/debug_less.pass.cpp
@@ -14,8 +14,13 @@
 
 // __debug_less checks that a comparator actually provides a strict-weak ordering.
 
+#include <chrono> // Include before defining _LIBCPP_ASSERT: cannot throw in a function marked noexcept.
+
 struct DebugException {};
 
+#ifdef _LIBCPP_ASSERT
+#undef _LIBCPP_ASSERT
+#endif
 #define _LIBCPP_DEBUG 0
 #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : throw ::DebugException())
 

diff  --git a/libcxx/www/cxx2a_status.html b/libcxx/www/cxx2a_status.html
index 05914fc6832d..2a93d35feb50 100644
--- a/libcxx/www/cxx2a_status.html
+++ b/libcxx/www/cxx2a_status.html
@@ -439,7 +439,7 @@ <h3>Library Working group Issues Status</h3>
 	<tr><td><a href="https://wg21.link/LWG3209">3209</a></td><td>Expression in <tt>year::ok()</tt> returns clause is ill-formed</td><td>Cologne</td><td>Complete</td></tr>
 
 	<tr><td></td><td></td><td></td><td></td></tr>
-	<tr><td><a href="https://wg21.link/LWG3231">3231</a></td><td><tt>year_month_day_last::day</tt> specification does not cover <tt>!ok()</tt> values</td><td>Belfast</td><td></td></tr>
+	<tr><td><a href="https://wg21.link/LWG3231">3231</a></td><td><tt>year_month_day_last::day</tt> specification does not cover <tt>!ok()</tt> values</td><td>Belfast</td><td><i>Nothing to do</i></td></tr>
 	<tr><td><a href="https://wg21.link/LWG3225">3225</a></td><td><tt>zoned_time</tt> converting constructor shall not be <tt>noexcept</tt></td><td>Belfast</td><td></td></tr>
 	<tr><td><a href="https://wg21.link/LWG3190">3190</a></td><td><tt>std::allocator::allocate</tt> sometimes returns too little storage</td><td>Belfast</td><td></td></tr>
 	<tr><td><a href="https://wg21.link/LWG3218">3218</a></td><td>Modifier for <tt>%d</tt> parse flag does not match POSIX and <tt>format</tt> specification</td><td>Belfast</td><td></td></tr>


        


More information about the llvm-branch-commits mailing list