Skip to content

Stdlib: in_array() loose comparison must not match enum case to backing scalar (ext/standard/array.c) #5592

Description

@PurHur

Category

stdlib

Problem

With loose comparison ($strict = false), in_array() must not treat a backed enum case as equal to its backing scalar (and vice versa). This compiler uses Variable::equals() which coerces enum cases to ints/strings, so in_array(1, [E::A], false) is true; Zend returns false.

php-src reference

Repro (verified 2026-06-04)

<?php
enum E: int { case A = 1; case B = 2; }
var_export(in_array(1, [E::A], false));
echo "\n";
var_export(in_array(E::A, [1], false));
echo "\n";
var_export(in_array('1', [E::A], false));
echo "\n";
var_export(in_array(E::A, [E::A], true)); // must stay true
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php -r "enum E:int{case A=1;} var_export(in_array(1,[E::A],false)); echo \"\\n\";"
php bin/vm.php -r "enum E:int{case A=1;} var_export(in_array(1,[E::A],false)); echo \"\\n\";"
'
Expression Zend VM today
in_array(1, [E::A], false) false true
in_array(E::A, [1], false) false true
in_array(E::A, [E::A], true) true true

Scope (this repo)

Module Path
VM ext/standard/in_array.php — looseEquals() / equals() path
VM lib/VM/Variable.php — enum-aware loose compare (coordinate #5178 loose ==)
JIT ext/standard/in_array.php JIT lowering via ArrayBuiltinHelper
Tests test/compliance/cases/stdlib/in_array_enum_loose.phpt

Done when

  • Repro: first three lines false; strict identity line true
  • ./script/ci-fast.sh --filter in_array_enum green

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