From 60f24c3c53eae116bb3051f02f05b74a8ecf37af Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 22 Oct 2022 16:51:29 +0300 Subject: 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 = ; }; } > tvix-eval -E 'let as = { x = 123; y = as; }; in as' => { x = 123; y = { x = 123; y = ; }; } :: set Change-Id: I007b918d5131d82c28884e46e46ff365ef691aa8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7056 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/value/attrs.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'tvix/eval/src/value/attrs.rs') diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index a67fd2f3e3..17c7b422cb 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -7,12 +7,13 @@ //! some peculiarities that are encapsulated within this module. use std::collections::btree_map; use std::collections::BTreeMap; -use std::fmt::Display; use crate::errors::ErrorKind; use crate::vm::VM; use super::string::NixString; +use super::thunk::ThunkSet; +use super::TotalDisplay; use super::Value; #[cfg(test)] @@ -74,19 +75,26 @@ impl AttrsRep { #[derive(Clone, Debug, PartialEq)] pub struct NixAttrs(AttrsRep); -impl Display for NixAttrs { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl TotalDisplay for NixAttrs { + fn total_fmt(&self, f: &mut std::fmt::Formatter<'_>, set: &mut ThunkSet) -> std::fmt::Result { f.write_str("{ ")?; match &self.0 { AttrsRep::KV { name, value } => { - write!(f, "name = {}; ", name)?; - write!(f, "value = {}; ", value)?; + f.write_str("name = ")?; + name.total_fmt(f, set)?; + f.write_str("; ")?; + + f.write_str("value = ")?; + value.total_fmt(f, set)?; + f.write_str("; ")?; } AttrsRep::Map(map) => { for (name, value) in map { - write!(f, "{} = {}; ", name.ident_str(), value)?; + write!(f, "{} = ", name.ident_str())?; + value.total_fmt(f, set)?; + f.write_str("; ")?; } } -- cgit 1.4.1