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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/src/__support/CPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions libc/src/__support/CPP/type_traits/always_false.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- 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
//
// This will be fixed in C++23 according to [CWG
// 2518](https://cplusplus.github.io/CWG/issues/2518.html).

// Usage `static_assert(cpp::always_false<T>, "error message");`
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
8 changes: 2 additions & 6 deletions libc/src/__support/CPP/utility/declval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
1 change: 1 addition & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down