Refactor: Improve type propagation in optimizer

Refactor the `updated_metrics` function to `refreshed_metrics`. This
function now clones the `NodeMetrics` if the new type is identical to
the original, avoiding unnecessary allocations.

Also, adjust the `Block` and `Lambda` node kinds to correctly propagate
concrete types after inlining, especially when dealing with polymorphic
functions. This ensures that the return types of blocks and lambdas
reflect the actual computed types rather than stale type variables.

This change improves type inference accuracy and prepares for more
advanced optimizations by ensuring type information is correctly
propagated through the AST.
This commit is contained in:
2026-03-29 18:50:05 +02:00
parent 03862d50c6
commit 6c8e2df45c
2 changed files with 49 additions and 12 deletions
+28
View File
@@ -249,3 +249,31 @@ fn test_inlined_tuple_has_concrete_type_heterogeneous() {
"Inlined tuple must not contain unresolved TypeVars. Dump:\n{dump}"
);
}
#[test]
fn test_block_and_lambda_propagate_concrete_type_after_inlining() {
// When a polymorphic function is inlined and the result tuple gets a concrete type,
// the enclosing Block and outer Lambda return type must propagate that concrete type
// upward — not retain stale TypeVars from before inlining.
let env = Environment::new();
let source = r#"(do
(def last (fn [s] (s 0)))
(def f (fn [n] n))
(def r (series 5))
(push r 4)
[(last f) (last r)]
)"#;
let dump = env.dump_ast_compact(source).unwrap();
// The outer Lambda and Block must carry concrete types, not stale TypeVars.
// Inner polymorphic lambdas (like `last`) legitimately retain TypeVars.
let first_line = dump.lines().next().unwrap();
assert!(
!first_line.contains('?'),
"Outer Lambda return type must be concrete. First line:\n{first_line}"
);
let block_line = dump.lines().find(|l| l.contains("Block")).unwrap();
assert!(
!block_line.contains('?'),
"Block type must be concrete. Line:\n{block_line}"
);
}