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

fix case of variant which is valueless by exception #3347

Merged
merged 7 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 9 additions & 5 deletions include/fmt/std.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,15 @@ struct formatter<
auto out = ctx.out();

out = detail::write<Char>(out, "variant(");
std::visit(
[&](const auto& v) {
out = detail::write_variant_alternative<Char>(out, v);
},
value);
try {
std::visit(
[&](const auto& v) {
out = detail::write_variant_alternative<Char>(out, v);
},
value);
} catch (const std::bad_variant_access&) {
detail::write<Char>(out, "valueless by exception");
}
*out++ = ')';
return out;
}
Expand Down
30 changes: 30 additions & 0 deletions test/std-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "fmt/std.h"

#include <stdexcept>
#include <string>
#include <vector>

Expand Down Expand Up @@ -95,6 +96,23 @@ TEST(std_test, optional) {
#endif
}

struct throws_on_move {
throws_on_move() = default;

[[noreturn]] throws_on_move(throws_on_move&&) {
throw std::runtime_error("Thrown by throws_on_move");
}

throws_on_move(const throws_on_move&) = default;
};

template <> struct fmt::formatter<throws_on_move> : formatter<string_view> {
auto format(const throws_on_move&, format_context& ctx) const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a return type for compatibility with older compilers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My apologies once again. I've fixed this.

string_view str("<throws_on_move>");
return formatter<string_view>::format(str, ctx);
}
};

TEST(std_test, variant) {
#ifdef __cpp_lib_variant
EXPECT_EQ(fmt::format("{}", std::monostate{}), "monostate");
Expand Down Expand Up @@ -126,6 +144,18 @@ TEST(std_test, variant) {

volatile int i = 42; // Test compile error before GCC 11 described in #3068.
EXPECT_EQ(fmt::format("{}", i), "42");

std::variant<std::monostate, throws_on_move> v6;

try {
throws_on_move thrower;
v6.emplace<throws_on_move>(std::move(thrower));
} catch (const std::runtime_error&) {
}
// v6 is now valueless by exception

EXPECT_EQ(fmt::format("{}", v6), "variant(valueless by exception)");

#endif
}

Expand Down