-
Notifications
You must be signed in to change notification settings - Fork 571
Don't remember types after impure assignments #5514
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a740556
Don't remember types after impure assignments
staabm 0c53e50
Update AssignHandler.php
staabm a07105f
Update bug-3190.php
staabm ab56e42
discard impure call knowledge
staabm 1ca65f5
Update AssignHandler.php
staabm 894390f
fix
staabm 6e84a69
Update AssignHandler.php
staabm c55f7ba
Update AssignHandler.php
staabm 676bcd8
tests
staabm 262c28a
Update functionMetadata_original.php
staabm ba701ce
more tests
staabm 828b15a
Update AssignHandler.php
staabm ccaaae9
test rememberPossiblyImpureFunctionValues
staabm 28b1b9b
Update AssignHandler.php
staabm b6ba210
fix build
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1758,4 +1758,4 @@ | |
| 'zlib_encode' => ['hasSideEffects' => false], | ||
| 'zlib_get_coding_type' => ['hasSideEffects' => false], | ||
|
|
||
| ]; | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/PHPStan/Analyser/data/bug-9455-not-remembered-possibly-impure.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9455DoNotRememberedPossiblyImpureFunctionValues; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A { | ||
| public function __construct(private int $id){} | ||
|
|
||
| public function getId(): int { | ||
| return $this->id; | ||
| } | ||
| } | ||
|
|
||
| class B { | ||
| public function __construct(private int $id, private ?A $a = null){} | ||
|
|
||
| public function getId(): int { | ||
| return $this->id; | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-pure | ||
| */ | ||
| public function getPure(): ?A { | ||
| return $this->a; | ||
| } | ||
| } | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function testFails(): void | ||
| { | ||
| $a = new A(1); | ||
| $b = new B(1, $a); | ||
|
|
||
| $hasA = $b->getPure() !== null; | ||
|
|
||
| if($hasA) { | ||
| assertType('Bug9455DoNotRememberedPossiblyImpureFunctionValues\A|null', $b->getPure()); | ||
| } | ||
| } | ||
|
|
||
| public function testSucceeds(): void | ||
| { | ||
| $a = new A(1); | ||
| $b = new B(1, $a); | ||
|
|
||
| if($b->getPure() !== null) { | ||
| assertType('Bug9455DoNotRememberedPossiblyImpureFunctionValues\A', $b->getPure()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class C { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| public function getImpure(): ?A { | ||
| return rand(0, 1) ? new A(1) : null; | ||
| } | ||
| } | ||
|
|
||
| class ImpureTest | ||
| { | ||
| public function testImpureMethodNotNarrowed(): void | ||
| { | ||
| $c = new C(); | ||
|
|
||
| $hasA = $c->getImpure() !== null; | ||
|
|
||
| if($hasA) { | ||
| assertType('Bug9455DoNotRememberedPossiblyImpureFunctionValues\A|null', $c->getImpure()); | ||
| } | ||
| } | ||
|
|
||
| public function testImpureMethodInline(): void | ||
| { | ||
| $c = new C(); | ||
|
|
||
| if($c->getImpure() !== null) { | ||
| assertType('Bug9455DoNotRememberedPossiblyImpureFunctionValues\A|null', $c->getImpure()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class D { | ||
| public function getUnknownPurity(): ?A { | ||
| return rand(0, 1) ? new A(1) : null; | ||
| } | ||
| } | ||
|
|
||
| function doUnknownPurity(): void | ||
| { | ||
| $d = new D(); | ||
|
|
||
| $hasA = $d->getUnknownPurity() !== null; | ||
|
|
||
| if($hasA) { | ||
| assertType('Bug9455DoNotRememberedPossiblyImpureFunctionValues\A|null', $d->getUnknownPurity()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| interface Server | ||
| { | ||
| /** @pure */ | ||
| public function isDedicated(): bool; | ||
|
|
||
| public function getSize(): int; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug5207; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| abstract class HelloWorld { | ||
| /** @phpstan-pure */ | ||
| abstract public function getChild(): ?HelloWorld; | ||
|
|
||
| public function sayHello(): void { | ||
| $foo = null !== $this->getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\HelloWorld', $this->getChild()); | ||
| } | ||
| } | ||
|
|
||
| public function sayHelloInline(): void { | ||
| if (null !== $this->getChild()) { | ||
| assertType('Bug5207\HelloWorld', $this->getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class StaticWorld { | ||
| /** @phpstan-pure */ | ||
| abstract public static function getChild(): ?StaticWorld; | ||
|
|
||
| public static function sayHello(): void { | ||
| $foo = null !== static::getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\StaticWorld', static::getChild()); | ||
| } | ||
| } | ||
|
|
||
| public static function sayHelloInline(): void { | ||
| if (null !== static::getChild()) { | ||
| assertType('Bug5207\StaticWorld', static::getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class ImpureStaticWorld { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| abstract public static function getChild(): ?ImpureStaticWorld; | ||
|
|
||
| public static function sayHello(): void { | ||
| $foo = null !== static::getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\ImpureStaticWorld|null', static::getChild()); | ||
| } | ||
| } | ||
|
|
||
| public static function sayHelloInline(): void { | ||
| if (null !== static::getChild()) { | ||
| assertType('Bug5207\ImpureStaticWorld|null', static::getChild()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abstract class ImpureWorld { | ||
| /** | ||
| * @phpstan-impure | ||
| */ | ||
| abstract public function getChild(): ?ImpureWorld; | ||
|
|
||
| public function sayHello(): void { | ||
| $foo = null !== $this->getChild(); | ||
| if ($foo) { | ||
| assertType('Bug5207\ImpureWorld|null', $this->getChild()); | ||
| } | ||
| } | ||
|
|
||
| public function sayHelloInline(): void { | ||
| if (null !== $this->getChild()) { | ||
| assertType('Bug5207\ImpureWorld|null', $this->getChild()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 not sure to understand the logic here.
If we rememberPossiblyImpureFunctionValues: false, you automatically continue.
But what if
rememberPossiblyImpureFunctionValuesis false BUT the function has@phpstan-pure? We should not continue no ?Uh oh!
There was an error while loading. Please reload this page.
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.
my thinking is:
the logic we have here depends on impure points, which only can exist or not. we cannot reflect a "maybe has impure point" situation.
thats why I allow narrowing using function/method/static-calls in assignments only when
rememberPossiblyImpureFunctionValues=trueto be defensive.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.
When rememberPossiblyImpureFunctionValues=true and the method is maybe impure, seems like we're calling
not sure if it helps.
Another idea:
Couldn't we add manually an ImpurePoint for non Pure method when
rememberPossiblyImpureFunctionValues=false?