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

Missing named arguments do not cause compilation error when using FMT_COMPILE #4124

Open
schaerfo opened this issue Aug 17, 2024 · 1 comment

Comments

@schaerfo
Copy link

Specifying a named argument in the format string but not providing a value does not result in a compilation error on recent GCC and Clang versions when the format string is compiled using FMT_COMPILE.

This is the case both when the argument is missing altogether (1) and when it is present but no value is assigned (2).

When the FMT_COMPILE macro is removed, both fmt::format calls result in compilation errors.

#include <fmt/format.h>
#include <fmt/compile.h>

int main() {
    using namespace fmt::literals;
    fmt::format(FMT_COMPILE("{x}")); // 1
    fmt::format(FMT_COMPILE("{x}"), "x"_a); // 2
    return 0;
}

On Compiler Explorer: https://godbolt.org/z/4zGrofeoE

@Lounarok
Copy link
Contributor

Lounarok commented Dec 2, 2024

Did some investigation but don't have time to work on it yet. Feel free if anyone would like to help.
The problem seems to be inside how FMT_COMPILE() handles named arguments in compile_format_string().
When else if constexpr (arg_id_result.kind == arg_id_kind::name) is true, get_arg_index_by_name() should return a value non -1 to compile time named argument. I'm not quite sure if it's broken or I don't get the implementation in correct way. It seems like if constexpr (arg_index >= 0) { condition is never met.. then goes to below section all the time.

 else if constexpr (c == '}') {
          // static_assert(false, "foo");
          return parse_tail<Args, arg_id_end_pos + 1, ID>(
              runtime_named_field<char_type>{arg_id_result.arg_id.name}, fmt);
}

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

No branches or pull requests

3 participants