Skip to content

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

Description

@peterazmanov

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions