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 fallback to runtime API from compile-time API #2143

Conversation

alexezeder
Copy link
Contributor

@alexezeder alexezeder commented Feb 21, 2021

Right now, when {fmt} fails to prepare compiled format for format string (using FMT_COMPILE or _cf) it also fails to report about that clearly. Here is an example - https://godbolt.org/z/G1ETdc.

There were some attempts to make the error a bit clearer, but as it turned out {fmt} should fall back to the runtime API in those cases. This PR adds that fallback.

Some points for the PR:

  • test added
  • there is a bit of code duplication introduced, because I decided that this code is clearer:
    #ifdef __cpp_if_constexpr
      if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
                                 detail::unknown_format>()) {
        return format(static_cast<basic_string_view<typename S::char_type>>(S()),
                      std::forward<Args>(args)...);
      } else {
        return format(compiled, std::forward<Args>(args)...);
      }
    #else
      return format(compiled, std::forward<Args>(args)...);
    #endif
    then this one:
    #ifdef __cpp_if_constexpr
      if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
                                 detail::unknown_format>()) {
        return format(static_cast<basic_string_view<typename S::char_type>>(S()),
                      std::forward<Args>(args)...);
      } else {
    #endif
        return format(compiled, std::forward<Args>(args)...);
    #ifdef __cpp_if_constexpr
      }
    #endif

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Looks good but I don't think we should provide a way to disable the fallback.

include/fmt/compile.h Show resolved Hide resolved
include/fmt/compile.h Outdated Show resolved Hide resolved
@alexezeder alexezeder requested a review from vitaut March 1, 2021 20:23
Comment on lines 847 to 861
constexpr auto compiled = detail::compile<Args...>(S());
#ifdef __cpp_if_constexpr
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
detail::unknown_format>()) {
auto it =
format_to(detail::truncating_iterator<OutputIt>(out, n),
static_cast<basic_string_view<typename S::char_type>>(S()),
std::forward<Args>(args)...);
return {it.base(), it.count()};
} else {
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
std::forward<Args>(args)...);
return {it.base(), it.count()};
}
#else
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we replace all of these by forwarding S to format_to call below which will handle fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can. It will create one more template instantiation, but it shouldn't be tough.
So, it's done.

…e API

instead of compiling it inside format_to_n(), to eliminate code duplication
@alexezeder alexezeder requested a review from vitaut March 4, 2021 00:03
@vitaut vitaut merged commit 684b5b0 into fmtlib:master Mar 4, 2021
@vitaut
Copy link
Contributor

vitaut commented Mar 4, 2021

Thank you!

@alexezeder alexezeder deleted the fix/fallback_to_runtime_api_for_unknown_format branch March 8, 2021 00:57
@YarikTH YarikTH mentioned this pull request Mar 29, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants