[libcxx-commits] [libcxx] [libc++] Fix std::variant evaluating template arguments too eagerly (PR #151028)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Aug 5 01:30:15 PDT 2025
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/151028
>From 9c7f246e1af917e3ea92e34b6aacebf6315d7b1e Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 28 Jul 2025 21:35:47 +0200
Subject: [PATCH] [libc++] Fix std::variant/std::invoke too eager instantiation
---
libcxx/include/__type_traits/invoke.h | 10 +++++-----
libcxx/include/string_view | 2 +-
.../string.view/assert.ctor.length.pass.cpp | 10 +++++++++-
.../variant.variant/variant.ctor/T.pass.cpp | 16 ++++++++++++++++
4 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/__type_traits/invoke.h b/libcxx/include/__type_traits/invoke.h
index 5ff2efbe5faaf..3f5626c014432 100644
--- a/libcxx/include/__type_traits/invoke.h
+++ b/libcxx/include/__type_traits/invoke.h
@@ -67,20 +67,20 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if __has_builtin(__builtin_invoke)
-template <class... _Args>
-using __invoke_result_t _LIBCPP_NODEBUG = decltype(__builtin_invoke(std::declval<_Args>()...));
-
template <class, class... _Args>
struct __invoke_result_impl {};
template <class... _Args>
-struct __invoke_result_impl<__void_t<__invoke_result_t<_Args...> >, _Args...> {
- using type _LIBCPP_NODEBUG = __invoke_result_t<_Args...>;
+struct __invoke_result_impl<__void_t<decltype(__builtin_invoke(std::declval<_Args>()...))>, _Args...> {
+ using type _LIBCPP_NODEBUG = decltype(__builtin_invoke(std::declval<_Args>()...));
};
template <class... _Args>
using __invoke_result _LIBCPP_NODEBUG = __invoke_result_impl<void, _Args...>;
+template <class... _Args>
+using __invoke_result_t _LIBCPP_NODEBUG = typename __invoke_result<_Args...>::type;
+
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __invoke_result_t<_Args...> __invoke(_Args&&... __args)
_NOEXCEPT_(noexcept(__builtin_invoke(std::forward<_Args>(__args)...))) {
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index f86b2722aca6c..b2e4d0af4f6ae 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -320,7 +320,7 @@ public:
_LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
_LIBCPP_DIAGNOSE_NULLPTR_IF(__len != 0 && __s == nullptr, " if len is not zero")
: __data_(__s), __size_(__len) {
-# if _LIBCPP_STD_VER >= 14
+# if !defined(_LIBCPP_CXX03_LANG) && (!defined(_LIBCPP_COMPILER_GCC) || _LIBCPP_STD_VER >= 14)
// Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input
// range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
// passed in a negative length.
diff --git a/libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp b/libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp
index af8b393f9e0a7..ef3b2972456e4 100644
--- a/libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp
+++ b/libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp
@@ -7,7 +7,8 @@
//===----------------------------------------------------------------------===//
// REQUIRES: has-unix-headers
-// UNSUPPORTED: c++03, c++11
+// UNSUPPORTED: c++03
+// UNSUPPORTED: c++11 && gcc
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
@@ -17,10 +18,17 @@
#include <string_view>
#include "check_assertion.h"
+#include "test_macros.h"
+
+// We're testing for assertions here, so let's not diagnose the misuses at compile time
+// FIXME: This should really be in ADDITIONAL_COMPILE_FLAGS, but it that doesn't work due to a Clang bug
+TEST_CLANG_DIAGNOSTIC_IGNORED("-Wnonnull")
int main(int, char**) {
char c = 0;
TEST_LIBCPP_ASSERT_FAILURE(
std::string_view(&c, -1), "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
+ TEST_LIBCPP_ASSERT_FAILURE(std::string_view(nullptr, 1),
+ "string_view::string_view(_CharT *, size_t): received nullptr");
return 0;
}
diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
index 142da1d820d9a..6111138726dbc 100644
--- a/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
+++ b/libcxx/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
@@ -173,6 +173,11 @@ void test_vector_bool() {
assert(std::get<0>(v) == true);
}
+struct ConvertibleFromAny {
+ template <class V>
+ ConvertibleFromAny(V) {}
+};
+
int main(int, char**) {
test_T_ctor_basic();
test_T_ctor_noexcept();
@@ -180,5 +185,16 @@ int main(int, char**) {
test_no_narrowing_check_for_class_types();
test_construction_with_repeated_types();
test_vector_bool();
+
+ { // Check that the constraints are evaluated lazily (see https://github.com/llvm/llvm-project/issues/151328)
+ struct Matcher {
+ Matcher() {}
+ Matcher(std::variant<ConvertibleFromAny>) {}
+ };
+
+ Matcher vec;
+ [[maybe_unused]] Matcher m = std::move(vec);
+ }
+
return 0;
}
More information about the libcxx-commits
mailing list