Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109504 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24392 invoked from network); 2 Apr 2020 10:20:51 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Apr 2020 10:20:51 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2171B18054A for ; Thu, 2 Apr 2020 01:47:29 -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,FREEMAIL_FROM,HTML_MESSAGE, 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: Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 ; Thu, 2 Apr 2020 01:47:28 -0700 (PDT) Received: by mail-lj1-f177.google.com with SMTP id p10so2388616ljn.1 for ; Thu, 02 Apr 2020 01:47:28 -0700 (PDT) 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 :cc; bh=D4EWQ2mLh5kYvPHaIZ0PyYhn6gpWsuw5L3mUlpsMiRk=; b=Ez+1TPjqLd1pU2lHVGAbkB3aHdOSCbVljo0NATSTwYuYAEHIBrFANe3fCWRueSMtJa fNcAZT55llTGrdqIzf0vBFc8VfHAzjuE4ugN703iOCCi90Bu/TvKAqmE6e0g/dD3YOWd 8mAZpBWFtgGRxRw8fJxqaoIKLIx0s021P62NJaFUHNpH6WteYftO3c0YMTHzFFBnUz13 p4nuBIdE0T/4RiWEKpJjmo25niaazTulN7l10z+Hk+971F68TFwJquNxfX8Rg8BFAWEV bQi0FUYNXhrwYnA4lxrtrtf5aayn8CHHhT+TA1AUoG1CkCXnSE7TQ/vZfmnq/Cs70f11 ajJw== 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=D4EWQ2mLh5kYvPHaIZ0PyYhn6gpWsuw5L3mUlpsMiRk=; b=E0Jk1FgYVRkc0SAmhQuaSVcMoPmDRjif1wuJpPnHaEWPXMpGmt0eW7e2CVeUSmgWnD XGs/HsteZfrF5/otNt6Ac4aW/NWc2NgGvetmovczJIcP6CaQmKYa9IAZ3kF9pgGLlAmp k6Xq/iT4tRg35MI0G/N8k2Nf6Sc9wT49RTQhbZG+zW5Cqxs7qD/1BxXJTGZwmHulNl2w 4ul0CkO7WgM657WrXHdYlDSoVST2Qd4RHFdryUGRFPNw7bdSK67n+8tIOoiJa+V19yy2 oaCgyHS5hu3KCw0BMqH30YN1sx2aj7brAT5hakIY9lskMinbnJxbPJOy5h2csEe4b2EA vlTw== X-Gm-Message-State: AGi0PuZbozxghxSN0bVqb2okEiECZyxjsO1gEePkra3WrUe/tT/EQEZ6 yXyrXw2BeCv/CwLOwHUZ+1IoyuuMbORAs5dI+b0= X-Google-Smtp-Source: APiQypLKCdgWM7ZoVi5snwNjNlUu2z9YjdYaJbVXTrVU5ZXEw72EJs2lbYaXV9nUTgFq7dzgzyfI1sDr+51yQjAmeK4= X-Received: by 2002:a2e:7a18:: with SMTP id v24mr1408295ljc.34.1585817244075; Thu, 02 Apr 2020 01:47:24 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 2 Apr 2020 10:47:07 +0200 Message-ID: To: "Christoph M. Becker" Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000830dd705a24ad9d0" Subject: Re: [RFC] Stricter type-checks for arithmetic/bitwise operators From: nikita.ppv@gmail.com (Nikita Popov) --000000000000830dd705a24ad9d0 Content-Type: text/plain; charset="UTF-8" On Thu, Apr 2, 2020 at 10:35 AM Christoph M. Becker wrote: > On 02.04.2020 at 10:14, Nikita Popov wrote: > > > I would like to propose making the use of arithmetic/bitwise operators on > > arrays, resources and (non-overloaded) objects a TypeError exception: > > > > https://wiki.php.net/rfc/arithmetic_operator_type_checks > > > > This is inspired by some of the recent discussions, in particular the > > operator overloading RFC (where the fact that operations on > non-overloaded > > objects still succeed with just a notice was criticized) and the > > increment/decrement RFC, which handles the array case of this proposal > for > > inc/dec only. > > > > I think as-is, this RFC should be completely uncontroversial. However, > > there is an open question on whether we want to make this slightly more > > strict, in order to align the semantics with the rules for weak parameter > > type checks for ints/floats. > > > > If we do that, then this RFC could be a stepping stone towards making > > "implicit" internal casts use the (weak) parameter type checking > semantics > > more generally, which I think would be a good idea. The current explicit > > cast semantics we use everywhere are too forgiving for most circumstances > > (e.g. an array is almost always not a reasonable input where an integer > is > > expected). > > Thanks! Generally I like that very much. I'm not quite sure, though, > what to do with resource to int conversions. These are documented to be > valid[1], and are sometimes deliberately used[2], and as such it might > be reasonable not to throw on these conversions (maybe even change the > current behavior of the ~ operator). > While resource to int conversions are indeed valid, they are not exactly commonly used, and if they are used, I would expect an explicit (int) cast to be involved. I very seriously doubt that someone writing "$x = $y / 2" really intended this to mean "$x = resource_id($y) / 2" -- it is much more likely that a variable go mixed up. In the unlikely case that that they really did intend that, the option of writing "$resourceId = (int) $y; $x = $resourceId / 2" remains, and is much clearer. On that note, I do wonder whether we should introduce a function like get_resource_id(), that makes it more explicit that an (int) cast is supposed to fetch a resource ID. This is not a very common operation, and I suspect that many PHP programmers may not be familiar with the semantics of integer casts on resources. Using get_resource_id($file) would make the intent clearer relative to (int) $file. This is similar to the recently added get_mangled_object_vars() function, which essentially does the same as (array) $object, but makes it obvious that you really are interested in mangled properties. > After all, I hope we will be able > to port all resources to objects sometime, and till then we could stick > with more lax semantics. I think that's really an argument to align the behavior of resources and objects, as it means one less BC break when resources are converted to objects. Historically that's something we did in minor versions, so it's good to reduce the amount of semantics that change when such a conversion is done. Regards, Nikita --000000000000830dd705a24ad9d0--