Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address flaw in type reconciliation on array references #8290

Merged
merged 3 commits into from Jul 21, 2022

Conversation

ohader
Copy link
Contributor

@ohader ohader commented Jul 20, 2022

Related: #8289

@ohader ohader marked this pull request as draft July 20, 2022 18:36
@@ -124,7 +124,7 @@ public static function reconcileKeyedTypes(

$cloned_referenceds = [];
foreach ($existing_references as $reference => $referenced) {
if (!isset($cloned_referenceds[$referenced])) {
if (!isset($cloned_referenceds[$referenced]) && isset($old_existing_types[$referenced])) {
Copy link
Contributor Author

@ohader ohader Jul 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndrolGenhald I'm not sure whether this is enough, or

  • whether there should be a fallback union type assigned to $existing_types[$referenced], or
  • whether the $referenced value (e.g. '$doesNotMatter[$a]') should to be split to corresponding types of $doesNotMatter + $a and traversed

Thanks in advance for any hint.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll step through it with your test tonight or tomorrow and see what's going on. Thanks for the reproduction and possible fix!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it needs to have a type, I just ran into another error in a project having this patch applied:

Uncaught Error: Call to a member function equals() on null in vendor/vimeo/psalm/src/Psalm/Context.php:524

if (!$this_type->equals($new_type)) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, having nulls in there is not a good idea. I think I know what's going on though; when we do the check against $a it potentially changes $a's type, which means we remove everything dependent on $a from Context::$vars_in_scope (somewhere, can't remember where right now) because $doesNotMatter[$a] doesn't have the same type if $a has changed. When that happens, we also need to do something to any references that exist to $doesNotMatter[$a], probably by changing whatever code removes $doesNotMatter[$a] to use the normal process by calling Context::remove() instead of whatever it's doing now.

Of course this means the fact that it's a reference will be lost, but like I said before, combining references with arrays and objects is just asking for trouble, and there's not much we can do about it.

Edit: oh, this is actually because it's removed from $existing_types in the same method further down, before it even gets to the context. Line 339 (and possibly 724) are where the issue is happening. I think it needs to do something sort of like Context:569-588, but probably a bit different using $reference_map.

@ohader ohader marked this pull request as ready for review July 20, 2022 19:50
@AndrolGenhald
Copy link
Collaborator

I think I have a fix that should work, but I need to find a bit more time to finish it since it requires modifying callers of Reconciler::reconcileKeyedTypes().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants