Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111930 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23370 invoked from network); 24 Sep 2020 09:36:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Sep 2020 09:36:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4FE06180531 for ; Thu, 24 Sep 2020 01:47:10 -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-Virus: No X-Envelope-From: Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (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, 24 Sep 2020 01:47:09 -0700 (PDT) Received: by mail-lf1-f43.google.com with SMTP id x69so3012301lff.3 for ; Thu, 24 Sep 2020 01:47:09 -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; bh=kpY+M+7rkYifJ041YZkdEHAoRUaGM11p0mTIUckRwL8=; b=cjT+mbqaaA8jWbAgwA0X3wemUHqQi4iRspd4pJSzxor81rsCKAhPsQekFQFXK8ZjmI gJ02hj282bAaiGGxcIxm/uyHATapzgLVGE+xsDDDDWu048Ig/KdSXk6rn0Kg5w1Wj+GM ek5eS7Ct01p6bV56j7pDsScI1lKLa+5BhRs/sILjXTu1GDhcnljsfsuXox1GNADaFHUv TX0l4QJpkBr22ym2LABcqttCHiMUxnhU/E5F1SaeWn4FgmgLM0rkklWeOxhoZ3zqaNdV sv7NnQGr3WH+JCgg5jNiljiSWAxBsjYE9b78wCM9rkvKkReo4zRG9k/Ysh8f0YmQNOiY s+cg== 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=kpY+M+7rkYifJ041YZkdEHAoRUaGM11p0mTIUckRwL8=; b=mnZzFl6ZaPcnH/n+9iCzvCmXrMb+k/OIR/2yjcJuoS29KTt5YXT5lrWMKJbU6M3DvO Yj75UfPf+bQjhW/Ge2zLx/w4iK7vX0z3JMaBRUO9q3XVh2DWCv66HLb1csMfBcAAAzBB U8Fw3ub+xMU98ZQaPyCZAwLRA5qZ/eb0pQ2F6n3s7EYFfgrY1ZDwiai379YAuauFSC7N /E25B6b3MQSShZrmwcVUG7xrLRJrewk0hyKojTYbkCS+X5AeWnJLFYI3mv5w0WCOIWrB pWfddf9j4rIkB0QcJ5vVogJQNsja300zHjjY5zln/CKluWTiiyfBIHUrey840bYtTtYZ Glsw== X-Gm-Message-State: AOAM5337IhHghpMBvntsGA66aC5TYTPSa6zuC79rQ7x1c/AAg4htZv6W NL9CDIStdX7BT+D39yXpymsbP45yv34fpMHP2gcv9ndJd7h9sivo X-Google-Smtp-Source: ABdhPJx7rWsaQGWSChBi5nh8Mu5UC6F+WMbgmVWe0ITeoTjgHZn0Wgj2qmklUe5mhGyrr4swMxSqNOsqzjUvF18AU2s= X-Received: by 2002:a19:dcb:: with SMTP id 194mr1257357lfn.25.1600937227811; Thu, 24 Sep 2020 01:47:07 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 24 Sep 2020 10:46:52 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000c581ef05b00b3ef2" Subject: Re: Attributes and strict types From: nikita.ppv@gmail.com (Nikita Popov) --000000000000c581ef05b00b3ef2 Content-Type: text/plain; charset="UTF-8" On Tue, Sep 22, 2020 at 2:58 PM Nikita Popov wrote: > Hi internals, > > I would like to clarify how attributes are supposed to interact with > strict_types. Consider the following code, split up into three files to > make it clear that each one could have their own strict_types mode: > > MyAttribute.php > #[Attribute] > class MyAttribute { > public function __construct(string $a) {} > } > > UseOfMyAttribute.php > declare(strict_types=1); > #[MyAttribute(42)] > class Test {} > > AccessOfAttribute.php > var_dump((new > ReflectionClass(Test::class'))->getAttributes()[0]->newInstance()); > > Currently, what happens is that this code will construct the attribute, > converting int 42 into string "42", even though UseOfMyAttribute.php has > strict_types=1. > > My intuitive expectation here would be that we should be following the > strict_types mode of wherever the attribute is being used (i.e. > UseOfMyAttribute.php, not MyAttribute.php or AccessOfAttribute.php). > Currently, we always assume strict_types=0. > > Is my expectation correct? > > Regards, > Nikita > As we seem to have an agreement that strict_types of UseOfMyAttribute.php should be respected, I've created an implementation for this at https://github.com/php/php-src/pull/6201. Partway through I realized that the root problem is really that we want the constructor call to happen "as if" it was caused by the use-site of the attribute. This affects not just strict_types=1, but also backtrace and error information. In particular, this means that the backtrace now also points you to which attribute use-site resulted in an exception, which seems like rather useful information to have, as the same attribute might be used in many places. This does mean we have to store the line number of the attribute internally. We could also expose that information via ReflectionAttribute now, if we wanted. Regards, Nikita --000000000000c581ef05b00b3ef2--