Skip to content

Add TemplateIntegerRangeType, TemplateFloatRangeType and TemplateConstantFloatType - #7290

Merged
ondrejmirtes merged 2 commits into
2.3.xfrom
template-range-and-constant-float-types
Dec 2, 2026
Merged

ondrejmirtes merged 2 commits into
2.3.xfrom
template-range-and-constant-float-types

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Template types bounded by int<a, b>, float<a, b> or a float constant were created as TemplateIntegerType / TemplateFloatType. They kept the bound but behaved as plain int / float, and TemplateTypeCheck reported these bounds as not supported. This adds TemplateIntegerRangeType, TemplateFloatRangeType and TemplateConstantFloatType, following TemplateConstantIntegerType / TemplateFloatType, with their turbo-ext mirrors.

With @template T of int<0, 5>: $t > 10 is false (was bool), $t + 1 is int<1, 6> (was int), abs($t) is T.

Things that needed care because a template range is now an IntegerRangeType / FloatRangeType:

  • IntegerRangeType and FloatRangeType constructors are protected instead of private.
  • A template range is an unknown part of its range: the range types no longer merge it in tryUnion() / tryIntersect() (which dropped the template) or remove it as a whole in tryRemove() (which was unsound: int<0, 9> minus T of int<0, 5> gave int<6, 9>).
  • UnionType::isSuperTypeOf() no longer hands a template range back to isSubTypeOf() (infinite recursion), like TemplateIterableType.
  • FiniteTypesRemoveRule no longer removes every value of a template that has several. This also fixes bool minus T of bool being never.
  • The range templates don't use UndecidedComparisonCompoundTypeTrait: the range's own comparisons hold for any subset of it.

Two expectations in bug-15714.php became more precise (abs() of a negative T of int is int<1, max>, abs() of T of float<0, 1> is T).

Closes phpstan/phpstan#10660

…tantFloatType

Template types bounded by an integer range, a float range or a float
constant were created as TemplateIntegerType or TemplateFloatType, which
kept the bound but behaved as plain int or float, and TemplateTypeCheck
reported these bounds as not supported. Each now has its own template
class over the bound's type, so comparisons, arithmetic and abs() use
the range or the constant.

A template range is an unknown part of its range, not all of it: the
range types no longer merge it in tryUnion()/tryIntersect() or remove it
as a whole in tryRemove(), UnionType::isSuperTypeOf() no longer hands it
back to isSubTypeOf(), and FiniteTypesRemoveRule no longer removes all
the values of a template that has several of them.

Amp-Thread-ID: https://ampcode.com/threads/T-01a23674-8e36-7696-8d80-30a0668f254a
@ondrejmirtes
ondrejmirtes merged commit 7790471 into 2.3.x Dec 2, 2026
880 of 916 checks passed
@ondrejmirtes
ondrejmirtes deleted the template-range-and-constant-float-types branch December 2, 2026 14:08
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.

Multiple errors when using int-mask-of values that resolve to int<0, n> in templates

1 participant