Skip to content

Forward outer context in nested_formatter for dynamic precision#4750

Open
barry3406 wants to merge 1 commit intofmtlib:mainfrom
barry3406:fix/nested-formatter-dynamic-precision
Open

Forward outer context in nested_formatter for dynamic precision#4750
barry3406 wants to merge 1 commit intofmtlib:mainfrom
barry3406:fix/nested-formatter-dynamic-precision

Conversation

@barry3406
Copy link
Copy Markdown

Dynamic precision like {:.{}} on a nested_formatter previously failed with argument not found, because the inner formatter stored an arg_ref that was being resolved against the intermediate format_to context rather than the original outer format call.

nested() now takes the outer format_context, and nested_view plumbs the outer args and locale through to formatter<nested_view>::format, so dynamic width/precision references resolve against the user's original argument list. The one-argument nested(value) overload is kept for backward compatibility; existing static-precision uses are unaffected.

Fixes #3860.

Dynamic width/precision arg indices inside a nested_formatter referred to
the arguments of the intermediate format_to invocation, not the original
outer format call, so "{:.{}}" failed with 'argument not found'.

Make nested() accept the outer context and plumb its args and locale
through nested_view, so the inner formatter resolves dynamic specs
against the original argument list.

Fixes fmtlib#3860.
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.

Comment thread include/fmt/format.h
Comment on lines +4112 to +4114
auto outer_ctx = OuterContext(ctx.out(), view.outer_args, view.outer_loc);
auto it = view.fmt->format(*view.value, outer_ctx);
ctx.advance_to(it);
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.

This will only work if iterators are compatible (same context types) so there shouldn't be parameterization on OuterContext.

Comment thread test/format-test.cc
}

TEST(format_test, nested_formatter_dynamic_precision) {
// https://github.com/fmtlib/fmt/issues/3860
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.

This is not needed, let's remove.

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.

Dynamic precision for nested_formatter does not work

2 participants