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:
@@ -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;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::ast::compiler::call_hooks::{CallHooks, IndexElementFn, TypeInferenceAccess, TypeSlotAccess};
|
||||
use crate::ast::compiler::call_hooks::{CallHooks, InferenceAccess, IndexElementFn};
|
||||
use crate::ast::nodes::{
|
||||
Address, AssignBinding, BoundLike, BoundPhase, DefBinding, IdentifierBinding, LambdaBinding,
|
||||
Node, NodeKind, TypedNode, TypedPhase, VirtualId,
|
||||
@@ -75,39 +75,41 @@ impl<'a> TypeContext<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeSlotAccess for TypeContext<'_> {
|
||||
fn get_slot_type(&self, addr: Address<VirtualId>) -> StaticType {
|
||||
self.get_type(addr)
|
||||
}
|
||||
|
||||
fn set_slot_type(&mut self, addr: Address<VirtualId>, ty: StaticType) {
|
||||
self.set_type(addr, ty);
|
||||
}
|
||||
/// Temporary wrapper that gives call-hooks unified access to both the
|
||||
/// type-checker's inference state and the current scope's slot types.
|
||||
/// Created on the stack at each hook dispatch site; zero allocation cost.
|
||||
struct CheckerInferenceAccess<'a, 'b> {
|
||||
checker: &'a TypeChecker,
|
||||
ctx: &'b TypeContext<'a>,
|
||||
}
|
||||
|
||||
impl TypeInferenceAccess for TypeChecker {
|
||||
impl InferenceAccess for CheckerInferenceAccess<'_, '_> {
|
||||
fn fresh_var(&self) -> StaticType {
|
||||
self.fresh_var()
|
||||
self.checker.fresh_var()
|
||||
}
|
||||
|
||||
fn unify(&self, a: StaticType, b: StaticType, diag: &mut Diagnostics) {
|
||||
self.unify(a, b, diag);
|
||||
self.checker.unify(a, b, diag);
|
||||
}
|
||||
|
||||
fn bind_typevar(&self, id: u32, ty: StaticType) {
|
||||
self.bind_var(id, ty);
|
||||
self.checker.bind_var(id, ty);
|
||||
}
|
||||
|
||||
fn apply_subst_ty(&self, ty: StaticType) -> StaticType {
|
||||
Self::apply_subst(ty, &self.subst.borrow())
|
||||
TypeChecker::apply_subst(ty, &self.checker.subst.borrow())
|
||||
}
|
||||
|
||||
fn try_numeric_widen(&self, a: &StaticType, b: &StaticType) -> Option<StaticType> {
|
||||
Self::numeric_widen(a, b)
|
||||
TypeChecker::numeric_widen(a, b)
|
||||
}
|
||||
|
||||
fn try_record_promote(&self, a: &StaticType, b: &StaticType) -> Option<StaticType> {
|
||||
Self::record_promote(a, b)
|
||||
TypeChecker::record_promote(a, b)
|
||||
}
|
||||
|
||||
fn get_slot_type(&self, addr: Address<VirtualId>) -> StaticType {
|
||||
self.ctx.get_type(addr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1473,7 +1475,8 @@ impl TypeChecker {
|
||||
} = &callee_typed.kind
|
||||
&& let Some(hook) = self.call_hooks.get(&idx.0)
|
||||
&& let Some(post_call) = hook.post_call {
|
||||
ret_ty = post_call(&args_typed, ret_ty, self, ctx, diag);
|
||||
let hook_ctx = CheckerInferenceAccess { checker: self, ctx };
|
||||
ret_ty = post_call(&args_typed, ret_ty, &hook_ctx, diag);
|
||||
}
|
||||
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user