Skip to content

Fix MixedType->equals(ErrorType) #3934

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

Merged
merged 13 commits into from
Jul 17, 2025
Prev Previous commit
Next Next commit
Update MutatingScope.php
  • Loading branch information
staabm committed May 31, 2025
commit e0b692d7c0163960c98f125750a0300e70594532
5 changes: 3 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4486,8 +4486,9 @@ public function addTypeToExpression(Expr $expr, Type $type): self

if ($originalExprType->equals($nativeType)) {
$newType = TypeCombinator::intersect($type, $originalExprType);
if (!$newType->isObject()->yes() && $newType->equals($originalExprType)) {
// don't add the same type over and over again to improve performance
if ($newType->isObject()->no() && $newType->equals($originalExprType)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will look into whether we can get rid of the isObject() check here today

Copy link
Contributor Author

@staabm staabm May 14, 2025

Choose a reason for hiding this comment

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

ok, I looked into it. I think we cannot drop the $newType->isObject()->no() check here, because Objects will be considered equal by ObjectType->equals() even though they have slight differences which are important when narrowing, e.g.

new Foo() in local-scope vs. Foo typed variable (implicit Final)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

most conservative move would be to leave it $newType->isConstantScalarValue()->yes() until we get a slow case which benefits from the change and only fix MixedType->equals(ErrorType) for now

// don't add the same type over and over again to improve performance.
// objects can get narrowed even though ObjectType->equal() will return true (e.g. via impicit "final" via new())
return $this;
}
return $this->specifyExpressionType($expr, $newType, $newType, TrinaryLogic::createYes());
Expand Down