Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126380 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 214F71A00BC for ; Wed, 12 Feb 2025 16:43:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739378423; bh=vjunFJOeQkc7wV3U9fB8Po5v0AJbp2gUhovsOOKE20g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=iOmOtdlQZbl4sWX8T6aqVqAuY4sp5bi3hlMHQAZDQdNcz1KogGrDLtDF/nTfdq450 9jNwAyb4MRsp7E4uPOs+unWk3VjXMgAB5VFWy+uhCj+cCKtSGDygWysenE4GKQYxgR AnMDJ9+W/DXnNs3tpXcgkCn1VhFRC/EYnMwBe4uY/xpaykjJjPHCVuhh2GH5gy8cCj q2ZHq7yWHXLCLnb5ikm8leXUU5VHq0Ht3HrpxHV+tD7LYYgILZT6H5iiRrjJW7bFYp we0UHt0xaPCWYWlhJQ5OvYhX+kq6Cyyk6TSEmeBZddPbrQehTIDzHZBjwrd3nJ7z3e mvxmmr807vVhA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BB703180034 for ; Wed, 12 Feb 2025 16:40:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 12 Feb 2025 16:40:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1739378583; bh=2yeoj85/JGJLDIjrH09HDv/F5+GRsUfb1MjC5SWgwWg=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=hqDfVoFunw0c7iOlp10oIe1IfgNfqLxm0u62kNDd1rpFe95WQZoduiVuiWTLyr88k fLKBPUBX+XbT5vghfktuiqLmTjZbOpK3eB6pr+dEzG8UfHOXpkmBLPiRgkQug7F4pg AO2ftfKltdIUcsajVmCqxFyEu+2Bx+ETgndrfR8UFsLD/p1S9jeNd4NdqtYuwzTgYa TCJHX2FYruvfRjjexLpL/C3iH7muylbS6UqXroyADzdl1Xr7XSkbZBBOcN3AzclLMk K5X87AefR8iEXFDyLzTWaW/p4MDBlDOLkazGTxanwTjLIQ/q1c4Q/dN2zdOKi5YfjK 9CasxbJ12rgiw== Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 12 Feb 2025 17:43:03 +0100 To: Kamil Tekiela Cc: Volker Dusch , php internals Subject: Re: [PHP-DEV] RFC: Marking return values as important (#[\NoDiscard]) In-Reply-To: References: Message-ID: <95a69ca549b246c3645f8c6db519f669@bastelstu.be> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi Am 2025-02-12 15:07, schrieb Kamil Tekiela: > I'd be honest, I have a very negative attitude towards this proposal > and I'd be voting against it. It seems to me like it's creating a > problem and then trying to find a solution for it. Given that even Rust with a modern stdlib and ergonomic language features has such an attribute, we do not believe it's an invented problem. With the `bulk_process()` example, we also tried to provide coherent user-story with a use-case that the readers might've already encountered themselves in the same or a similar fashion. Personally I encountered situations where I wished such an attribute would've warned me both in PHP and JavaScript/TypeScript. Also in C of course, but given that C doesn't have Exceptions, the situation is somewhat different. For an additional example use-case, consider a function returning a resource object with side-effects which will automatically clean up the resource when being destructed. Specifically a function to spawn a new process or thread: class Process { public function __destruct() { /* kill the process */ } } #[NoDiscard("the process will be terminated when the Process object goes out of scope")] function start_process(string $executable): Process { return new Process($executable); } start_process('/my/background/process'); Depending on timing, the process might or might not run to completion before PHP gets around to kill it, for example when a step debugger is attached to PHP, making the bug disappear when debugging. Related to the above example and also to the `flock()` function: A developer might want to write a locking function that returns a lock resource that will automatically unlock when it goes out of scope, preventing the unlock from being forgotten. It might not be immediately obvious that the locking function returns a lock resource, especially when it's associated with another object: $cacheDriver->lock('someCacheEntry'); Is there a `$cacheDriver->unlock('someCacheEntry')` or will `->lock()` return a lock resource? Having the attribute on the `lock()` method can help. Searching for `language:rust "must_use ="` on GitHub reveals that a common use-case in Rust is lazy iterators, where you can call higher-order functions (e.g. map() or filter()), but they will not do anything unless you explicitly “poll” them at least once. As far as I know, Java has lazy streams that behave identically. > A return value is always supposed to be used. If some API is returning > a value that can be safely ignored, it's a badly designed API. If a We agree that in a greenfield ecosystem, we would make ignoring the return value of a function a warning by default (with an opt-out mechanism), as done in Swift. However in PHP we do not have a greenfield situation, there a number of functions where the return value is useless and only exists for historical reasons. This includes all functions which nowadays have a return-type of just `true`, which was only added for this singular purpose. There's cases where ignoring the return value is safe and reasonable, without being a badly designed API. `array_pop()` would come to mind: If one is only interested in the side-effect of removing the last element in the array, one does not need the return value without necessarily doing something unsafe. > doesn't feel like the solution. In fact, it's creating a problem for > users who want to ignore the value, which you then propose to solve > with (void) cast. Ignoring the return value of functions having the attribute should be a rare occurrence given the intended use-case of pointing out unsafe situations. But as with static analyzers there needs to solution to suppress warnings, after carefully verifying that the warning is not applicable in a given situation. > the compilation phase. In PHP warnings are runtime errors. The code > should emit an exception instead of a warning. It would also make it See Volker's reply to Derick. > much easier to handle and you wouldn't need any special construct to > allow users to ignore the new attribute. And I am really not a fan of Even if an Exception would be thrown, there would be a mechanism to suppress the Exception. A catch-block wouldn't work, because if the Exception is thrown, the function is not executed / control flow is interrupted. > the PHP engine generating E_USER_WARNING which should be reserved only > for warnings triggered by trigger_error. This follows the lead of the `#[\Deprecated]` attribute, which emits `E_USER_DEPRECATED` for userland functions and `E_DEPRECATED` for native functions, despite being triggered by the engine. > The examples you used don't support the need for the new attribute. > Regarding the DateTimeImmutable methods, you said yourself: "The > methods are badly named, because they do not actually set the updated > value". So your proposal suggests adding a brand new thing to PHP to > deal with bad method names? `DateTimeImmutable` is not part of the “Use Cases” section: Our intended use-case is the kind of `bulk_process()` functionality that is used for all the code snippets. But given that the attribute is also useful for `DateTimeImmutable`, we made it part of the proposal, without it being part of the intended “user-story”. > This problem should be solved using static analysers, IDE, and proper > code refactoring. See Volker's reply to Matteo. Best regards Tim Düsterhus