Refactor InferenceAccess for call hooks

The `TypeInferenceAccess` and `TypeSlotAccess` traits have been
consolidated into a single `InferenceAccess` trait. This simplifies the
call hook interface by providing a unified way to access type inference
operations and scope slot types.

A new struct, `CheckerInferenceAccess`, is introduced to provide a
concrete implementation of `InferenceAccess` for the `TypeChecker`. This
struct wraps both the `TypeChecker` and the `TypeContext`, allowing call
hooks to interact with both without needing to know their specific
types. This avoids potential circular dependencies between modules.

The `get_slot_type` method has been moved to the new `InferenceAccess`
trait. This method is crucial for call hooks that need to recover the
original `TypeVar` ID after substitutions have occurred, particularly in
the `push_post_call` function for series.
This commit is contained in:
2026-03-29 13:55:32 +02:00
parent 93a85cd15c
commit 0292ead7a5
3 changed files with 41 additions and 43 deletions
+10 -12
View File
@@ -5,16 +5,10 @@ use crate::ast::diagnostics::Diagnostics;
use crate::ast::nodes::{Address, NodeKind, TypedNode, TypedPhase, VirtualId};
use crate::ast::types::StaticType;
/// Gives a call-hook read/write access to the current scope's slot types without
/// exposing `TypeContext` internals.
pub trait TypeSlotAccess {
fn get_slot_type(&self, addr: Address<VirtualId>) -> StaticType;
fn set_slot_type(&mut self, addr: Address<VirtualId>, ty: StaticType);
}
/// Gives a call-hook access to the type-checker's core inference operations without
/// depending on the concrete `TypeChecker` type (avoids a circular module dependency).
pub trait TypeInferenceAccess {
/// Gives a call-hook access to the type-checker's core inference operations and
/// scope slot types without depending on the concrete `TypeChecker` type
/// (avoids a circular module dependency).
pub trait InferenceAccess {
/// Allocate a fresh unique `TypeVar`.
fn fresh_var(&self) -> StaticType;
@@ -34,6 +28,11 @@ pub trait TypeInferenceAccess {
/// Record-field promotion rule. Returns the promoted record type when one record
/// is a strict widening of the other, or `None` otherwise.
fn try_record_promote(&self, a: &StaticType, b: &StaticType) -> Option<StaticType>;
/// Read the raw (unsubstituted) type stored in a scope slot.
/// Used by hooks that need to recover the original `TypeVar` ID after
/// the substitution has already resolved it to a concrete type.
fn get_slot_type(&self, addr: Address<VirtualId>) -> StaticType;
}
/// Maps a concrete type to its element type for index-constraint propagation.
@@ -51,8 +50,7 @@ pub type IndexElementFn = fn(&StaticType) -> Option<StaticType>;
pub type PostCallHook = fn(
args: &TypedNode,
ret_ty: StaticType,
inference: &dyn TypeInferenceAccess,
ctx: &mut dyn TypeSlotAccess,
ctx: &dyn InferenceAccess,
diag: &mut Diagnostics,
) -> StaticType;