fix(tvix/eval): detect cycles when printing infinite values

Using the same method as in Thunk::deep_force, detect cycles when
printing values by maintaining a set of already seen thunks.

With this, display of infinite values matches that of Nix:

    > nix-instantiate --eval --strict -E 'let as = { x = 123; y = as; }; in as'
    { x = 123; y = { x = 123; y = <CYCLE>; }; }

    > tvix-eval -E 'let as = { x = 123; y = as; }; in as'
    => { x = 123; y = { x = 123; y = <CYCLE>; }; } :: set

Change-Id: I007b918d5131d82c28884e46e46ff365ef691aa8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7056
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-10-22 16:51:29 +03:00 committed by tazjin
parent 4ff06ba67d
commit 60f24c3c53
4 changed files with 45 additions and 24 deletions

View file

@ -21,7 +21,6 @@
use std::{
cell::{Ref, RefCell, RefMut},
collections::HashSet,
fmt::Display,
rc::Rc,
};
@ -35,7 +34,7 @@ use crate::{
Value,
};
use super::Lambda;
use super::{Lambda, TotalDisplay};
/// Internal representation of the different states of a thunk.
///
@ -188,11 +187,15 @@ impl Thunk {
}
}
impl Display for Thunk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl TotalDisplay for Thunk {
fn total_fmt(&self, f: &mut std::fmt::Formatter<'_>, set: &mut ThunkSet) -> std::fmt::Result {
if !set.insert(self) {
return f.write_str("<CYCLE>");
}
match self.0.try_borrow() {
Ok(repr) => match &*repr {
ThunkRepr::Evaluated(v) => v.fmt(f),
ThunkRepr::Evaluated(v) => v.total_fmt(f, set),
_ => f.write_str("internal[thunk]"),
},