Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:110734
Return-Path: <levi.morrison@datadoghq.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 95705 invoked from network); 26 Jun 2020 15:09:11 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 26 Jun 2020 15:09:11 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 153D11804CB
	for <internals@lists.php.net>; Fri, 26 Jun 2020 06:57:08 -0700 (PDT)
X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net
X-Spam-Level: 
X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,
	RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no
	autolearn_force=no version=3.4.2
X-Spam-ASN: AS15169 209.85.128.0/17
X-Spam-Virus: No
X-Envelope-From: <levi.morrison@datadoghq.com>
Received: from mail-io1-f48.google.com (mail-io1-f48.google.com [209.85.166.48])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by php-smtp4.php.net (Postfix) with ESMTPS
	for <internals@lists.php.net>; Fri, 26 Jun 2020 06:57:07 -0700 (PDT)
Received: by mail-io1-f48.google.com with SMTP id h4so9892832ior.5
        for <internals@lists.php.net>; Fri, 26 Jun 2020 06:57:07 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=datadoghq.com; s=google;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=wKvhLtOSJE+99UJQjK5wTau8WNgYSBS/pYUMTS2q/Lo=;
        b=hEKgf4//3vqfWmgJt4N6mNTevZXO3Vl5j4Aib8OvNijfRrs3MzVHDnMDq8wYBjj8Ef
         eHFdqlbyiEBUSa93M3OVZHE4XzKguKDIGnkqXx/qbBwnqQTKfc5+pMqzI9npHXAbuR76
         qpbc37LugjnPGPXHYL+9f38XyUGnXnqWg8Ihs=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=wKvhLtOSJE+99UJQjK5wTau8WNgYSBS/pYUMTS2q/Lo=;
        b=f5rGZ+g3MlJd3uDQp6yOPWxz3QdWcXVZUDMg2Yts3lKATv9FtcphFjJiMrPMjWzVmE
         0nkEtB/FuEggRsvS+ShR1QT7IxN8ihF5k3DwiL75r2P50QJMWmxKP6ZzeiRDjbR8k2Ob
         +4yTK+Se3HLOY4u1chip9P/Q+DX2OoJM27cvcsBYGwRumJ04qQIMQ4Lq0SXfN+Euwqjb
         /CYdm/Zoh3TuBS9o+fjsGmla4I82/KskZZ8iiED+9rOe78bxwzLqYrE2/JNm4gRQAZZi
         mNqqcQqCnEGoJnbH6GiaTEOVY9HpnX8ARMp8mMYYYBZiDPxq1CrZSUqdE0DA4d9WasaJ
         qwHQ==
X-Gm-Message-State: AOAM5328MpidNsM0z73lsY3yEUyjfRNhY+VthppmO9v8myy/8xvIDG8z
	KVwQBbqw928/y3CcVo/fyXGD3R8qiJYXbYLqcV2E9ojV
X-Google-Smtp-Source: ABdhPJyqHSa6O4rWv2cuZCS1hU9q6f8ivx9qwCDqjYQrwEXFemoQkIwPMRY7lQnRuyMt4Ek7ukl+PdIiCfFHHq61oxg=
X-Received: by 2002:a6b:4409:: with SMTP id r9mr3516376ioa.158.1593179826236;
 Fri, 26 Jun 2020 06:57:06 -0700 (PDT)
MIME-Version: 1.0
References: <CACsECNfs4ZDOzkpRh_TqD7O8Mh0FU9MP0he0JoRoVMiVWXbEeQ@mail.gmail.com>
In-Reply-To: <CACsECNfs4ZDOzkpRh_TqD7O8Mh0FU9MP0he0JoRoVMiVWXbEeQ@mail.gmail.com>
Reply-To: Levi Morrison <levi.morrison@datadoghq.com>
Date: Fri, 26 Jun 2020 07:56:55 -0600
Message-ID: <CAAKU0un+vUWi9PuSvAFNd=LhZJDuRvvDMGHT4WL7o4VyH2tZ3Q@mail.gmail.com>
To: Alex <alexinbeijing@gmail.com>
Cc: PHP internals <internals@lists.php.net>
Content-Type: text/plain; charset="UTF-8"
Subject: Re: [PHP-DEV] Convert SplFixedArray to Aggregate?
From: internals@lists.php.net ("Levi Morrison via internals")

On Fri, Jun 26, 2020 at 12:45 AM Alex <alexinbeijing@gmail.com> wrote:
>
> Dear PHP Internals,
>
> I would like to propose a backwards-incompatible change to
> SplFixedArray which fixes the strange and almost certainly unintended
> behavior reported in Bug 79404
> (https://bugs.php.net/bug.php?id=79404).
>
> In short: Because SplFixedArray is an Iterator, and stores its own
> iteration state, it cannot be used in nested foreach loops. If you
> try, the inner loop will overwrite the iteration position of the outer
> loop, so that the outer loop 'thinks' it is finished after the inner
> loop finishes.
>
> To illustrate:
>
> $spl = SplFixedArray::fromArray([0, 1]);
> foreach ($spl as $a) {
>   foreach ($spl as $b) {
>     echo "$a $b";
>   }
> }
>
> Only prints two lines:
>
> 0 0
> 0 1
>
> The fix is to convert SplFixedArray to an IteratorAggregate, so each
> nested foreach loop operates on a different iterator object and thus
> nested loops don't interfere with each other.
>
> Now, it would be possible to do this while still defining key(),
> current(), next(), etc. methods on SplFixedArray. However, that would
> cause an even more insidious BC break, since these methods would no
> longer work as formerly when called in the body of a foreach loop.
> Therefore, I think it better to remove them. This may break a few
> users' code, but in a way which will be easier for them to debug than
> if the old methods were kept but subtly changed in behavior.
>
> Code to implement this change is here:
> https://github.com/php/php-src/pull/5384/files
>
> Your comments will be appreciated,
> Alex Dowad
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>

As someone who has patches in the SPL, I support the idea of this
change. One gotcha of aggregates is iterator invalidation; are you
sure the patch takes care of it? If you have an array of size 3, get
an iterator, iterate to offset 2, shrink the size with `setSize`, and
use the iterator at offset 2, will everything be okay?