Skip to content

Commit

Permalink
Inference: Handle arrays with key types
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Jan 10, 2025
1 parent a1efd70 commit de93b6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
15 changes: 9 additions & 6 deletions crates/inference/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,22 @@ impl<'a> TypeMapGenerator<'a> {
);
}

todo!();

let key_types: Vec<Type<ResolvedName>> = node
.items
.iter()
.filter_map(|item| -> Option<Type<ResolvedName>> {
.map(|item| -> Type<ResolvedName> {
match item {
ArrayItem::KeyValue(array_item_key_value) => todo!(),
ArrayItem::ReferencedKeyValue(array_item_referenced_key_value) => todo!(),
_ => None,
ArrayItem::KeyValue(array_item_key_value) => self.map.resolve(array_item_key_value.key.id).clone(),
ArrayItem::ReferencedKeyValue(array_item_referenced_key_value) => self.map.resolve(array_item_referenced_key_value.key.id).clone(),
_ => Type::Integer,
}
})
.collect();

Type::TypedArray(
Box::new(self.simplify_union(key_types)),
Box::new(self.simplify_union(value_types)),
)
}
}

Expand Down
22 changes: 22 additions & 0 deletions crates/inference/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ mod tests {
);
}

#[test]
fn it_infers_type_of_keyed_array() {
assert_eq!(
infer(r#"$a = ['a' => 1, 'b' => 2]"#),
Type::TypedArray(
Box::new(Type::String),
Box::new(Type::Integer),
)
)
}

#[test]
fn it_infers_type_of_mixed_keyed_array() {
assert_eq!(
infer(r#"$a = ['a' => 1, 2]"#),
Type::TypedArray(
Box::new(Type::array_key_types()),
Box::new(Type::Integer),
),
)
}

#[test]
fn it_infers_type_of_new_expression() {
let inferred = infer(
Expand Down

0 comments on commit de93b6e

Please sign in to comment.