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

Custom formatter can trigger compiler bug for GCC < v12.0 (constexpr downcast) #3128

Closed
peterazmanov opened this issue Oct 6, 2022 · 1 comment

Comments

@peterazmanov
Copy link

peterazmanov commented Oct 6, 2022

Test case (https://godbolt.org/z/1KvqKjf4n):

#include <fmt/core.h>

#include <string_view>

struct X
{
    double d = 0.1;
};

template <>
struct fmt::formatter<X> : fmt::formatter<double>
{
   constexpr auto parse(fmt::basic_format_parse_context<char> & ctx) -> decltype(ctx.begin())
   {
       return fmt::formatter<double>::parse(ctx);
   }

   template <class FormatContext>
   constexpr auto format(X x, FormatContext & ctx) const
   {
       return fmt::formatter<double>::format(x.d, ctx);
   }
};

int main()
{
    using namespace std::literals;
    return fmt::format("{:<{}}"sv, X{}, 30).size();
}

causes compilation error due to GCC compiler bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103879#c6).
Error message:
error: accessing value of '<anonymous>.fmt::v9::detail::format_string_checker<char, fmt::v9::detail::error_handler, X, int>::context_.fmt::v9::detail::compile_parse_context<char, fmt::v9::detail::error_handler>::<anonymous>' through a 'fmt::v9::detail::compile_parse_context<char, fmt::v9::detail::error_handler>' glvalue in a constant expression

This can be workarounded with parse context template parameter:

   template <typename ParseContext>
   constexpr auto parse(ParseContext & ctx) -> decltype(ctx.begin())

Perhaps it's a good idea to add a workaround in fmt code, somehow.
If not, this issue can be closed, hope it'll help someone who stumbles upon this obscure bug.

@vitaut
Copy link
Contributor

vitaut commented Oct 6, 2022

Worked around in 491c32c. Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants