1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-16 07:22:43 +01:00

libutil: Use std::source_location for unreachable

Make unreachable a function instead of a macro, since
C++20 provides a convenience class as a replacement for
older __FILE__, __LINE__ macros.
This commit is contained in:
Sergei Zimmerman 2025-08-29 00:21:04 +03:00
parent 1f607b5def
commit d59b959c87
No known key found for this signature in database
2 changed files with 11 additions and 10 deletions

View file

@ -6,6 +6,7 @@
#include "nix/util/terminal.hh"
#include "nix/util/position.hh"
#include <cinttypes>
#include <iostream>
#include <optional>
#include "nix/util/serialise.hh"
@ -439,10 +440,16 @@ void panic(std::string_view msg)
std::terminate();
}
void panic(const char * file, int line, const char * func)
void unreachable(std::source_location loc)
{
char buf[512];
int n = snprintf(buf, sizeof(buf), "Unexpected condition in %s at %s:%d", func, file, line);
int n = snprintf(
buf,
sizeof(buf),
"Unexpected condition in %s at %s:%" PRIuLEAST32,
loc.function_name(),
loc.file_name(),
loc.line());
if (n < 0)
panic("Unexpected condition and could not format error message");
panic(std::string_view(buf, std::min(static_cast<int>(sizeof(buf)), n)));