-
Notifications
You must be signed in to change notification settings - Fork 590
Ignore the new Foo() exactness flavour when comparing generic type arguments for variance
#6487
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
Open
phpstan-bot
wants to merge
1
commit into
phpstan:2.3.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-vzs2zfj
base: 2.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug15235; | ||
|
|
||
| /** | ||
| * @template T of object | ||
| */ | ||
| class A | ||
| { | ||
| /** | ||
| * @param T $object | ||
| */ | ||
| public function __construct(public object $object) {} | ||
| } | ||
|
|
||
| class B | ||
| { | ||
| /** | ||
| * @param A<covariant static> $a | ||
| */ | ||
| public function method(A $a): void {} | ||
|
|
||
| /** | ||
| * @param A<static> $a | ||
| */ | ||
| public function invariant(A $a): void {} | ||
|
|
||
| /** | ||
| * @param A<contravariant static> $a | ||
| */ | ||
| public function contravariant(A $a): void {} | ||
| } | ||
|
|
||
| class D | ||
| { | ||
| public function inconsistent(B $b): void | ||
| { | ||
| $b->method(new A($b)); | ||
| } | ||
| } | ||
|
|
||
| function fromNew(): void | ||
| { | ||
| $b = new B(); | ||
| $b->method(new A($b)); | ||
| $b->invariant(new A($b)); | ||
| $b->contravariant(new A($b)); | ||
| } | ||
|
|
||
| function fromGetClass(B $b): void | ||
| { | ||
| if (get_class($b) !== B::class) { | ||
| return; | ||
| } | ||
|
|
||
| $b->method(new A($b)); | ||
| } | ||
|
|
||
| /** @template-covariant T of object */ | ||
| class Covariant | ||
| { | ||
| /** @param T $object */ | ||
| public function __construct(public object $object) {} | ||
| } | ||
|
|
||
| /** @template-contravariant T of object */ | ||
| class Contravariant | ||
| { | ||
| /** @param T $object */ | ||
| public function set(object $object): void {} | ||
| } | ||
|
|
||
| class E | ||
| { | ||
| /** @param Covariant<static> $c */ | ||
| public function declaredCovariant(Covariant $c): void {} | ||
|
|
||
| /** @param Contravariant<static> $c */ | ||
| public function declaredContravariant(Contravariant $c): void {} | ||
|
|
||
| /** @param \Traversable<int, static> $it */ | ||
| public function traversable(\Traversable $it): void {} | ||
|
|
||
| /** @return Covariant<static> */ | ||
| public function makeCovariant(): Covariant | ||
| { | ||
| return new Covariant($this); | ||
| } | ||
|
|
||
| /** @return Wrapper<static> */ | ||
| public function makeWrapper(): Wrapper | ||
| { | ||
| return new Wrapper($this); | ||
| } | ||
| } | ||
|
|
||
| class Consumer | ||
| { | ||
| /** @param Covariant<E> $c */ | ||
| public function takesCovariant(Covariant $c): void {} | ||
|
|
||
| /** @param Wrapper<contravariant E> $wrapper */ | ||
| public function takesContravariantWrapper(Wrapper $wrapper): void {} | ||
|
|
||
| /** @param Wrapper<Covariant<E>> $wrapper */ | ||
| public function takesNested(Wrapper $wrapper): void {} | ||
| } | ||
|
|
||
| /** | ||
| * @param Covariant<E> $covariant | ||
| * @param Contravariant<E> $contravariant | ||
| * @param \Traversable<int, E> $traversable | ||
| */ | ||
| function declaredVariances(Covariant $covariant, Contravariant $contravariant, \Traversable $traversable): void | ||
| { | ||
| $e = new E(); | ||
| $e->declaredCovariant($covariant); | ||
| $e->declaredContravariant($contravariant); | ||
| $e->traversable($traversable); | ||
| } | ||
|
|
||
| function flavourOnTheArgumentSide(Consumer $consumer): void | ||
| { | ||
| $e = new E(); | ||
| $consumer->takesCovariant($e->makeCovariant()); | ||
| $consumer->takesContravariantWrapper($e->makeWrapper()); | ||
| } | ||
|
|
||
| /** @template T of object */ | ||
| class Wrapper | ||
| { | ||
| /** @param T $object */ | ||
| public function __construct(public object $object) {} | ||
| } | ||
|
|
||
| function nestedInGenericType(Consumer $consumer): void | ||
| { | ||
| $e = new E(); | ||
| $consumer->takesNested(new Wrapper($e->makeCovariant())); | ||
| } | ||
|
|
||
| class Composite | ||
| { | ||
| /** @param Covariant<static>|null $c */ | ||
| public function inUnion(?Covariant $c): void {} | ||
|
|
||
| /** @param array<int, Covariant<static>> $c */ | ||
| public function inArray(array $c): void {} | ||
|
|
||
| /** @param Covariant<Covariant<static>> $c */ | ||
| public function nested(Covariant $c): void {} | ||
|
|
||
| /** @param Wrapper<Covariant<static>> $c */ | ||
| public function nestedInvariant(Wrapper $c): void {} | ||
| } | ||
|
|
||
| /** | ||
| * @param Covariant<Composite> $covariant | ||
| * @param array<int, Covariant<Composite>> $array | ||
| * @param Covariant<Covariant<Composite>> $nested | ||
| * @param Wrapper<Covariant<Composite>> $nestedInvariant | ||
| */ | ||
| function compositeShapes(Covariant $covariant, array $array, Covariant $nested, Wrapper $nestedInvariant): void | ||
| { | ||
| $composite = new Composite(); | ||
| $composite->inUnion($covariant); | ||
| $composite->inUnion(null); | ||
| $composite->inArray($array); | ||
| $composite->nested($nested); | ||
| $composite->nestedInvariant($nestedInvariant); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug15235Properties; | ||
|
|
||
| /** | ||
| * @template T of object | ||
| */ | ||
| class A | ||
| { | ||
| /** | ||
| * @param T $object | ||
| */ | ||
| public function __construct(public object $object) {} | ||
| } | ||
|
|
||
| /** @template-covariant T of object */ | ||
| class Covariant | ||
| { | ||
| /** @param T $object */ | ||
| public function __construct(public object $object) {} | ||
| } | ||
|
|
||
| class B | ||
| { | ||
| /** @var A<covariant static>|null */ | ||
| public ?A $callSiteVariance = null; | ||
|
|
||
| /** @var Covariant<static>|null */ | ||
| public ?Covariant $declaredVariance = null; | ||
| } | ||
|
|
||
| function fromParameter(B $b): void | ||
| { | ||
| $b->callSiteVariance = new A($b); | ||
| $b->declaredVariance = new Covariant($b); | ||
| } | ||
|
|
||
| function fromNew(): void | ||
| { | ||
| $b = new B(); | ||
| $b->callSiteVariance = new A($b); | ||
| $b->declaredVariance = new Covariant($b); | ||
| } |
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.
Since this is almost a one liner, this is not worth introducing a private method.
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.
Still running — I'll report once both complete.