-
Notifications
You must be signed in to change notification settings - Fork 553
Fix inconsistent type inference #4928
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
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
bc4bf76
5cbf5ba
deac291
2397e54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1062,6 +1062,19 @@ private function processStmtNode( | |
| }); | ||
|
|
||
| $this->processStmtNodesInternal($stmt, $classLikeStatements, $classScope, $storage, $classStatementsGatherer, $context); | ||
| foreach ($stmt->stmts as $classLikeStmt) { | ||
| if (!$classLikeStmt instanceof Node\Stmt\ClassConst || !$classLikeStmt->isPublic()) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($classLikeStmt->consts as $const) { | ||
| $scope = $scope->assignExpression( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why we don't know the full closure type, whereever it is assigned/calculated before this PR
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already inferred it, but scope transitions dropped |
||
| new Expr\ClassConstFetch(new Name\FullyQualified($classReflection->getName()), $const->name), | ||
| $classScope->getType($const->value), | ||
| $classScope->getNativeType($const->value), | ||
| ); | ||
| } | ||
| } | ||
| $this->callNodeCallback($nodeCallback, new ClassPropertiesNode($stmt, $this->readWritePropertiesExtensionProvider, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls(), $classStatementsGatherer->getReturnStatementsNodes(), $classStatementsGatherer->getPropertyAssigns(), $classReflection), $classScope, $storage); | ||
| $this->callNodeCallback($nodeCallback, new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls(), $classReflection), $classScope, $storage); | ||
| $this->callNodeCallback($nodeCallback, new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches(), $classReflection), $classScope, $storage); | ||
|
|
@@ -2212,10 +2225,17 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch | |
| if ($scope->getClassReflection() === null) { | ||
| throw new ShouldNotHappenException(); | ||
| } | ||
| $constType = $scope->getType($const->value); | ||
| $constNativeType = $scope->getNativeType($const->value); | ||
| $scope = $scope->assignExpression( | ||
| new Expr\ClassConstFetch(new Name\FullyQualified($scope->getClassReflection()->getName()), $const->name), | ||
| $scope->getType($const->value), | ||
| $scope->getNativeType($const->value), | ||
| $constType, | ||
| $constNativeType, | ||
| ); | ||
| $scope = $scope->assignExpression( | ||
| new Expr\ClassConstFetch(new Name('self'), $const->name), | ||
| $constType, | ||
| $constNativeType, | ||
| ); | ||
| } | ||
| } elseif ($stmt instanceof Node\Stmt\EnumCase) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php // lint >= 8.2 | ||
|
|
||
| namespace Bug14105; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| final readonly class Foo | ||
| { | ||
| const func = static function (int $num): array { | ||
| return ['num' => $num]; | ||
| }; | ||
| } | ||
|
|
||
| final readonly class PrivateFoo | ||
| { | ||
| private const func = static function (int $num): array { | ||
| return ['num' => $num]; | ||
| }; | ||
|
|
||
| public static function testStatic(): void | ||
| { | ||
| assertType('Closure(int): array{num: int}', self::func); | ||
| } | ||
|
|
||
| public function testNonStatic(): void | ||
| { | ||
| assertType('Closure(int): array{num: int}', self::func); | ||
| } | ||
| } | ||
|
|
||
| const func = static function (int $num): array { | ||
| return ['num' => $num]; | ||
| }; | ||
|
|
||
| assertType('Closure(int): array{num: int}', Foo::func); | ||
| assertType('Closure(int): array{num: int}', func); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a test when the constant is private:
https://3v4l.org/qhTGS#veol
I wonder why this works before the PR already
https://phpstan.org/r/5e8b0fd5-fa52-4787-bcd5-cb25eed12508
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna add a few more tests with fixes. Also, I think I've accidentally fixed this:
phpstan-src/tests/PHPStan/Analyser/nsrt/initializer-expr-type-resolver.php
Line 17 in 5abeb3c
I'm not sure if this was supposed to be fixed? It would make sense, though. Now it correctly resolves to
42and the*ERROR*assertion fails.It doesn't look like working to me. It prints
Dumped type: Closure(int): array, it should printDumped type: Closure(int): array{num: int}.