Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Mar 13, 2025

for expression like

if (doFoo() > 0)

we want to pass the context specific return-type information into type-specifying extensions so they can decide not just on true/truethy/false/falsy but also on "remaining return type values" (e.g. preg_match returns 0|1|false)

this should help to reduce the number of hardcoded hacks within the TypeSpecifier und move over some cases into extensions (which also allows 3rd party extensions todo similar things, as they can't hard-code hack the TypeSpecifier)

ondrej quoted:

There should be a new method on the context, can be getReturnType

Let’s say we have doFoo(): int
if (doFoo() > 0), in the then branch, this should return int<1, max>, in the else branch it should return int<min, 0>

public static function createTrue(?Type $returnType = null): self
{
return self::create(self::CONTEXT_TRUE);
return self::create(self::CONTEXT_TRUE, $returnType);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

todo: bit-field cleanup after the main-point of this PR works

About true/truthy - let’s get rid of the bit-masking, I think they will be implemented as getReturnType()->isTrue()->yes()
The other one as getReturnType()->toBoolean()->isTrue()->yes()

@clxmstaab clxmstaab force-pushed the context-return-type branch from 24676ef to 2c615a7 Compare March 13, 2025 15:41
$negatedReturnType = null;
if ($this->returnType !== null) {
$baseType = $this->returnType->generalize(GeneralizePrecision::lessSpecific());
$negatedReturnType = TypeCombinator::remove($baseType, $this->returnType);
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 won't work for unions of int. We will need 2 two return types in the context and toggle between them on negate()

@staabm
Copy link
Contributor Author

staabm commented Mar 14, 2025

had a different try with #3881

@staabm staabm closed this Mar 14, 2025
@staabm staabm deleted the context-return-type branch March 14, 2025 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants