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 condition literal-string is non-empty-string #10927

Draft
wants to merge 12 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ public static function handleSplice(
if (($array_arg_type = $statements_analyzer->node_data->getType($array_arg))
&& $array_arg_type->hasArray()
) {
/**
* @var TArray|TKeyedArray
*/
$array_type = $array_arg_type->getArray();
if ($generic_array_type = ArrayType::infer($array_type)) {
$array_size = $generic_array_type->count;
Expand Down
5 changes: 4 additions & 1 deletion src/Psalm/Internal/LanguageServer/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ public function initialize(
$this->path_mapper->configureClientRoot($this->getPathPart($rootUri));
}

return call(
/** @var Promise<InitializeResult> $promise */
$promise = call(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoid

The type 'Amp\Promise<LanguageServerProtocol\InitializeResult>|Amp\Promise<mixed>' is more general than the declared return type 'Amp\Promise<LanguageServerProtocol\InitializeResult>' for Psalm\Internal\LanguageServer\LanguageServer::initialize

This is technically true because the conditional return type is

(T is Promise ? false : (T is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGeneratorReturn>) : int))

and the check InitializeResult is Promise cannot be resolved since InitializeResult&Promise would be a possible type.

/** @return Generator<int, true, mixed, InitializeResult> */
function () use ($workDoneToken) {
$progress = $this->client->makeProgress($workDoneToken ?? uniqid('tkn', true));
Expand Down Expand Up @@ -578,6 +579,8 @@ function () use ($workDoneToken) {
return new InitializeResult($serverCapabilities, $initializeResultServerInfo);
},
);

return $promise;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Psalm/Internal/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public function __construct(Union $key, Union $value, bool $is_list, ?int $count

/**
* @return (
* $type is TArrayKey ? self : (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TArrayKey doesn't exist.

As soon as I fixed TemplateInferredTypeReplacer.php, the error was detected.

* $type is TArray ? self : null
* $type is TKeyedArray ? self : (
* $type is TNonEmptyArray ? self : (
* $type is TArray ? self : null
* )
* )
* )
*/
Expand Down
5 changes: 4 additions & 1 deletion src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public static function isContainedBy(
}

if ($container_type_part instanceof TNonEmptyString
&& get_class($input_type_part) === TString::class
&& (
get_class($input_type_part) === TString::class
|| get_class($input_type_part) === TNonspecificLiteralString::class
)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Internal/Type/TemplateInferredTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ private static function replaceConditional(
null,
false,
false,
) && null === Type::intersectUnionTypes(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic was

When doing A is B ? C : D:

  • If A is contained by B => It's C
  • If B is not contained by A => It's D
  • Else, it's C|D

I think this logic is wrong and does not work well with string type like literal-string and non-empty-string.
literal-string is non-empty-string ? int : float.
Since non-empty-string is not contained by literal-string it was considering the result as a float.
But since literal-string&non-empty-string is non empty, the result should have been int|float.

new Union([$candidate_atomic_type]),
$conditional_type,
$codebase,
false,
false,
)) {
$matching_else_types[] = $candidate_atomic_type;
}
Expand Down
31 changes: 29 additions & 2 deletions src/Psalm/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNever;
use Psalm\Type\Atomic\TNonEmptyLowercaseString;
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonFalsyString;
use Psalm\Type\Atomic\TNonspecificLiteralString;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TNumericString;
Expand Down Expand Up @@ -787,8 +789,26 @@ public static function intersectUnionTypes(

//if a type is contained by the other, the intersection is the narrowest type
if (!$intersection_performed) {
$type_1_in_2 = UnionTypeComparator::isContainedBy($codebase, $type_1, $type_2);
$type_2_in_1 = UnionTypeComparator::isContainedBy($codebase, $type_2, $type_1);
$type_1_in_2 = UnionTypeComparator::isContainedBy(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to use intersectUnionTypes with $allow_float_int_equality = false I got a bug.

Seems like those parameters were not passed to every calls.

$codebase,
$type_1,
$type_2,
false,
false,
null,
$allow_interface_equality,
$allow_float_int_equality,
);
$type_2_in_1 = UnionTypeComparator::isContainedBy(
$codebase,
$type_2,
$type_1,
false,
false,
null,
$allow_interface_equality,
$allow_float_int_equality,
);
if ($type_1_in_2) {
$intersection_performed = true;
$combined_type = $type_1->getBuilder();
Expand Down Expand Up @@ -909,6 +929,13 @@ private static function intersectAtomicTypes(
$intersection_atomic = $type_1_atomic;
$wider_type = $type_2_atomic;
$intersection_performed = true;
} elseif (($type_1_atomic instanceof TNonspecificLiteralString
&& $type_2_atomic instanceof TNonEmptyString)
|| ($type_1_atomic instanceof TNonEmptyString
&& $type_2_atomic instanceof TNonspecificLiteralString)
) {
$intersection_atomic = new TNonEmptyNonspecificLiteralString();
$intersection_performed = true;
}

if ($intersection_atomic
Expand Down
2 changes: 1 addition & 1 deletion stubs/CoreGenericClasses.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface Traversable {
*
* @template-implements Traversable<TKey, TValue>
*/
class Generator implements Traversable {
final class Generator implements Traversable {
/**
* @psalm-ignore-nullable-return
* @return ?TValue Can return any type.
Expand Down
58 changes: 57 additions & 1 deletion tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ function getSomethingElse()
interface ContainerInterface
{
/**
* @template TRequestedInstance extends InstanceType
* @template TRequestedInstance of InstanceType
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@template TRequestedInstance extends InstanceType

is not a correct syntax, of should be used.

As soon as I fixed the TemplateInferredTypeReplacer.php the error occured.

* @param class-string<TRequestedInstance>|string $name
* @return ($name is class-string ? TRequestedInstance : InstanceType)
*/
Expand Down Expand Up @@ -979,6 +979,62 @@ function getSomething(string $string)
],
'ignored_issues' => [],
],
'literalStringIsNotNonEmpty' => [
'code' => '<?php
/**
* @param literal-string $string
* @psalm-return ($string is non-empty-string ? string : int)
*/
function getSomething(string $string)
{
if (!$string) {
return 1;
}

return "";
}

/** @var literal-string $literalString */
$literalString;
$something = getSomething($literalString);
/** @var non-empty-literal-string $nonEmptyliteralString */
$nonEmptyliteralString;
$something2 = getSomething($nonEmptyliteralString);
',
'assertions' => [
'$something' => 'int|string',
'$something2' => 'string',
],
'ignored_issues' => [],
],
'literalStringIsNotNonEmptyWithUnion' => [
'code' => '<?php
/**
* @param literal-string|int $string
* @psalm-return ($string is non-empty-string|int ? string : int)
*/
function getSomething($string)
{
if (!$string) {
return 1;
}

return "";
}

/** @var literal-string $literalString */
$literalString;
$something = getSomething($literalString);
/** @var non-empty-literal-string $nonEmptyliteralString */
$nonEmptyliteralString;
$something2 = getSomething($nonEmptyliteralString);
',
'assertions' => [
'$something' => 'int|string',
'$something2' => 'string',
],
'ignored_issues' => [],
],
];
}
}
Loading