Skip to content

[libc++][format] Fixes nested concept evaluation. #85548

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

Merged
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
3 changes: 1 addition & 2 deletions libcxx/include/__format/format_arg_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ consteval __arg_t __determine_arg_t() {
// The overload for not formattable types allows triggering the static
// assertion below.
template <class _Context, class _Tp>
requires(!__formattable<_Tp, typename _Context::char_type>)
requires(!__formattable_with<_Tp, _Context>)
consteval __arg_t __determine_arg_t() {
return __arg_t::__none;
}
Expand All @@ -165,7 +165,6 @@ _LIBCPP_HIDE_FROM_ABI basic_format_arg<_Context> __create_format_arg(_Tp& __valu
using _Dp = remove_const_t<_Tp>;
constexpr __arg_t __arg = __determine_arg_t<_Context, _Dp>();
static_assert(__arg != __arg_t::__none, "the supplied type is not formattable");

static_assert(__formattable_with<_Tp, _Context>);

// Not all types can be used to directly initialize the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

// XFAIL: availability-fp_to_chars-missing

// The sample code is based on the bug report
// https://github.com/llvm/llvm-project/issues/81590
//
// Tests whether this formatter does not fail to compile due to nested concept
// evaluation.

#include <format>
#include <variant>

struct X : std::variant<X*> {
X* p = nullptr;
constexpr const std::variant<X*>& decay() const noexcept { return *this; }
};

template <>
struct std::formatter<X, char> : std::formatter<std::string, char> {
static constexpr auto format(const X& x, auto ctx) {
if (!x.p)
return ctx.out();
auto m = [&](const X* t) { return std::format_to(ctx.out(), "{}", *t); };
return std::visit(m, x.decay());
}
};

void bug_81590() { (void)std::format("{}", X{}); }