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

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

Merged
merged 2 commits into from
Sep 14, 2023

Conversation

gchatelet
Copy link
Contributor

No description provided.

@gchatelet gchatelet requested a review from a team as a code owner September 13, 2023 13:48
@llvmbot llvmbot added the libc label Sep 13, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2023

@llvm/pr-subscribers-libc

Changes None -- Full diff: https://github.com//pull/66209.diff

4 Files Affected:

  • (modified) libc/src/__support/CPP/CMakeLists.txt (+1)
  • (added) libc/src/__support/CPP/type_traits/always_false.h (+25)
  • (modified) libc/src/__support/CPP/utility/declval.h (+2-6)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+1)
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",

@gchatelet gchatelet merged commit aee8f87 into llvm:main Sep 14, 2023
2 checks passed
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this pull request Sep 14, 2023
@gchatelet gchatelet deleted the add_always_false branch September 15, 2023 08:14
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants