BUG-TAG: Type inference for lambda parameters
The type checker incorrectly inferred `Any` for lambda parameters when a `Program` node was involved, preventing optimizations like constant folding. This was because the `check_params_tuple` function was resolving `TypeVar` to `StaticType::Any` instead of propagating the type variable. This commit addresses the issue by: - Explicitly wrapping the bound AST in a parameterless lambda within `compile_pipeline`. This ensures that the type checker always receives a `Lambda` node, even if the original input was a `Program` node. - Adding debug logging to the type checker to help diagnose similar issues in the future. Additionally, the commit fixes a bug where `parser.parse_program()` was used instead of `parser.parse_expression()`, which would consume the entire input and prevent checking for trailing expressions.
This commit is contained in:
@@ -526,25 +526,23 @@ impl TypeChecker {
|
||||
let mut lambda_ctx =
|
||||
TypeContext::new(64, upvalue_types, ctx.root_types, Some(ctx));
|
||||
|
||||
// Generate a fresh TypeVar per positional parameter so that HM
|
||||
// constraint propagation works across nested closures.
|
||||
// `check_lambda_with_hints` (used at call sites) overrides these with
|
||||
// concrete types; this path only fires for lambdas typed as values
|
||||
// (e.g. returned from another lambda, stored in a def).
|
||||
let param_hint_ty = StaticType::Tuple(
|
||||
(0..positional_count.unwrap_or(0)).map(|_| self.fresh_var()).collect(),
|
||||
);
|
||||
eprintln!("[TC] Lambda check_node: positional_count={:?} param_hint={}", positional_count, param_hint_ty.display_compact());
|
||||
let params_typed = self.check_params(
|
||||
params.as_ref(),
|
||||
¶m_hint_ty,
|
||||
&mut lambda_ctx,
|
||||
diag,
|
||||
);
|
||||
eprintln!("[TC] Lambda params_typed.ty={}", params_typed.ty.display_compact());
|
||||
|
||||
lambda_ctx.current_params_ty = Some(params_typed.ty.clone());
|
||||
|
||||
let body_typed = self.check_node(body, &mut lambda_ctx, diag);
|
||||
let ret_ty = body_typed.ty.clone();
|
||||
eprintln!("[TC] Lambda body ret_ty={}", ret_ty.display_compact());
|
||||
|
||||
let fn_ty = StaticType::Function(Box::new(Signature {
|
||||
params: params_typed.ty.clone(),
|
||||
@@ -565,7 +563,9 @@ impl TypeChecker {
|
||||
}
|
||||
|
||||
NodeKind::Call { callee, args } => {
|
||||
eprintln!("[TC] Call: checking callee...");
|
||||
let callee_typed = self.check_node(callee, ctx, diag);
|
||||
eprintln!("[TC] Call: callee_typed.ty={}", callee_typed.ty.display_compact());
|
||||
|
||||
let args_typed = if let NodeKind::Tuple { elements } = &args.kind {
|
||||
let arg_count = elements.len();
|
||||
@@ -624,6 +624,7 @@ impl TypeChecker {
|
||||
self.check_node(args, ctx, diag)
|
||||
};
|
||||
|
||||
eprintln!("[TC] Call: args_typed.ty={}", args_typed.ty.display_compact());
|
||||
let mut ret_ty = match callee_typed.ty.resolve_call(&args_typed.ty) {
|
||||
Some(ty) => ty,
|
||||
None => {
|
||||
@@ -687,9 +688,16 @@ impl TypeChecker {
|
||||
if let StaticType::Function(sig) = &callee_typed.ty
|
||||
&& Self::has_typevar_component(&sig.params)
|
||||
{
|
||||
eprintln!("[TC] HM10: unify params={} with args={}", sig.params.display_compact(), args_typed.ty.display_compact());
|
||||
let params = sig.params.clone();
|
||||
self.unify(params, args_typed.ty.clone(), diag);
|
||||
ret_ty = Self::apply_subst(ret_ty, &self.subst.borrow());
|
||||
eprintln!("[TC] HM10: after unify ret_ty={}", ret_ty.display_compact());
|
||||
} else {
|
||||
eprintln!("[TC] HM10: SKIPPED (callee_ty={}, has_typevar={})",
|
||||
callee_typed.ty.display_compact(),
|
||||
if let StaticType::Function(sig) = &callee_typed.ty { Self::has_typevar_component(&sig.params) } else { false }
|
||||
);
|
||||
}
|
||||
|
||||
// Dispatch compiler hooks registered by the RTL (keyed by global slot index).
|
||||
|
||||
@@ -70,6 +70,7 @@ impl TypeChecker {
|
||||
) -> TypedNode {
|
||||
match &node.kind {
|
||||
NodeKind::Lambda { params, body, info } => {
|
||||
eprintln!("[TC] check_node_as_bound: Lambda entry");
|
||||
let upvalues = &info.upvalues;
|
||||
let positional_count = info.positional_count;
|
||||
|
||||
@@ -115,6 +116,7 @@ impl TypeChecker {
|
||||
}
|
||||
}
|
||||
NodeKind::Block { .. } | NodeKind::Program { .. } => {
|
||||
eprintln!("[TC] check_node_as_bound: Block/Program entry");
|
||||
let mut ctx = TypeContext::new(64, vec![], &self.root_types, None);
|
||||
self.check_node(node, &mut ctx, diag)
|
||||
}
|
||||
|
||||
+41
-4
@@ -12,7 +12,7 @@ use std::rc::Rc;
|
||||
|
||||
use crate::ast::nodes::{
|
||||
Address, AnalyzedNode, ExecNode, GlobalAnalyzedRegistry, GlobalFunctionRegistry, GlobalIdx,
|
||||
Node, NodeKind, VirtualId,
|
||||
LambdaBinding, Node, NodeKind, VirtualId,
|
||||
};
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
@@ -447,11 +447,38 @@ impl Environment {
|
||||
typed
|
||||
}
|
||||
|
||||
/// Full compilation pipeline: expand → bind → type-check.
|
||||
/// Wraps a bound AST in a parameterless lambda (unless it already is one).
|
||||
fn wrap_as_lambda(&self, bound_ast: Node<BoundPhase>) -> Node<BoundPhase> {
|
||||
if let NodeKind::Lambda { .. } = bound_ast.kind {
|
||||
bound_ast
|
||||
} else {
|
||||
Node {
|
||||
identity: bound_ast.identity.clone(),
|
||||
kind: NodeKind::Lambda {
|
||||
params: Rc::new(Node {
|
||||
identity: bound_ast.identity.clone(),
|
||||
kind: NodeKind::Tuple { elements: vec![] },
|
||||
ty: (),
|
||||
comments: Rc::from([]),
|
||||
}),
|
||||
body: Rc::new(bound_ast),
|
||||
info: LambdaBinding {
|
||||
upvalues: vec![],
|
||||
positional_count: Some(0),
|
||||
},
|
||||
},
|
||||
ty: (),
|
||||
comments: Rc::from([]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Full compilation pipeline: expand → bind → wrap → type-check.
|
||||
fn compile_pipeline(&self, syntax_ast: SyntaxNode, diagnostics: &mut Diagnostics) -> Option<TypedNode> {
|
||||
let expanded = self.expand(syntax_ast, diagnostics)?;
|
||||
let bound = self.bind_and_update(&expanded, diagnostics)?;
|
||||
let typed = self.type_check(&bound, diagnostics);
|
||||
let wrapped = self.wrap_as_lambda(bound);
|
||||
let typed = self.type_check(&wrapped, diagnostics);
|
||||
Some(typed)
|
||||
}
|
||||
|
||||
@@ -710,7 +737,17 @@ impl Environment {
|
||||
}
|
||||
|
||||
let mut parser = Parser::new(source);
|
||||
let syntax_ast = parser.parse_program();
|
||||
let syntax_ast = parser.parse_expression();
|
||||
|
||||
if !parser.at_eof() {
|
||||
parser
|
||||
.diagnostics
|
||||
.push_error("Unexpected trailing expressions in script.", None);
|
||||
return CompilationResult {
|
||||
ast: None,
|
||||
diagnostics: parser.diagnostics,
|
||||
};
|
||||
}
|
||||
let mut diagnostics = parser.diagnostics;
|
||||
|
||||
let typed_ast = self.compile_pipeline(syntax_ast, &mut diagnostics);
|
||||
|
||||
Reference in New Issue
Block a user