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 FMT_COMPILE() with custom types #2326

Merged
merged 1 commit into from
Jun 1, 2021

Conversation

alexezeder
Copy link
Contributor

@alexezeder alexezeder commented May 30, 2021

By reading the changelog for upcoming release, I realized that it does not mention one of the major breaking change of custom formatters for compile-time formatting - const-qualified format method, so I decided to check how do custom formatters work at compile-time and realized that they actually don't work.

There are 3 commits, by the complexity of changes from the commit that just fixes the library, to the commit that adds dynamic width handling for the test_formattable class in compile-test.cc. The latter two can be removed or squashed into the first one, on demand. This time I cannot use custom formatters from chrono.h because they use memory_buffer and it's not ready to be used at compile-time yet.

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.

Comment on lines 71 to 100
struct spec_handler {
formatter& f;
basic_format_parse_context<char>& context;

void on_error(const char* msg) { FMT_THROW(format_error(msg)); }
FMT_CONSTEXPR void on_fill(basic_string_view<char> fill) {
f.specs.fill = fill;
}
FMT_CONSTEXPR void on_align(align_t align) { f.specs.align = align; }
FMT_CONSTEXPR void on_width(int width) { f.specs.width = width; }
FMT_CONSTEXPR void on_precision(int) {}
FMT_CONSTEXPR void end_precision() {}
template <typename Id>
FMT_CONSTEXPR detail::arg_ref<char> make_arg_ref(Id arg_id) {
context.check_arg_id(arg_id);
return detail::arg_ref<char>(arg_id);
}
FMT_CONSTEXPR detail::arg_ref<char> make_arg_ref(
basic_string_view<char> arg_id) {
context.check_arg_id(arg_id);
return detail::arg_ref<char>(arg_id);
}
FMT_CONSTEXPR detail::arg_ref<char> make_arg_ref(detail::auto_id) {
return detail::arg_ref<char>(context.next_arg_id());
}
template <typename Id> FMT_CONSTEXPR void on_dynamic_width(Id arg_id) {
f.width_ref = make_arg_ref(arg_id);
}
template <typename Id> FMT_CONSTEXPR void on_dynamic_precision(Id) {}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit of an overkill for the test. Let's do some basic parsing instead of relying on internal API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, commits 1+2 then, dropping the 3

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, could you update the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, I will do so, but maybe an hour later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@vitaut
Copy link
Contributor

vitaut commented May 31, 2021

it does not mention one of the major breaking change of custom formatters for compile-time formatting - const-qualified format method

Good catch, added to changelog in 9bb406d.

@alexezeder alexezeder force-pushed the fix/fmt_compile_with_custom_types branch from cd72ce7 to 7bdc3fd Compare June 1, 2021 15:16
@alexezeder alexezeder requested a review from vitaut June 1, 2021 15:25
@vitaut vitaut merged commit 9976869 into fmtlib:master Jun 1, 2021
@vitaut
Copy link
Contributor

vitaut commented Jun 1, 2021

Thank you!

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