Skip to content

Treat a switch whose cases cover every value as exhaustive - #7264

Open
ondrejmirtes wants to merge 3 commits into
2.3.xfrom
switch-exhaustive-cases
Open

ondrejmirtes wants to merge 3 commits into
2.3.xfrom
switch-exhaustive-cases

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

A switch was exhaustive only when the subject's remaining type became never. In switch (true) the subject stays true, so a case that always matches never made the switch exhaustive, unlike the equivalent if/elseif chain.

  • A case whose subject == case comparison is always true now makes the switch exhaustive: no return.missing, and code after the switch is unreachable.
  • The subject is narrowed by every case that did not match, also across cases falling through into each other, the same way match narrows after each arm condition. Previously it was narrowed only after a terminating group, through a growing BooleanOr. The always-true verdict is composed from the subject's and the case's results through resolveEqualType(), with no on-demand walk.
  • Like a match arm, an always-true last case is not reported unless a default case it makes unreachable follows it (switch.alwaysTrue, bleeding edge).
  • The native SwitchHandler twin is updated accordingly.

Closes phpstan/phpstan#15892

A case whose `subject == case` comparison is always true leaves no value
for the code below the switch, like an always-true elseif. The subject is
now narrowed by every case that did not match, also across cases falling
through into each other, the same way match narrows after each arm
condition.

An exhaustive switch no longer reports return.missing, the code after it
is unreachable, and an always-true last case followed by a default case is
reported like the corresponding match arm.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22b99-13c4-7062-a0a0-1c86517418dc
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.

switch (true) whose cases cover every value reports return.missing, unlike the equivalent if/elseif

1 participant