Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108806 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81286 invoked from network); 2 Mar 2020 16:20:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Mar 2020 16:20:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 045E2180089 for ; Mon, 2 Mar 2020 06:39:43 -0800 (PST) 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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,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: Received: from mail-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (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 ; Mon, 2 Mar 2020 06:39:42 -0800 (PST) Received: by mail-il1-f182.google.com with SMTP id b15so9517695iln.3 for ; Mon, 02 Mar 2020 06:39:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=jsbRo1XRHfOwVhQ0KZbHmHw3BNCNL473RNHyKBcGQs4=; b=gMbm/EGMpaHSOJo3p9VxeiKa9xjxCyYJJXCmelh2NyfZTiu39Xpbebz7naHpIkw4In Ovch3Yj61IaX/ze835TwZ5kOM9O9/KQMyWBTrwKOIj7jG8tXuqjuhWjfb87y56qUy9zG xAm/EOglW8p0yKxnY7V8p4aNK/sGvX6EeduDglD166KQ4rxwETJP8tcvnjMbbMbtkfPI 4qlEdhE7KJ1RqpwBYYDfMhMFbBEkg2vyTZpsZ/X9KGbi0mDP1djHEMzPvLV/ArJnoL57 bksgfF2UR9B90FyxfqurHFPdGeYSPnYzH52wZn+ZcOFjNmU2zRZzozgJMY6CKGhudwCb bCKw== 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; bh=jsbRo1XRHfOwVhQ0KZbHmHw3BNCNL473RNHyKBcGQs4=; b=WsGghV5eEiTXz2PaLHFcbaKVovrkWugRtUjsbdzuOe90oL7vLBOzenx30wtQ/znnMT G6ElrgFpGqmG1vW7+CzISMBJHcyvk+dKJmrD7qVsOkhjQ3Bn9HLAyLc3U9v34EgfYwaU OLrYITyC2t3oQsE4Pyw1lJ5uF8bX6NGSSeQFenSO4+MvpYWlVMBL5hAqTItcuVZjbj6h MHPXPRZ3u8xpQKy/wV2w99I33UDrFbNDCdef8HwkbZ580lKT27BFible9cUqZRTAK0f9 esMfoyk+QqMMYK/eTg1zF3bH3inCpUNuMuNGya+aWCuQ9Xjc8JH3MFfUOgVp808aJfuo DcTg== X-Gm-Message-State: ANhLgQ34h5NNchZeIPeEtfCvvSZuBauJI4AKUldRhIFK7eEEvzEkVeYc 2mEDysg21HFbwdGLTEofqsuUuMueaKz8a5sT+aMmpRta X-Google-Smtp-Source: ADFU+vsu+LIMbubkhSBGRcl3q/2aS958sJWPvJ7yrcNXNQpJTK/TZn5HoOBKcTcrFfrYqR5SUWHcXLYYt2eBzv39VSA= X-Received: by 2002:a92:aac1:: with SMTP id p62mr3356353ill.120.1583159981902; Mon, 02 Mar 2020 06:39:41 -0800 (PST) MIME-Version: 1.0 References: <3f615d82-a84b-697b-5c02-8d915270f92c@gmail.com> In-Reply-To: Date: Mon, 2 Mar 2020 14:39:30 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000580e78059fe02818" Subject: Re: [PHP-DEV] [RFC] Increment/Decrement Fixes From: rowan.collins@gmail.com (Rowan Tommins) --000000000000580e78059fe02818 Content-Type: text/plain; charset="UTF-8" Hi Marco, On Mon, 2 Mar 2020 at 14:00, Marco Pivetta wrote: > > Overall against the RFC: `++` and `--` (prefix and suffix) are supposed to > be used with numeric values, not with other types, as that makes everything > only more confusing. > While I'm generally sympathetic to that principle, it could also be said of the + and - operators, which currently silently handle both nulls and booleans. I would be open to changing *all* arithmetic behaviour, particularly on booleans, but don't think a vague ambition for that justifies inconsistent behaviour between different arithmetic operators. > Code written (intentionally) to use `++` and `--` against non-numeric > values should **NOT** pass a code review, and I am sorry for those that > have to maintain it if that happens. > I think you're taking simplified examples too literally here, and assuming that all affected code would be obvious at a glance. Take for instance this innocent-looking patch: function foo($bar) { - $bar -= 1; + $bar--; // more code here } With the current behaviour, any inadvertent calls to the function passing null, true, false, or an array will change behaviour when this patch is merged. In particular, the variable $bar would previously have been guaranteed to be an integer, and will now retain the original type passed in. I completely agree that such code has a bug, but the difference in behaviour is still a massive WTF. The asymmetry of ++ and -- is even more confusing. Consider this patch: function repeatThing($extraTimes) { - for ( $i = 0; $i <= $extraTimes ; $i ++ ) { + for ( $i = $extraTimes ; $i >= 0; $i -- ) { doThing(); } } Again, this looks like a safe change - but if passed a value of NULL, the old code will call doThing() once, and the new code will enter an infinite loop. This asymmetry is the primary problem I want to address with this RFC. > Changing the current behavior, regardless in which way, is a BC break: > might as well make the BC break useful: > > RFC Proposal: `$a = null; $a--; $a === -1`. Let's make this an explicit > TypeError instead. > RFC Proposal: `$a = null; --$a; $a === -1`. Let's make this an explicit > TypeError instead. > And what about `$a = null; $a++;` / `$a = null; ++$a;`, which currently has behaviour consistent with other arithmetic operations (in particular, is identical to $a+=1) and this RFC does not propose to change? If we change the ++ case to throw a TypeError, would we also change null+1 to throw one? And would we then also examine all the other operators and utility functions which silently coerce null to zero or an empty string? I'd rather not go down that rabbit hole to fix something which is seemingly just a bug in the implementation. If an RFC passes that makes all arithmetic on NULL an error in PHP 8.0 (i.e. with no deprecation period), my RFC will be redundant; but that seems unlikely. I am less precious about the changes to boolean, which is why I propose three separate votes. I would be open to an alternative RFC removing all implicit casts on boolean, object, and resource; but unless and until undefined variable behaviour changes, null is necessarily a special case. Regards, -- Rowan Tommins [IMSoP] --000000000000580e78059fe02818--