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

in_array returns false in strict mode if types are incompatibles #7141

Merged
merged 1 commit into from Dec 12, 2021

Conversation

mathroc
Copy link
Contributor

@mathroc mathroc commented Dec 12, 2021

fixes #5552

@mathroc mathroc force-pushed the fix/in-array-return-type branch 8 times, most recently from c8eba46 to 2374d24 Compare December 12, 2021 18:49
@mathroc mathroc marked this pull request as ready for review December 12, 2021 19:00
src/Psalm/Internal/Type/TypeCombiner.php Show resolved Hide resolved
@@ -377,24 +377,7 @@ function assertInArray($x, $y) {

return $x;
}',
'error_message' => 'RedundantConditionGivenDocblockType - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:30 - Docblock-defined type int for $x is never string',
],
'assertNegatedInArrayOfNotIntersectingTypeTriggersMixedReturnStatement' => [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

would become the same as assertNegatedInArrayOfNotIntersectingTypeTriggersRedundantCondition L317

@weirdan weirdan added the release:feature The PR will be included in 'Features' section of the release notes label Dec 12, 2021
Copy link
Collaborator

@weirdan weirdan left a comment

Choose a reason for hiding this comment

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

Not sure about all those changes to expected messages. Shouldn't we mark the return type with from_docblock flag when any of the source types are coming from docblock, like this:

$ret = Type::getBool();
$ret->from_docblock = $needle_type->from_docblock || $haystack_type->from_docblock;
return $ret;

?

Comment on lines 35 to 36
if (!$third_arg instanceof ConstFetch
|| strtolower($third_arg->name->parts[0]) !== 'true'
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would work with literal true only, but is there any reason for that limitation? Can't we fetch the expression type from the node type provider and use that instead?

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 would work with literal true only, but is there any reason for that limitation? Can't we fetch the expression type from the node type provider and use that instead?

I'm looking at the Psalm internals for the first time, and I forgot to come back to this bit after I discovered the node type provider. I'll change that right away!

Not sure about all those changes to expected messages. Shouldn't we mark the return type with from_docblock flag when any of the source types are coming from docblock, like this:

Didn't know about that either, I'll try and see what I can do 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's ok now, let me know if anything else can be improved

@mathroc mathroc force-pushed the fix/in-array-return-type branch 2 times, most recently from afce897 to 86bb0e3 Compare December 12, 2021 22:52
@orklah
Copy link
Collaborator

orklah commented Dec 12, 2021

Shouldn't we mark the return type with from_docblock flag when any of the source types are coming from docblock

I'm not sure about that. I don't think that's what Psalm does when we use stubs with conditional returns, it seems weird to do that here only

@weirdan
Copy link
Collaborator

weirdan commented Dec 12, 2021

I don't think that's what Psalm does when we use stubs with conditional returns, it seems weird to do that here only

Given the following code:

/** @param list<int> $a */
function f(array $a, string $b): void {
   if (in_array($b, $a, true) {}
}

What's more appropriate here, TypeDoesNotContainType or DocblockTypeContradiction?

@orklah
Copy link
Collaborator

orklah commented Dec 12, 2021

The second one seems more logical. We may want to check how conditional returns are applied and make sure the flag goes through too...

Copy link
Collaborator

@weirdan weirdan left a comment

Choose a reason for hiding this comment

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

Looks good to me. @orklah, do you think this is good to merge?

@orklah orklah merged commit fb07d58 into vimeo:master Dec 12, 2021
@orklah
Copy link
Collaborator

orklah commented Dec 12, 2021

yep :) Thanks @mathroc !

@mathroc mathroc deleted the fix/in-array-return-type branch December 12, 2021 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

in_array 3rd param true - TypeDoesNotContainType error but not vice versa
3 participants