libexpr: fix elided value counting in printer

using the total-attrs-printed and total-list-items-printed counters to
calculate how many attrs were elided only works properly if no nesting
is involved. once things do nest the global counter can exceed the size
of the currently printed object, leading to unsigned wrapping and great
overestimation of elided counts. counting locally in addition to global
counts fixes this.

these are functional tests because creating these objects requires the
evaluator to not be a huge amount of code, and we also want defaults to
be tested for cli usage.

fixes #14

Change-Id: Icb9a0cb21b2f4bacbc5e9dcdd8c0b9055b4088a7
This commit is contained in:
eldritch horrors 2024-03-10 07:44:21 +01:00
parent 2a8f579c53
commit c26599b143
6 changed files with 39 additions and 3 deletions

View file

@ -337,11 +337,12 @@ private:
auto prettyPrint = shouldPrettyPrintAttrs(sorted); auto prettyPrint = shouldPrettyPrintAttrs(sorted);
size_t printedHere = 0;
for (auto & i : sorted) { for (auto & i : sorted) {
printSpace(prettyPrint); printSpace(prettyPrint);
if (attrsPrinted >= options.maxAttrs) { if (attrsPrinted >= options.maxAttrs) {
printElided(sorted.size() - attrsPrinted, "attribute", "attributes"); printElided(sorted.size() - printedHere, "attribute", "attributes");
break; break;
} }
@ -350,6 +351,7 @@ private:
print(*i.second, depth + 1); print(*i.second, depth + 1);
output << ";"; output << ";";
attrsPrinted++; attrsPrinted++;
printedHere++;
} }
decreaseIndent(); decreaseIndent();
@ -394,11 +396,12 @@ private:
output << "["; output << "[";
auto listItems = v.listItems(); auto listItems = v.listItems();
auto prettyPrint = shouldPrettyPrintList(listItems); auto prettyPrint = shouldPrettyPrintList(listItems);
size_t printedHere = 0;
for (auto elem : listItems) { for (auto elem : listItems) {
printSpace(prettyPrint); printSpace(prettyPrint);
if (listItemsPrinted >= options.maxListItems) { if (listItemsPrinted >= options.maxListItems) {
printElided(v.listSize() - listItemsPrinted, "item", "items"); printElided(v.listSize() - printedHere, "item", "items");
break; break;
} }
@ -408,6 +411,7 @@ private:
printNullptr(); printNullptr();
} }
listItemsPrinted++; listItemsPrinted++;
printedHere++;
} }
decreaseIndent(); decreaseIndent();

View file

@ -6,4 +6,4 @@ error:
| ^ | ^
10| 10|
error: cannot coerce a set to a string: { a = { a = { a = { a = "ha"; b = "ha"; c = "ha"; d = "ha"; e = "ha"; f = "ha"; g = "ha"; h = "ha"; j = "ha"; }; «4294967295 attributes elided» }; «4294967294 attributes elided» }; «4294967293 attributes elided» } error: cannot coerce a set to a string: { a = { a = { a = { a = "ha"; b = "ha"; c = "ha"; d = "ha"; e = "ha"; f = "ha"; g = "ha"; h = "ha"; j = "ha"; }; «8 attributes elided» }; «8 attributes elided» }; «8 attributes elided» }

View file

@ -0,0 +1,12 @@
error:
… in the condition of the assert statement
at /pwd/lang/eval-fail-print-limit-list.nix:1:1:
1| assert (
| ^
2| let x = [ 1 [ 2 3 4 5 6 7 8 9 x 10 11 ] 12 ];
error: expected a Boolean but found a list: [ 1 [ 2 3 4 5 6 7 8 9 [ 1 «2 items elided» ] «2 items elided» ] «1 item elided» ]
at /pwd/lang/eval-fail-print-limit-list.nix:1:1:
1| assert (
| ^
2| let x = [ 1 [ 2 3 4 5 6 7 8 9 x 10 11 ] 12 ];

View file

@ -0,0 +1,4 @@
assert (
let x = [ 1 [ 2 3 4 5 6 7 8 9 x 10 11 ] 12 ];
in builtins.deepSeq x x
); 1

View file

@ -0,0 +1,12 @@
error:
… in the condition of the assert statement
at /pwd/lang/eval-fail-print-limit-set.nix:1:1:
1| assert (
| ^
2| let x = { a.a.a.a.a.a.a.a.a = { a.a = 1; b = 2; }; a.b.c.x = 3; c = 4; };
error: expected a Boolean but found a set: { a = { a = { a = { a = { a = { a = { a = { a = { a = { a = { ... }; b = 2; }; }; }; }; }; }; }; }; «1 attribute elided» }; «1 attribute elided» }
at /pwd/lang/eval-fail-print-limit-set.nix:1:1:
1| assert (
| ^
2| let x = { a.a.a.a.a.a.a.a.a = { a.a = 1; b = 2; }; a.b.c.x = 3; c = 4; };

View file

@ -0,0 +1,4 @@
assert (
let x = { a.a.a.a.a.a.a.a.a = { a.a = 1; b = 2; }; a.b.c.x = 3; c = 4; };
in builtins.deepSeq x x
); 1