Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/types/checker/stmt_check/assignments/locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,29 @@ pub(super) fn check_list_unpack(
update_list_unpack_callable_metadata(checker, var, value, &unpack_ty);
}
}
// An associative-array RHS, or a union that carries an array-like member
// (a null-narrowed `?array` return leaves `Union[Array, Void]`; a Mixed
// union), is an array at runtime in well-typed code. Its elements bind as
// Mixed (adaptive access), the same trust posture PHP applies at runtime.
PhpType::AssocArray { .. } => {
for var in vars {
env.insert(var.clone(), PhpType::Mixed);
update_list_unpack_callable_metadata(checker, var, value, &PhpType::Mixed);
}
}
PhpType::Union(members)
if members.iter().any(|member| {
matches!(
member.codegen_repr(),
PhpType::Array(_) | PhpType::AssocArray { .. } | PhpType::Mixed
)
}) =>
{
for var in vars {
env.insert(var.clone(), PhpType::Mixed);
update_list_unpack_callable_metadata(checker, var, value, &PhpType::Mixed);
}
}
_ => {
return Err(CompileError::new(
span,
Expand Down
29 changes: 29 additions & 0 deletions tests/codegen/array_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,32 @@ fn test_in_array_strict_distinguishes_bool_int_membership() {
);
assert_eq!(out, "101010");
}

/// List/array destructuring accepts a union RHS that carries an array member: a `?array`
/// method return, narrowed by `=== null`, still types as `Union[Array, Void]` at the
/// destructure, and its elements bind as Mixed (adaptive). Byte-parity vs PHP 8.5.
#[test]
fn test_list_unpack_null_narrowed_array_return() {
let out = compile_and_run(
r#"<?php
final class R {
private function mk(int $n): ?array {
if ($n < 0) { return null; }
return ["k" . $n, "v" . $n];
}
public function run(): string {
$out = "";
foreach ([1, -1, 2] as $n) {
$entry = $this->mk($n);
if ($entry === null) { continue; }
[$key, $value] = $entry;
$out .= $key . "=" . $value . ";";
}
return $out;
}
}
echo (new R())->run();
"#,
);
assert_eq!(out, "k1=v1;k2=v2;");
}
Loading