aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/print-ambiguous.cc
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-03-08 03:05:47 +0100
committereldritch horrors <pennae@lix.systems>2024-03-09 03:50:06 +0100
commit512c1f05c37c612347dd1fda4771a09744a1a3cd (patch)
tree9ae77da1a45060b12520326b426efc5014a51c0b /src/libexpr/print-ambiguous.cc
parent0e8f505f666d9b0714840e4fe878e6aece918908 (diff)
Unify and refactor value printing
Previously, there were two mostly-identical value printers -- one in `libexpr/eval.cc` (which didn't force values) and one in `libcmd/repl.cc` (which did force values and also printed ANSI color codes). This PR unifies both of these printers into `print.cc` and provides a `PrintOptions` struct for controlling the output, which allows for toggling whether values are forced, whether repeated values are tracked, and whether ANSI color codes are displayed. Additionally, `PrintOptions` allows tuning the maximum number of attributes, list items, and bytes in a string that will be displayed; this makes it ideal for contexts where printing too much output (e.g. all of Nixpkgs) is distracting. (As requested by @roberth in https://github.com/NixOS/nix/pull/9554#issuecomment-1845095735) Please read the tests for example output. Future work: - It would be nice to provide this function as a builtin, perhaps `builtins.toStringDebug` -- a printing function that never fails would be useful when debugging Nix code. - It would be nice to support customizing `PrintOptions` members on the command line, e.g. `--option to-string-max-attrs 1000`. (cherry picked from commit 0fa08b451682fb3311fe58112ff05c4fe5bee3a4, ) === Restore ambiguous value printer for `nix-instantiate` The Nix team has requested that this output format remain unchanged. I've added a warning to the man page explaining that `nix-instantiate --eval` output will not parse correctly in many situations. (cherry picked from commit df84dd4d8dd3fd6381ac2ca3064432ab31a16b79) Change-Id: I7cca6b4b53cd0642f2d49af657d5676a8554c9f8
Diffstat (limited to 'src/libexpr/print-ambiguous.cc')
-rw-r--r--src/libexpr/print-ambiguous.cc99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/libexpr/print-ambiguous.cc b/src/libexpr/print-ambiguous.cc
new file mode 100644
index 000000000..fb59bbf4b
--- /dev/null
+++ b/src/libexpr/print-ambiguous.cc
@@ -0,0 +1,99 @@
+#include "print-ambiguous.hh"
+#include "print.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.s);
+ 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");
+ abort();
+ }
+}
+
+}