Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113812 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41119 invoked from network); 27 Mar 2021 16:08:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Mar 2021 16:08:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7640D1804D3 for ; Sat, 27 Mar 2021 09:05:22 -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,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (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 ; Sat, 27 Mar 2021 09:05:22 -0700 (PDT) Received: by mail-ed1-f51.google.com with SMTP id x21so9650296eds.4 for ; Sat, 27 Mar 2021 09:05:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=DZXWf5ac032hQLLid5iu/jrqkweSHI193ZUbNOxUjls=; b=bDqRo7gAEAjeGIxY4dt4fHmPjKTmkzuSpp6SBmniGw2h/SJItc5DyF1/plAeeQgc17 Lwk+f0/KjTJoB1og/VPQLpGAiVWEflyTQTQ8KnoWVNv716CniZj9G5oBHW1EKj6plRIH 9wQFvdM2DavbCsxyqH17fnGcbk80y7PH8z9VIkgGwWtalCWBHkcGfPD/ZidZBUB1dQ3T DXKLjJyDt+wS8LMTA3bf3VT7n+zBJSdPKPoI6hVEYcaWUblcTEp4HUbWb234ENiy5ZDp 6dslV3qjARWbjtw8o4fidcOlvPIU/Q5L/UVrHNYtXmSALRGnzKZs3dQbKkw3wjBZj8U1 IN+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=DZXWf5ac032hQLLid5iu/jrqkweSHI193ZUbNOxUjls=; b=SLDaA3fD922/uob0NsuT4EppWW4nPvDhr/OX65CIiSuy34kcDDmLeR84y4ISPS9+A7 KHjq6U3FQiegc48Omg6tZi5MoKn7uwYnaZqwydS3ufJ2T5ogWx1EOAAYXXTWVk3JzsdL 8BiqhcSXPB+mbQE05PRBSoy2JEd7SgCw5tfDnaORPYz77DuMMjfjhhCzSx7cNf8GKQZN U1joEJD7Osquo66ErAbQFG+WKgzEH5rsOZDCQD32/+y/7Un3bTrayFx1LPIWLOkFlYAm 8mSHByw1eh3N+41nOQBjcBKhhCY5j1z6bGcHbPvOGc9OSrXAIpgk2CeH4xNeaT0Q3C9P gK3A== X-Gm-Message-State: AOAM531hC8wZxavOcfptxTsvc2kjpY6twxamybAnye34jJQa1SIitSzY 2I9Xn3qMqjOQUneHhqGon2U2vsUwU24= X-Google-Smtp-Source: ABdhPJy3HvbThzeYrHioww58UX6GjEoVybFW2bj+BpX9P6u7SOlifq61TIwm82PGJ008MpkT6IJSRA== X-Received: by 2002:a17:907:10c1:: with SMTP id rv1mr21118555ejb.5.1616861110200; Sat, 27 Mar 2021 09:05:10 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id r5sm6114002eds.49.2021.03.27.09.05.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 27 Mar 2021 09:05:09 -0700 (PDT) To: PHP Internals References: <88c9eb5f-f80c-4869-b7f8-1b58b9e2eaa3@www.fastmail.com> <605bae82.1c69fb81.f49f7.d11eSMTPIN_ADDED_MISSING@mx.google.com> <919e30e7-3e5e-d955-7bb4-1e1b5825cdd1@gmail.com> <635DD146-FC6F-4991-8D2C-5A6B492722D5@newclarity.net> <734f12de-da98-6b76-c2fe-8682f4d177aa@gmail.com> <36E45DD6-E2BD-4801-BAAE-4355C83D1AC3@newclarity.net> <15AE4315-A456-4ED8-990A-49EBD76C5B46@newclarity.net> <5501FE70-FBED-47EB-8010-173644BC064F@newclarity.net> Message-ID: <3d453ce7-db16-fb75-91b4-9a2a71994164@gmail.com> Date: Sat, 27 Mar 2021 16:05:08 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] [RFC] Auto-capture multi-line closures and shortfunctions take 2 From: rowan.collins@gmail.com (Rowan Tommins) On 27/03/2021 00:05, Nuno Maduro wrote: > > I've just added a few more tests with the exact examples you have > presented in this mail list: > https://github.com/php/php-src/pull/6246/commits/c3a50d671c5d8fa4b775ec67fe77d0cbd5cc8030 > . Hi Nuno, Thanks, I hadn't thought of writing out test cases, that makes a lot of sense; although I now realise my question wasn't very clear. My biggest concern with automatic capture is the potential for *accidentally* capturing variables - that is, intending to introduce a local variable inside the closure, but capturing a variable from the outer scope that happens to have the same name. This is less of an issue with capture by value, but can still mean resources not being freed, e.g. a large array of data not being freed from memory, or an object destructor not executing when expected. This is more likely in PHP than many other languages, because there is no requirement, or even an option, to explicitly declare a local variable in the closure. It's not a new problem, but since single-expression closures are unlikely to use many local variables, it's probably not one that's been given lots of thought. I've written some tests that demonstrate the current behaviour using an object destructor: https://github.com/nunomaduro/php-src/commit/ae18662cc92f5d07520b4574dcae71d38a9e0a41 Based on those, there seems to be no way to prevent a variable being captured, even if its value is immediately discarded each time the closure runs. This may be less than ideal: $results = getLargeReportFromDatabase(); // ... $fn = fn() {     $results = []; // coincidentally the same name, immediately assigned its own value     // ... } unset($results); // does not free the array, because $fn has captured the value I wonder if the capture analysis could be made smarter to make this less likely. PS I'd like to apologise if some of my messages in this thread have come across as harsh or demanding, I do appreciate you bringing up this feature, as I know it's one a lot of people want, even if I'm sceptical. Regards, -- Rowan Tommins [IMSoP]