Re: how to add annotations to a C extension function

[email protected] ("Christoph M. Becker")
Newsgroups php.general
Message-ID <[email protected]>
On 08.01.2022 at 09:12, Brett McBride wrote:

> This is a php-internals question.

And as such would better be asked on the internals mailing list, or
maybe on the PECL developers list[2]. :)

> I'm trying to fix some php8.1 deprecation notices in a C extension
> (google/protobuf). They mostly relate to ArrayAccess and Iterator now
> requiring return types:
>
> Deprecated: Return type of
> Google\Protobuf\Internal\RepeatedField::offsetGet($index) should either be
> compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the
> #[\ReturnTypeWillChange] attribute should be used to temporarily suppress
> the notice in Unknown on line 0
>
> I've been able to fix the methods that require standard return types (int,
> void, etc), via ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX macro.

Consider to use stub files, instead of maintaining the arginfo manually.
 As of PHP 8.0.0, there is build/gen_stub.php which allows to generate
arginfo.h files from the stubs.  See
<https://github.com/laruence/yac/blob/master/yac.stub.php> for an
example.  Note that the generate-legacy-arginfo annotation causes a PHP
7 compatible arginfo.h to be generated as well.

> The ones I can't fix (due to needing to maintain BC to php7.0) involve the
> "mixed" type, so as an interim solution I want to add the
> #[\ReturnTypeWillChange]
> annotation to some methods, but I can't find a way to do that.

Frankly, I don't know.  But given how that is checked[3], I think that
it could be possible to call zend_add_attribute()[4] during MINIT to add
the attribute the methods.

> This is cross-posted on stack overflow, if anybody is after points:
> https://stackoverflow.com/questions/70620027/how-to-create-a-php-extension-function-with-a-return-type-hint-plus-annotation

[1] <https://www.php.net/mailing-lists.php#internals>
[2] <https://pecl.php.net/support.php>
[3]
<https://github.com/php/php-src/blob/php-8.1.1/Zend/zend_inheritance.c#L991-L995>
[4]
<https://github.com/php/php-src/blob/php-8.1.1/Zend/zend_attributes.h#L80-L82>

Christoph
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.