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

Add FMT_STRING for format_to() call with clang-cl #3413

Merged
merged 1 commit into from
May 3, 2023
Merged

Add FMT_STRING for format_to() call with clang-cl #3413

merged 1 commit into from
May 3, 2023

Conversation

mogemimi
Copy link
Contributor

I get the following error when compiling os.cc on clang-cl 15.

1>..\fmt\src\os.cc(172,52): error : call to consteval function 'fmt::basic_format_string<char, const char *&, fmt::detail::utf16_to_utf8 &>::basic_format_string<char[7], 0>' is not a constant expression
1>..\fmt\include\fmt/core.h(3165,62): message : non-constexpr constructor 'basic_string_view' cannot be used in a constant expression
1>..\fmt\src\os.cc(172,52): message : in call to 'basic_format_string("{}: {}")'
1>..\fmt\include\fmt/core.h(451,3): message : declared here

This pull request adds a FMT_STRING wrapper to resolve a format-string type and fixes the error.

@vitaut
Copy link
Contributor

vitaut commented Apr 30, 2023

FMT_STRING shouldn't be needed here. Please provide a complete repro, ideally on godbolt.

@mogemimi
Copy link
Contributor Author

@vitaut
Thank you for replying. When I compiled os.cc with /std:c++20 and -fmsc-version=1913 on clang-cl 15, the above error occurred. I had overlooked that I had specified -fmsc-version= option.

The reproduction procedure is as follows

  1. Install LLVM 15 on Windows. Or install clang-cl 15 from Visual Studio 2022 installer.
    # "/c/Program Files/LLVM/bin/clang-cl.exe" --version
    
    clang version 15.0.7
    Target: x86_64-pc-windows-msvc
    Thread model: posix
    InstalledDir: C:\Program Files\LLVM\bin
    # "/c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/bin/clang-cl.exe" --version
    
    clang version 15.0.1
    Target: i686-pc-windows-msvc
    Thread model: posix
    InstalledDir: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin
  2. Go to the fmt directory.
    # cd path/to/fmt
    # ls src/os.cc
  3. Run the following command to compile src/os.cc using clang-cl:
    # "/c/Program Files/LLVM/bin/clang-cl.exe" -Xclang -Iinclude /std:c++20 -fmsc-version=1913 /EHsc /diagnostics:column -Xclang -fcxx-exceptions src/os.cc
    
    src/os.cc(172,52): error: call to consteval function 'fmt::basic_format_string<char, const char *&, fmt::basic_string_view<char>>::basic_format_string<char[7], 0>' is not a constant expression
    include\fmt/core.h(2758,62): note: non-constexpr constructor 'basic_string_view' cannot be used in a constant expression
    src/os.cc(172,52): note: in call to 'basic_format_string("{}: {}")'
    include\fmt/core.h(421,3): note: declared here

In addition, when I specified /std:c++14 or /std:c++17 instead of /std:c++20, the error did not occur. Also, specifying 1914 or higher for -fmsc-version= like -fmsc-version=1914 did not cause an error.

The reason seems to be that FMT_CONSTEXPR_CHAR_TRAITS is enabled when FMT_MSC_VERSION is 1914 or higher.

fmt/include/fmt/core.h

Lines 124 to 126 in 02cae7e

#elif FMT_MSC_VERSION >= 1914 && FMT_CPLUSPLUS >= 201703L
# define FMT_CONSTEXPR_CHAR_TRAITS constexpr
#endif

@vitaut vitaut merged commit d8973bf into fmtlib:master May 3, 2023
@vitaut
Copy link
Contributor

vitaut commented May 3, 2023

I think it's OK as a workaround considering that we use FMT_STRING in similar cases elsewhere. Thanks.

@mogemimi mogemimi deleted the fix-compile-error-clang-cl-15 branch May 3, 2023 15:06
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