Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113912 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 65947 invoked from network); 1 Apr 2021 15:09:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Apr 2021 15:09:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7E1A51804D0 for ; Thu, 1 Apr 2021 08:07:30 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.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 ; Thu, 1 Apr 2021 08:07:29 -0700 (PDT) Received: by mail-wr1-f48.google.com with SMTP id e18so2159391wrt.6 for ; Thu, 01 Apr 2021 08:07:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beberlei-de.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=CymfnWzYTfd/PVapHZFx3sW8sbtmBnk8LNhuwT/UpAo=; b=TUeBvYU5PuzAw3UHRANI1og9rg9rBa5nwlj3mrEkE3pNf6neHHO5fqK/C+H7jjLJt3 Ay76Czj6phcSZ/4DWHihEgYdflA8mXulyf9npR+MAPG483F7x7EnPiUSOThCtMEvg0Gl rNL5tBMuQDspzDK30EA90d0bvTRBx+9AfEOIVXyRTYGeR5IxTgq35bOCR8SON9VhngrO YrL2ckyRkG9h8BhQINDw358HUV3h8KIruAb9VM0Swe+eFGC3GnZ4qjUAOQmhd6A8k3Ht IgkuRIdb0pPAT79aw4ufA2pZ5xLayuszYJhqSEGqvS2bnGl78orHQA6rwZ1B6mwapQKu N3yQ== 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=CymfnWzYTfd/PVapHZFx3sW8sbtmBnk8LNhuwT/UpAo=; b=sLRsbFfct1tcBaNg7IWYMM0gO0WP7mSt+xQGvnOxGtyWqDDghM/BffhHkHz/q8F3Cr xgsPymesqKC97/5J3HNUm9ktmJyXFCzKdnrUiMkxfdEj+MeUqNd1NNRpstt6Zf/XByuN W2AP7tHtt3r7AV+1tykTkfgJPgB78n6u1ERT/8eqDiLjWaZ5P9biAgH5k8Qfet+FPuNF AVtsfplr5XikApP4c5hr4cGkdYqioDJEzcPkUMPistH1vk+CbcxNaVOorNslknlQOJJ0 +HKdW7BL1FMtfz20yKo+oNAVI7krzZNvCbv5J78dg2TaVmlxwhPqvv/gD4rMeCl29F77 16Tw== X-Gm-Message-State: AOAM530+VnRG76Z350yy3/Kf9uTMnQumxE3tEUnhOR40qf7krka7YleV Ng620u74pKkWU3emcr8S18Wh3kpaieFSMs21HQ5eQQ== X-Google-Smtp-Source: ABdhPJyUKdVcSuPJHjAWzFvzF20YkZVfHgZvSVI/FNNXLF4SxWu6knSxZgGlyjFsvDnhf5h9YZNAAZOzWqWx16JXxuk= X-Received: by 2002:a5d:6152:: with SMTP id y18mr10333956wrt.255.1617289646080; Thu, 01 Apr 2021 08:07:26 -0700 (PDT) MIME-Version: 1.0 References: <7399011c-a638-5363-5303-c01ac402bb92@gmx.net> In-Reply-To: <7399011c-a638-5363-5303-c01ac402bb92@gmx.net> Date: Thu, 1 Apr 2021 17:07:15 +0200 Message-ID: To: Andreas Leathley Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000da98b005beea962f" Subject: Re: [PHP-DEV] [VOTE] noreturn type From: kontakt@beberlei.de (Benjamin Eberlei) --000000000000da98b005beea962f Content-Type: text/plain; charset="UTF-8" On Thu, Apr 1, 2021 at 3:56 PM Andreas Leathley wrote: > On 01.04.21 10:56, Benjamin Eberlei wrote: > > This RFC is using the declaration of the return type, to specify that it > > never returns. As such noreturn or never is a keyword a long the lines of > > public or final, but it is not a return type. > > > > I don't think the argument for potential optimizations in Opcache to > > eliminate dead code or allow IDEs to hint at dead code are valuable > enough > > to justify this change. > > I already use @return no-return (supported by Psalm/PHPStan) in my code > now, to clarify the code flow, and for me it fits perfectly for return > types, as not returning is also a result of the function/method. Having > a return type of "int" or "string" (or even "void") seems misleading > when the method will never return anyway. > > noreturn/never might not be useful in all code, but for some parts like > in a controller of a web application it makes handling redirects, > authorization or 404 errors much easier, clearer and less error-prone, > for example: > > ```php > if (!isset($user)) { > $this->notFound(); // this is a noreturn/never method > } > > if (!$this->isGranted('adminPrivileges')) { > $this->notAuthorized(); // this is a noreturn/never method > } > ``` > > Adding noreturn/never on a language level would make sure calling > "$this->notAuthorized();" never continues code flow. This is often also > a security issue, as seen by an alternative way of handling the above > use cases: > > ```php > if (!isset($user)) { > $this->notFound(); > return; > } > > if (!$this->isGranted('adminPrivileges')) { > $this->notAuthorized(); > return; > } > ``` > > If you forget a return, suddenly you have issues. So even though > noreturn/never is a niche language feature not everyone will use, it > does address a very real use case. I don't know, you are arguing that you forgot a return, but you also did not forget to add a never return type. You could easily fix this by inlining the exception instead. ```php if (!isset($user)) { throw new NotFoundException(); } ``` Even when you insist on the function, it is a one liner that throws immediately. Adding the never return type there isn't going to catch a great many bugs. > Using attributes has the disadvantage > of not making the intent clear: > > ```php > #[\NoReturn] > function notAuthorized(): string > { > // Some code > } > ``` > > Now you have a return type of string, but also the information that the > function will never return anyway. That is confusing, but will be > inevitable in some cases when implementing interfaces or an abstract > class. In my opinion, the string return type is wrong in this case and > can only mislead a developer. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --000000000000da98b005beea962f--