Refactor type inference for collections

The type inference for collections (tuples, vectors, matrices, and
records) has been significantly refactored.

This includes:
- Introducing distinct `StaticType` variants for `Tuple`, `Vector`, and
  `Matrix`.
- Implementing more robust type checking for these collections, allowing
  for homogeneous and heterogeneous structures.
- Enhancing the `is_assignable_from` method to handle type compatibility
  between these collection types.
- Updating `Value::static_type()` to correctly infer the types of
  literal collections.
- Improving the type inference for map literals to create `Record`
  types.
- Adding comprehensive unit tests for tuple, vector, matrix, and record
  type inference.
This commit is contained in:
Michael Schimmel
2026-02-20 10:19:29 +01:00
parent 494bf554d2
commit 4f849ebd34
12 changed files with 225 additions and 50 deletions
+25 -31
View File
@@ -467,13 +467,12 @@ mod tests {
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind {
if let UntypedKind::Expansion { expanded: result, call } = &exprs[1].kind {
if let UntypedKind::Def { name, .. } = &result.kind {
assert_eq!(name.context, Some(call.identity.clone()));
assert_eq!(name.name.as_ref(), "y");
} else { panic!("Expected Def result, got {:?}", result.kind); }
}
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { expanded: result, call } = &exprs[1].kind {
if let UntypedKind::Def { name, .. } = &result.kind {
assert_eq!(name.context, Some(call.identity.clone()));
assert_eq!(name.name.as_ref(), "y");
} else { panic!("Expected Def result, got {:?}", result.kind); }
}
}
@@ -490,20 +489,18 @@ mod tests {
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind {
if let UntypedKind::Expansion { call: _, expanded: result } = &exprs[1].kind {
if let UntypedKind::If { cond, then_br, else_br } = &result.kind {
if let UntypedKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false");
} else { panic!("Expected identifier 'false', got {:?}", cond.kind); }
assert!(matches!(then_br.kind, UntypedKind::Nop));
if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42);
} else { panic!("Expected 42, got {:?}", eb.kind); }
} else { panic!("Else branch missing"); }
}
}
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Expansion { call: _, expanded: result } = &exprs[1].kind
&& let UntypedKind::If { cond, then_br, else_br } = &result.kind {
if let UntypedKind::Identifier(sym) = &cond.kind {
assert_eq!(sym.name.as_ref(), "false");
} else { panic!("Expected identifier 'false', got {:?}", cond.kind); }
assert!(matches!(then_br.kind, UntypedKind::Nop));
if let Some(eb) = else_br {
if let UntypedKind::Constant(Value::Int(n)) = &eb.kind {
assert_eq!(*n, 42);
} else { panic!("Expected 42, got {:?}", eb.kind); }
} else { panic!("Else branch missing"); }
}
}
@@ -520,16 +517,13 @@ mod tests {
let mut expander = MacroExpander::new(MacroRegistry::new(), SimpleEvaluator);
let expanded = expander.expand(untyped).unwrap();
if let UntypedKind::Block { exprs } = &expanded.kind {
if let UntypedKind::Def { value, .. } = &exprs[1].kind {
if let UntypedKind::Expansion { expanded: result, .. } = &value.kind {
if let UntypedKind::Tuple { elements } = &result.kind {
assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind { assert_eq!(*n, 0); }
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind { assert_eq!(*n, 4); }
}
}
}
if let UntypedKind::Block { exprs } = &expanded.kind
&& let UntypedKind::Def { value, .. } = &exprs[1].kind
&& let UntypedKind::Expansion { expanded: result, .. } = &value.kind
&& let UntypedKind::Tuple { elements } = &result.kind {
assert_eq!(elements.len(), 5);
if let UntypedKind::Constant(Value::Int(n)) = &elements[0].kind { assert_eq!(*n, 0); }
if let UntypedKind::Constant(Value::Int(n)) = &elements[4].kind { assert_eq!(*n, 4); }
}
}