aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/print-ambiguous.cc
blob: bcf86b5c62f0f3aaca364cb9ecff11c5f19b9339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "print-ambiguous.hh"
#include "attr-set.hh"
#include "logging.hh"
#include "print.hh"
#include "signals.hh"
#include "escape-string.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:
        escapeString(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("Lix evaluator internal error: printAmbiguous: invalid value type");
        abort();
    }
}

}