From c28ecbee970c0a16b7f93241a5a2ef2df49406c6 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 13 Sep 2022 16:04:52 +0300 Subject: refactor(tvix/eval): encapsulate scope_depth in compiler::scope This field no longer needs to be directly accessible by the compiler. Addresses a sterni lint from cl/6466 Change-Id: I5e6791943d7f0ab3d9b7a30bb1654c4a6a435b1f Reviewed-on: https://cl.tvl.fyi/c/depot/+/6564 Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/compiler/attrs.rs | 4 ++-- tvix/eval/src/compiler/mod.rs | 18 ++++++------------ tvix/eval/src/compiler/scope.rs | 13 ++++++++++++- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/tvix/eval/src/compiler/attrs.rs b/tvix/eval/src/compiler/attrs.rs index 91039d6dbfbe..649cdf37f00a 100644 --- a/tvix/eval/src/compiler/attrs.rs +++ b/tvix/eval/src/compiler/attrs.rs @@ -39,7 +39,7 @@ impl Compiler<'_, '_> { // Open a scope to track the positions of the temporaries used // by the `OpAttrs` instruction. - self.begin_scope(); + self.scope_mut().begin_scope(); let mut count = self.compile_inherit_attrs(slot, node.inherits()); @@ -69,7 +69,7 @@ impl Compiler<'_, '_> { let fragment_slot = match key_count { 0 => key_idx, 1 => { - self.begin_scope(); + self.scope_mut().begin_scope(); self.scope_mut().declare_phantom(key_span, false) } _ => self.scope_mut().declare_phantom(key_span, false), diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 440ac284c1a6..bcf4abacd776 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -427,7 +427,7 @@ impl Compiler<'_, '_> { // Open a temporary scope to correctly account for stack items // that exist during the construction. - self.begin_scope(); + self.scope_mut().begin_scope(); for item in node.items() { // Start tracing new stack slots from the second list @@ -562,7 +562,7 @@ impl Compiler<'_, '_> { /// simply pushed on the stack and their indices noted in the /// entries vector. fn compile_let_in(&mut self, slot: LocalIdx, node: ast::LetIn) { - self.begin_scope(); + self.scope_mut().begin_scope(); // First pass to find all plain inherits (if they are not useless). // Since they always resolve to a higher scope, we can just compile and @@ -741,7 +741,7 @@ impl Compiler<'_, '_> { /// pop/remove the indices of attribute sets that are implicitly /// in scope through `with` on the "with-stack". fn compile_with(&mut self, slot: LocalIdx, node: ast::With) { - self.begin_scope(); + self.scope_mut().begin_scope(); // TODO: Detect if the namespace is just an identifier, and // resolve that directly (thus avoiding duplication on the // stack). @@ -867,7 +867,7 @@ impl Compiler<'_, '_> { self.new_context(); let span = self.span_for(&node); let slot = self.scope_mut().declare_phantom(span, false); - self.begin_scope(); + self.scope_mut().begin_scope(); // Compile the function itself match node.param().unwrap() { @@ -952,7 +952,7 @@ impl Compiler<'_, '_> { self.new_context(); let span = self.span_for(node); let slot = self.scope_mut().declare_phantom(span, false); - self.begin_scope(); + self.scope_mut().begin_scope(); content(self, node, slot); self.cleanup_scope(node); @@ -1060,12 +1060,6 @@ impl Compiler<'_, '_> { } } - /// Increase the scope depth of the current function (e.g. within - /// a new bindings block, or `with`-scope). - fn begin_scope(&mut self) { - self.scope_mut().scope_depth += 1; - } - /// Decrease scope depth of the current function and emit /// instructions to clean up the stack at runtime. fn cleanup_scope(&mut self, node: &N) { @@ -1097,7 +1091,7 @@ impl Compiler<'_, '_> { /// determine the stack offset of variables. fn declare_local, N: AstNode>(&mut self, node: &N, name: S) -> LocalIdx { let name = name.into(); - let depth = self.scope().scope_depth; + let depth = self.scope().scope_depth(); // Do this little dance to get ahold of the *static* key and // use it for poisoning if required. diff --git a/tvix/eval/src/compiler/scope.rs b/tvix/eval/src/compiler/scope.rs index 1909a8dc8a76..29947ea4a208 100644 --- a/tvix/eval/src/compiler/scope.rs +++ b/tvix/eval/src/compiler/scope.rs @@ -123,7 +123,7 @@ pub struct Scope { pub upvalues: Vec, /// How many scopes "deep" are these locals? - pub scope_depth: usize, + scope_depth: usize, /// Current size of the `with`-stack at runtime. with_stack_size: usize, @@ -272,6 +272,12 @@ impl Scope { StackIdx(idx.0 - uninitialised_count) } + /// Increase the current scope depth (e.g. within a new bindings + /// block, or `with`-scope). + pub fn begin_scope(&mut self) { + self.scope_depth += 1; + } + /// Decrease the scope depth and remove all locals still tracked /// for the current scope. /// @@ -314,4 +320,9 @@ impl Scope { (pops, unused_spans) } + + /// Access the current scope depth. + pub fn scope_depth(&self) -> usize { + self.scope_depth + } } -- cgit 1.4.1