Skip to content

Keep the witness type of ErrorType - #7294

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
error-type-witness
Dec 2, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
error-type-witness

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

An undefined function or another error yields ErrorType, a subclass of MixedType. Until now it dropped the witness type, so a path known to produce a wrong value was hidden next to it:

$a = random(); // undefined function
if (rand(0, 1)) {
	$a = 1;
}
echo strlen($a); // no error

https://phpstan.org/r/7cf17f3b-a48a-4042-be7d-ac7a4e86714a

With the checkMixedWitnessTypes bleeding edge toggle, $a is now *ERROR*+1 and strlen() reports "expects string, int given."

  • MixedType builds the types derived from it (subtract(), changeSubtractedType(), changeWitnessType(), traverse()) through a new protected recreate(). ErrorType overrides it to stay an error with its reason, with nothing subtracted.
  • ErrorType takes an optional witness type in its constructor, describes it at the precise level as *ERROR*+W, compares it in equals() and removes from it in subtract().
  • TypeCombinator::joinValues() gives witnesses to an exact ErrorType as well as an exact MixedType; template and other subclasses still do not hold them.
  • The native extension mirrors all of this and honours a PHP subclass that overrides recreate(). Analysis output is identical with the extension on and off.

Amp thread: https://ampcode.com/threads/T-01a22b49-862e-7075-bc0f-c71955e86bb9

A path known to produce a concrete value is no longer lost when it meets
an error type, so strlen() of *ERROR*+1 reports that it expects string
and int is given. MixedType builds its derived types through recreate(),
which ErrorType overrides to keep its reason and kind.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22b49-862e-7075-bc0f-c71955e86bb9
@ondrejmirtes
ondrejmirtes merged commit a7b39ed into 2.3.x Dec 2, 2026
387 of 409 checks passed
@ondrejmirtes
ondrejmirtes deleted the error-type-witness branch December 2, 2026 20:39
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.

1 participant