Repository navigation
fix(templates): a template of numeric can be identical to a string - #14430
Conversation
…meo#14054 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change
|
✅ Performance: unchanged · Types: unchanged
How to read this table
head 25d147f · base c5f4dfd (merge-base) · workflow run |
…eeps both branches vimeo#14054 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Corpus check (laravel and symfony sources, this branch vs
Not run: the full PHPUnit suite (CI runs it). |
orklah
left a comment
There was a problem hiding this comment.
Thanks for this. The goal is right, but the new branch in AssertionReconciler::refine() loses precision on the other side of ===.
The intersection replaces the existing atomic, and then count($acceptable_atomic_types) === count($existing_var_type->getAtomicTypes()) still holds, so $existing_var_type is returned unnarrowed. Before this PR the list was empty and the later code narrowed it:
/** @param positive-int $i */
function takesPositiveInt(int $i): void {}
/** @param list<string> $l */
function takesListOfStrings(array $l): void {}
/** @param numeric-string $s */
function takesNumericString(string $s): void {}
/**
* @template T of positive-int
* @param T $t
*/
function e1($t, int $i): void {
if ($i === $t) {
takesPositiveInt($i); // master: no issue; this PR: ArgumentTypeCoercion (T as int)
}
}
/**
* @template T of list<string>
* @param T $t
*/
function e2($t, array $a): void {
if ($a === $t) {
takesListOfStrings($a); // master: no issue; this PR: ArgumentTypeCoercion (T as array<array-key, mixed>)
}
}
/**
* @template T of numeric
* @param T $t
*/
function e3($t, string $s): void {
if ($t === $s) {
takesNumericString($s); // this PR: ArgumentTypeCoercion ($s: T as string)
}
}Tracking the replacement fixes all three (int<1, max>, list<string>, numeric-string, and int|numeric-string for an int|string operand), and your two test cases still pass:
$acceptable_atomic_types = [];
+ $narrowed = false;
...
+ $narrowed = true;
foreach ($overlap->getAtomicTypes() as $overlap_atomic_type) {
...
- count($acceptable_atomic_types) === count($existing_var_type->getAtomicTypes())
+ !$narrowed && count($acceptable_atomic_types) === count($existing_var_type->getAtomicTypes())Could you add e1, e2 and e3 as valid-code cases, plus one for is_string($t) on a T of numeric (the reconcileString() change is only covered through === for now)?
…numeric-identical-string
…ps instead of keeping the type refine() tracks that the overlap replaced the existing atomic, so the unchanged type is not returned just because the number of atomics is the same. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A template bounded by
numericcompared with===to astringis reported as a contradiction and narrowed tonever, although a numeric string is both.Before
After
Fixes #14054