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

Fixed coercion of mixed into non-empty-mixed #9541

Merged
merged 1 commit into from Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php
Expand Up @@ -26,6 +26,7 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNever;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNonEmptyMixed;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
Expand Down Expand Up @@ -87,8 +88,11 @@ public static function isContainedBy(
&& !$container_type_part->extra_types
&& $input_type_part instanceof TMixed)
) {
if (get_class($container_type_part) === TEmptyMixed::class
&& get_class($input_type_part) === TMixed::class
if (get_class($input_type_part) === TMixed::class
&& (
get_class($container_type_part) === TEmptyMixed::class
|| get_class($container_type_part) === TNonEmptyMixed::class
)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
Expand Down
5 changes: 1 addition & 4 deletions src/Psalm/Internal/Type/Comparator/UnionTypeComparator.php
Expand Up @@ -44,7 +44,7 @@ public static function isContainedBy(
bool $allow_interface_equality = false,
bool $allow_float_int_equality = true
): bool {
if ($container_type->isMixed()) {
if ($container_type->isVanillaMixed()) {
return true;
}

Expand All @@ -63,9 +63,6 @@ public static function isContainedBy(
return false;
}

if ($container_type->hasMixed() && !$container_type->isEmptyMixed()) {
return true;
}

$container_has_template = $container_type->hasTemplateOrStatic();

Expand Down
4 changes: 4 additions & 0 deletions tests/TypeComparatorTest.php
Expand Up @@ -173,5 +173,9 @@ public function getUnsuccessfulComparisons(): iterable
'list<int>',
'array{int, string}',
];
yield 'nonEmptyMixedDoesNotAcceptMixed' => [
'non-empty-mixed',
'mixed',
];
}
}