-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Switch to std::format #10294
Comments
I looked into this a bit and Would it be acceptable to add {fmt} as a dependency until nix use-format → cat format_test.cc
#include <cassert>
#include <format>
int main()
{
std::string message = std::format("The answer is {}.", 42);
assert(message == "The answer is 42.");
}
nix use-format → clang++ -o format_test -std=c++2a format_test.cc
format_test.cc:2:10: fatal error: 'format' file not found
#include <format>
^~~~~~~~
1 error generated.
nix use-format → clang++ --version
clang version 16.0.6
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /nix/store/xph7agmqsna5ia7h699acfmk9gp4iks3-clang-16.0.6/bin
nix use-format → g++ --version
gcc (GCC) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
nix use-format → g++ -o format_test -std=c++2a format_test.cc
format_test.cc:2:10: fatal error: format: No such file or directory
2 | #include <format>
| ^~~~~~~~
compilation terminated. |
Yes that might be an option. Or we wait for GCC 13 and Clang 14. |
Hi, I am new to open source and have looked into this issue for a while now. If I am not wrong, our target file is this: https://github.com/NixOS/nix/blob/master/src/libutil/fmt.hh Since std::format is now supported in gcc compiler version 13.1.0, we can replace boost::format with std::format. But, this will require many changes to the code base, mainly due to differences in the syntax of boost::format and std::format. In the fmt.hh file, the class HintFmt is defined as a wrapper class for boost::format in the fmt.hh file. There are around 450 calls to HintFmt() in the entire codebase. In each call to HintFmt(), the first argument is a std::string e.g :- In the file https://github.com/NixOS/nix/blob/master/src/libexpr/parser-state.hh at line 68, the following code is present: inline void ParserState::dupAttr(Symbol attr, const PosIdx pos, const PosIdx prevPos)
{
throw ParseError({
.msg = HintFmt("attribute '%1%' already defined at %2%", symbols[attr], positions[prevPos]),
.pos = positions[pos]
});
} if we are using std::format instead of boost::format, we must change the string We can replace these strings now, consider this call to HintFmt(): if (!fromPath)
throw Error({
.msg = HintFmt("attribute '%s' is missing in call to 'fetchClosure'", "fromPath"),
.pos = state.positions[pos]
}); Like printf(), boost::format supports format specifiers like %s , but std::format does not. So, we have to change We must also rewrite the HintFmt constructors and the operator overloads to support std::format. Is this a correct approach? Please look into this. |
Is your feature request related to a problem? Please describe.
Unlike
boost::format
,std::format
is supposed to check format strings at compile time, so it should prevent problems like #10293 once and for all.Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
Priorities
Add 👍 to issues you find important.
The text was updated successfully, but these errors were encountered: