E570
Skip to content

Fix fallback to runtime API from compile-time API#2143

Merged
vitaut merged 3 commits intofmtlib:masterfrom
alexezeder:fix/fallback_to_runtime_api_for_unknown_format
Mar 4, 2021
Merged

Fix fallback to runtime API from compile-time API#2143
vitaut merged 3 commits intofmtlib:masterfrom
alexezeder:fix/fallback_to_runtime_api_for_unknown_format

Conversation

@alexezeder
Copy link
Copy Markdown
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
Copy Markdown
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.

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

2 participants

0