mirror of
https://github.com/NixOS/nix
synced 2025-06-27 16:51:15 +02:00
The short answer for why we need to do this is so we can consistently do `#include "nix/..."`. Without this change, there are ways to still make that work, but they are hacky, and they have downsides such as making it harder to make sure headers from the wrong Nix library (e..g. `libnixexpr` headers in `libnixutil`) aren't being used. The C API alraedy used `nix_api_*`, so its headers are *not* put in subdirectories accordingly. Progress on #7876 We resisted doing this for a while because it would be annoying to not have the header source file pairs close by / easy to change file path/name from one to the other. But I am ameliorating that with symlinks in the next commit.
101 lines
2.8 KiB
C++
101 lines
2.8 KiB
C++
#include "nix/print-ambiguous.hh"
|
|
#include "nix/print.hh"
|
|
#include "nix/signals.hh"
|
|
#include "nix/eval.hh"
|
|
|
|
namespace nix {
|
|
|
|
// See: https://github.com/NixOS/nix/issues/9730
|
|
void printAmbiguous(
|
|
Value &v,
|
|
const SymbolTable &symbols,
|
|
std::ostream &str,
|
|
std::set<const void *> *seen,
|
|
int depth)
|
|
{
|
|
checkInterrupt();
|
|
|
|
if (depth <= 0) {
|
|
str << "«too deep»";
|
|
return;
|
|
}
|
|
switch (v.type()) {
|
|
case nInt:
|
|
str << v.integer();
|
|
break;
|
|
case nBool:
|
|
printLiteralBool(str, v.boolean());
|
|
break;
|
|
case nString:
|
|
printLiteralString(str, v.string_view());
|
|
break;
|
|
case nPath:
|
|
str << v.path().to_string(); // !!! escaping?
|
|
break;
|
|
case nNull:
|
|
str << "null";
|
|
break;
|
|
case nAttrs: {
|
|
if (seen && !v.attrs()->empty() && !seen->insert(v.attrs()).second)
|
|
str << "«repeated»";
|
|
else {
|
|
str << "{ ";
|
|
for (auto & i : v.attrs()->lexicographicOrder(symbols)) {
|
|
str << symbols[i->name] << " = ";
|
|
printAmbiguous(*i->value, symbols, str, seen, depth - 1);
|
|
str << "; ";
|
|
}
|
|
str << "}";
|
|
}
|
|
break;
|
|
}
|
|
case nList:
|
|
if (seen && v.listSize() && !seen->insert(v.listElems()).second)
|
|
str << "«repeated»";
|
|
else {
|
|
str << "[ ";
|
|
for (auto v2 : v.listItems()) {
|
|
if (v2)
|
|
printAmbiguous(*v2, symbols, str, seen, depth - 1);
|
|
else
|
|
str << "(nullptr)";
|
|
str << " ";
|
|
}
|
|
str << "]";
|
|
}
|
|
break;
|
|
case nThunk:
|
|
if (!v.isBlackhole()) {
|
|
str << "<CODE>";
|
|
} else {
|
|
// Although we know for sure that it's going to be an infinite recursion
|
|
// when this value is accessed _in the current context_, it's likely
|
|
// that the user will misinterpret a simpler «infinite recursion» output
|
|
// as a definitive statement about the value, while in fact it may be
|
|
// a valid value after `builtins.trace` and perhaps some other steps
|
|
// have completed.
|
|
str << "«potential infinite recursion»";
|
|
}
|
|
break;
|
|
case nFunction:
|
|
if (v.isLambda()) {
|
|
str << "<LAMBDA>";
|
|
} else if (v.isPrimOp()) {
|
|
str << "<PRIMOP>";
|
|
} else if (v.isPrimOpApp()) {
|
|
str << "<PRIMOP-APP>";
|
|
}
|
|
break;
|
|
case nExternal:
|
|
str << *v.external();
|
|
break;
|
|
case nFloat:
|
|
str << v.fpoint();
|
|
break;
|
|
default:
|
|
printError("Nix evaluator internal error: printAmbiguous: invalid value type");
|
|
unreachable();
|
|
}
|
|
}
|
|
|
|
}
|