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 <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-09-13 16:04:52 +03:00 committed by tazjin
parent 268605140e
commit c28ecbee97
3 changed files with 20 additions and 15 deletions

View file

@ -39,7 +39,7 @@ impl Compiler<'_, '_> {
// Open a scope to track the positions of the temporaries used // Open a scope to track the positions of the temporaries used
// by the `OpAttrs` instruction. // by the `OpAttrs` instruction.
self.begin_scope(); self.scope_mut().begin_scope();
let mut count = self.compile_inherit_attrs(slot, node.inherits()); let mut count = self.compile_inherit_attrs(slot, node.inherits());
@ -69,7 +69,7 @@ impl Compiler<'_, '_> {
let fragment_slot = match key_count { let fragment_slot = match key_count {
0 => key_idx, 0 => key_idx,
1 => { 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)
} }
_ => self.scope_mut().declare_phantom(key_span, false), _ => self.scope_mut().declare_phantom(key_span, false),

View file

@ -427,7 +427,7 @@ impl Compiler<'_, '_> {
// Open a temporary scope to correctly account for stack items // Open a temporary scope to correctly account for stack items
// that exist during the construction. // that exist during the construction.
self.begin_scope(); self.scope_mut().begin_scope();
for item in node.items() { for item in node.items() {
// Start tracing new stack slots from the second list // 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 /// simply pushed on the stack and their indices noted in the
/// entries vector. /// entries vector.
fn compile_let_in(&mut self, slot: LocalIdx, node: ast::LetIn) { 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). // 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 // 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 /// pop/remove the indices of attribute sets that are implicitly
/// in scope through `with` on the "with-stack". /// in scope through `with` on the "with-stack".
fn compile_with(&mut self, slot: LocalIdx, node: ast::With) { 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 // TODO: Detect if the namespace is just an identifier, and
// resolve that directly (thus avoiding duplication on the // resolve that directly (thus avoiding duplication on the
// stack). // stack).
@ -867,7 +867,7 @@ impl Compiler<'_, '_> {
self.new_context(); self.new_context();
let span = self.span_for(&node); let span = self.span_for(&node);
let slot = self.scope_mut().declare_phantom(span, false); let slot = self.scope_mut().declare_phantom(span, false);
self.begin_scope(); self.scope_mut().begin_scope();
// Compile the function itself // Compile the function itself
match node.param().unwrap() { match node.param().unwrap() {
@ -952,7 +952,7 @@ impl Compiler<'_, '_> {
self.new_context(); self.new_context();
let span = self.span_for(node); let span = self.span_for(node);
let slot = self.scope_mut().declare_phantom(span, false); let slot = self.scope_mut().declare_phantom(span, false);
self.begin_scope(); self.scope_mut().begin_scope();
content(self, node, slot); content(self, node, slot);
self.cleanup_scope(node); 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 /// Decrease scope depth of the current function and emit
/// instructions to clean up the stack at runtime. /// instructions to clean up the stack at runtime.
fn cleanup_scope<N: AstNode>(&mut self, node: &N) { fn cleanup_scope<N: AstNode>(&mut self, node: &N) {
@ -1097,7 +1091,7 @@ impl Compiler<'_, '_> {
/// determine the stack offset of variables. /// determine the stack offset of variables.
fn declare_local<S: Into<String>, N: AstNode>(&mut self, node: &N, name: S) -> LocalIdx { fn declare_local<S: Into<String>, N: AstNode>(&mut self, node: &N, name: S) -> LocalIdx {
let name = name.into(); 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 // Do this little dance to get ahold of the *static* key and
// use it for poisoning if required. // use it for poisoning if required.

View file

@ -123,7 +123,7 @@ pub struct Scope {
pub upvalues: Vec<Upvalue>, pub upvalues: Vec<Upvalue>,
/// How many scopes "deep" are these locals? /// How many scopes "deep" are these locals?
pub scope_depth: usize, scope_depth: usize,
/// Current size of the `with`-stack at runtime. /// Current size of the `with`-stack at runtime.
with_stack_size: usize, with_stack_size: usize,
@ -272,6 +272,12 @@ impl Scope {
StackIdx(idx.0 - uninitialised_count) 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 /// Decrease the scope depth and remove all locals still tracked
/// for the current scope. /// for the current scope.
/// ///
@ -314,4 +320,9 @@ impl Scope {
(pops, unused_spans) (pops, unused_spans)
} }
/// Access the current scope depth.
pub fn scope_depth(&self) -> usize {
self.scope_depth
}
} }