Update record type computation
This commit refactors the way record types are computed within the optimizer. Previously, the optimizer would often reuse the existing `NodeMetrics` for records, which could lead to stale or polymorphic types. The changes introduce a new function, `compute_record_type`, which explicitly recalculates the `StaticType` for a `Record` node based on the concrete types of its child fields. This ensures that after optimizations like inlining and folding, record types are as concrete as possible, eliminating unresolved `TypeVars`. Additionally, the `folder.rs` module is updated to recompute the `RecordLayout` when creating constant record values, further improving type accuracy. Two new tests are added to verify that inlined records, including those with multiple fields, correctly result in concrete types.
This commit is contained in:
@@ -277,3 +277,36 @@ fn test_block_and_lambda_propagate_concrete_type_after_inlining() {
|
||||
"Block type must be concrete. Line:\n{block_line}"
|
||||
);
|
||||
}
|
||||
|
||||
// ── Record type recomputation after inlining ────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_inlined_record_has_concrete_type() {
|
||||
// A polymorphic function returning a record, called with a concrete arg.
|
||||
// After inlining + folding, the record type must be concrete (no TypeVars).
|
||||
let env = Environment::new();
|
||||
let source = r#"(do
|
||||
(def wrap (fn [x] {:val x}))
|
||||
(wrap 42)
|
||||
)"#;
|
||||
let dump = env.dump_ast_compact(source).unwrap();
|
||||
assert!(
|
||||
!dump.contains('?'),
|
||||
"Inlined record must not contain unresolved TypeVars. Dump:\n{dump}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inlined_record_has_concrete_type_multi_field() {
|
||||
// Heterogeneous case: Int + String fields should produce concrete record type.
|
||||
let env = Environment::new();
|
||||
let source = r#"(do
|
||||
(def mkr (fn [a b] {:x a :y b}))
|
||||
(mkr 1 "hello")
|
||||
)"#;
|
||||
let dump = env.dump_ast_compact(source).unwrap();
|
||||
assert!(
|
||||
!dump.contains('?'),
|
||||
"Inlined multi-field record must not contain unresolved TypeVars. Dump:\n{dump}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user