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

Fix mixed type hole when sending Foo<string> to Foo<mixed> #8481

Merged
merged 4 commits into from Sep 13, 2022
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
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -52,6 +52,7 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16",
"slevomat/coding-standard": "^7.0",
"phpstan/phpdoc-parser": "1.6.4",
"squizlabs/php_codesniffer": "^3.6",
"symfony/process": "^4.3 || ^5.0 || ^6.0"
},
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Expand Up @@ -38,6 +38,7 @@
<file name="vendor/felixfbecker/advanced-json-rpc/lib/Dispatcher.php" />
<directory name="vendor/netresearch/jsonmapper" />
<directory name="vendor/phpunit" />
<file name="vendor/nikic/php-parser/lib/PhpParser/Node/UnionType.php" />
</ignoreFiles>
</projectFiles>

Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Internal/LanguageServer/Client/TextDocument.php
Expand Up @@ -57,6 +57,8 @@ public function publishDiagnostics(string $uri, array $diagnostics): void
* @param TextDocumentIdentifier $textDocument The document to get the content for
*
* @return Promise<TextDocumentItem> The document's current content
*
* @psalm-suppress MixedReturnTypeCoercion due to Psalm bug
*/
public function xcontent(TextDocumentIdentifier $textDocument): Promise
{
Expand Down
8 changes: 1 addition & 7 deletions src/Psalm/Internal/Type/Comparator/GenericTypeComparator.php
Expand Up @@ -167,13 +167,7 @@ public static function isContainedBy(
) {
// do nothing
} else {
if ($container_param->hasMixed() || $container_param->isArrayKey()) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced_from_mixed = true;
}
} else {
$all_types_contain = false;
}
$all_types_contain = false;

if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = false;
Expand Down
6 changes: 3 additions & 3 deletions tests/IfThisIsTest.php
Expand Up @@ -219,7 +219,7 @@ public function compact(): ArrayList
'ifThisIsResolveTemplateParams' => [
'code' => '<?php
/**
* @template T
* @template-covariant T
*/
final class Option
{
Expand All @@ -228,8 +228,8 @@ public function unwrap() { throw new RuntimeException("???"); }
}

/**
* @template L
* @template R
* @template-covariant L
* @template-covariant R
*/
final class Either
{
Expand Down
5 changes: 3 additions & 2 deletions tests/Template/ClassTemplateExtendsTest.php
Expand Up @@ -987,15 +987,16 @@ public function __construct(SplObjectStorage $handlers)
}
}

/** @var SplObjectStorage<\stdClass, string> */
/** @var SplObjectStorage<\stdClass, mixed> */
$storage = new SplObjectStorage();
new SomeService($storage);

$c = new \stdClass();
$storage[$c] = "hello";
/** @psalm-suppress MixedAssignment */
$b = $storage->offsetGet($c);',
'assertions' => [
'$b' => 'string',
'$b' => 'mixed',
],
],
'extendsArrayIterator' => [
Expand Down
17 changes: 17 additions & 0 deletions tests/Template/ClassTemplateTest.php
Expand Up @@ -4600,6 +4600,23 @@ final class Two {}
final class Three {}',
'error_message' => 'InvalidReturnStatement - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:40 - The inferred type \'T:A as One|Two\' ',
],
'preventMixedNestedCoercion' => [
'code' => '<?php
/** @template T */
class MyCollection {
/** @param array<T> $members */
public function __construct(public array $members) {}
}

/**
* @param MyCollection<string> $c
* @return MyCollection<mixed>
*/
function getMixedCollection(MyCollection $c): MyCollection {
return $c;
}',
'error_message' => 'InvalidReturnStatement',
],
];
}
}