Repository navigation
fix(try): a variable assigned on some paths of a finally block keeps that type after the try - #14416
Conversation
…that type after the try vimeo#14188 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change
|
✅ Performance: unchanged · Types: decreased
How to read this table
✅ symfony: issues +0 −1Memory 3 600 → 3 600 MB (<0.1 %) · Wall 217.4 → 216.9 s (−0.2 %) Weighted 98 405 → 98 401 (−4, <0.01 %): L4 −1
Removed (1)
head 18f54a5 · base 7bfe967 (merge-base) · workflow run |
…keeps it possibly undefined vimeo#14188 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Behaviour change (update, from the Bugbot finding)
|
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit fb67a99. Configure here.
orklah
left a comment
There was a problem hiding this comment.
Thanks, this fixes both issues. One problem with the type the post-try scope gets for a variable the finally block assigns on some paths only: it is the finally block's end type, which starts from finally_scope, i.e. it includes the state before the try (and every return/throw state). The code after the try is only reached from the normal end of the try or of a continuing catch, so the pre-try type leaks and creates new false positives:
function g(): void {}
function f(): int {
$t = null;
try {
$t = 1;
g();
} finally {
if (rand(0, 1)) {
$t = 2;
}
}
/** @psalm-check-type-exact $t = 1|2 */; // head: 1|2|null
return $t; // head: NullableReturnStatement
}Same with a catch ($s = 'a' in the try, 'b' in the catch, 'c' conditionally in the finally: head gives 'a'|'b'|'c'|null) and with a return null inside the try (head: 0|1|2|null, PossiblyNullOperand).
For these variables the type after the try should be what the finally block leaves when it starts from the post-try state ($context after the catches), not from finally_scope. Could you fix that and add the case above as a valid-code test (it should fail on the current head)?
Optional: a conditional unset($t) in the finally is now reported as PossiblyUndefinedVariable after the try (master reports nothing); a test for it would pin that too.
…onditional-assignment
…type of the post-try state The finally block is analysed once more, silently, from the state after the try and the catches that go on, and the variables it assigns on some paths only take the type that analysis ends with: the end of the first one starts from every state, the state before the try included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed 8294f4e. For a variable the |
…ollected without array_diff_key() Psalm's own analysis took the property it was assigned [] before the analysis for a never-array. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A variable assigned inside a condition, a loop or a nested
tryof afinallyblock keeps the type it had before thetry, as if the assignment never ran.Before
After
The same in a closure writing a variable captured by reference was reported as
TypeDoesNotContainTypeafter the call (#13809).Fixes #14188
Fixes #13809