Skip to content

Stdlib: array_reduce() — enum case + int arithmetic must TypeError not coerce backing int (ext/standard/array.c) #8972

Description

@PurHur

Category

stdlib | php-src-strict

Problem

array_reduce() with an int-backed enum case array and a callback that adds integers must TypeError when Zend cannot coerce enum objects to int in the reducer. This compiler coerces enum cases to backing ints and returns a sum.

Distinct from #5626 (callback must receive enum objects); this issue is the TypeError when reducer performs illegal scalar arithmetic on enum cases.

php-src reference

Repro

<?php
enum E: int { case A = 1; case B = 2; }
try {
    echo array_reduce([E::A, E::B], fn($c, $i) => $c + $i, 0), "\n";
} catch (TypeError $e) {
    echo "te\n";
}
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php repro.php          # te
php bin/vm.php repro.php   # 3 today
'
Runtime Output
Zend PHP 8.2+ te (TypeError on enum + int)
VM 3

Scope

Module Path
VM ext/standard/array_reduce.php, enum-aware reducer in lib/VM/
JIT defer or mirror VM strictness
Tests test/compliance/cases/stdlib/array_reduce_enum_type_error.phpt

Done when

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions