[libc-commits] [libc] [libc][utils] cpp::always_false to enable static_assert(false) (PR #66209)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Wed Sep 13 06:48:53 PDT 2023


https://github.com/gchatelet created https://github.com/llvm/llvm-project/pull/66209:

None

>From 1e6278ed2ace08ba2ebd627818e83e6a7ef2dfa9 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Wed, 13 Sep 2023 13:47:36 +0000
Subject: [PATCH] [libc][utils] cpp::always_false to enable
 static_assert(false)

---
 libc/src/__support/CPP/CMakeLists.txt         |  1 +
 .../__support/CPP/type_traits/always_false.h  | 25 +++++++++++++++++++
 libc/src/__support/CPP/utility/declval.h      |  8 ++----
 .../llvm-project-overlay/libc/BUILD.bazel     |  1 +
 4 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 libc/src/__support/CPP/type_traits/always_false.h

diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index d24c023ec28ebc9..4e3e4a3edf381d2 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -95,6 +95,7 @@ add_header_library(
   type_traits
   HDRS
     type_traits.h
+    type_traits/always_false.h
     type_traits/add_lvalue_reference.h
     type_traits/add_pointer.h
     type_traits/add_rvalue_reference.h
diff --git a/libc/src/__support/CPP/type_traits/always_false.h b/libc/src/__support/CPP/type_traits/always_false.h
new file mode 100644
index 000000000000000..84be58d16ec8da6
--- /dev/null
+++ b/libc/src/__support/CPP/type_traits/always_false.h
@@ -0,0 +1,25 @@
+//===-- convenient static_assert(false) helper ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_ALWAYS_FALSE_H
+#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_ALWAYS_FALSE_H
+
+#include "src/__support/macros/attributes.h"
+
+namespace __llvm_libc::cpp {
+
+// This is technically not part of the standard but it come often enough that
+// it's convenient to have around.
+//
+// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2593r0.html#valid-workaround
+
+template <typename...> LIBC_INLINE_VAR constexpr bool always_false = false;
+
+} // namespace __llvm_libc::cpp
+
+#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_ALWAYS_FALSE_H
diff --git a/libc/src/__support/CPP/utility/declval.h b/libc/src/__support/CPP/utility/declval.h
index 9261ceb9332d518..21bb973de0dd048 100644
--- a/libc/src/__support/CPP/utility/declval.h
+++ b/libc/src/__support/CPP/utility/declval.h
@@ -9,17 +9,13 @@
 #define LLVM_LIBC_SRC_SUPPORT_CPP_UTILITY_DECLVAL_H
 
 #include "src/__support/CPP/type_traits/add_rvalue_reference.h"
-#include "src/__support/macros/attributes.h"
+#include "src/__support/CPP/type_traits/always_false.h"
 
 namespace __llvm_libc::cpp {
 
 // declval
-namespace detail {
-template <typename T> LIBC_INLINE_VAR constexpr bool always_false = false;
-}
-
 template <typename T> cpp::add_rvalue_reference_t<T> declval() {
-  static_assert(detail::always_false<T>,
+  static_assert(cpp::always_false<T>,
                 "declval not allowed in an evaluated context");
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 17e4913749d51c6..160998049fcd2a8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -289,6 +289,7 @@ libc_support_library(
         "src/__support/CPP/type_traits/add_lvalue_reference.h",
         "src/__support/CPP/type_traits/add_pointer.h",
         "src/__support/CPP/type_traits/add_rvalue_reference.h",
+        "src/__support/CPP/type_traits/always_false.h",
         "src/__support/CPP/type_traits/bool_constant.h",
         "src/__support/CPP/type_traits/conditional.h",
         "src/__support/CPP/type_traits/decay.h",



More information about the libc-commits mailing list