From 9cd5f03835d9fcc5b3c8fb0173c4a46c10519bc5 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 29 Sep 2022 18:07:50 +0300 Subject: refactor(tvix/eval): split out AttributeSet::from_ast helper Change-Id: Id43dbd06aef14cf01b4901d9b3668d790cd2b5ae Reviewed-on: https://cl.tvl.fyi/c/depot/+/6805 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/compiler/bindings.rs | 57 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/compiler/bindings.rs b/tvix/eval/src/compiler/bindings.rs index 0f12511df12c..7bbf41b205e9 100644 --- a/tvix/eval/src/compiler/bindings.rs +++ b/tvix/eval/src/compiler/bindings.rs @@ -51,6 +51,36 @@ impl ToSpan for AttributeSet { } } +impl AttributeSet { + fn from_ast(c: &Compiler, node: &ast::AttrSet) -> Self { + AttributeSet { + span: c.span_for(node), + + // Kind of the attrs depends on the first time it is + // encountered. We actually believe this to be a Nix + // bug: https://github.com/NixOS/nix/issues/7111 + kind: if node.rec_token().is_some() { + BindingsKind::RecAttrs + } else { + BindingsKind::Attrs + }, + + inherits: ast::HasEntry::inherits(node).collect(), + + entries: ast::HasEntry::attrpath_values(node) + .map(|entry| { + let span = c.span_for(&entry); + ( + span, + entry.attrpath().unwrap().attrs(), + entry.value().unwrap(), + ) + }) + .collect(), + } + } +} + // Data structures to track the bindings observed in the second pass, and // forward the information needed to compile their value. enum Binding { @@ -78,32 +108,7 @@ impl Binding { // and recurse. Binding::Plain { expr } => match expr { ast::Expr::AttrSet(existing) => { - let nested = AttributeSet { - span: c.span_for(existing), - - // Kind of the attrs depends on the first time it is - // encountered. We actually believe this to be a Nix - // bug: https://github.com/NixOS/nix/issues/7111 - kind: if existing.rec_token().is_some() { - BindingsKind::RecAttrs - } else { - BindingsKind::Attrs - }, - - inherits: ast::HasEntry::inherits(existing).collect(), - - entries: ast::HasEntry::attrpath_values(existing) - .map(|entry| { - let span = c.span_for(&entry); - ( - span, - entry.attrpath().unwrap().attrs(), - entry.value().unwrap(), - ) - }) - .collect(), - }; - + let nested = AttributeSet::from_ast(c, existing); *self = Binding::Set(nested); self.merge(c, value); } -- cgit 1.4.1