Implementation ideas for adding early warning for mis-use of "parent"?

php.internals

Levi Morrison

8 years ago
This code the illustrates the issue; it has no errors or warnings: ```php class A { function m(): parent { return $this; } } ``` However, if you run the method you will get an error. I want to add a light warning such as E_STRICT or E_DEPRECATED at compile time. Using CG(active_class_entry)->parent will not work at compile time, so that strategy is out. Given that `zend_compile_func_decl` is not called directly from `zend_compile_class_decl` it would be difficult to pass the parent's name in any way other than a compiler global. Is there anything in particular I need to do if I add another compiler global? I intend to set and unset it in `zend_compile_class_decl`. Anyone have a better way to pass around the parent's name at compile time?

Nikita Popov

8 years ago
On Sun, Jul 22, 2018 at 2:05 AM, Levi Morrison <levim@php.net> wrote:
> This code the illustrates the issue; it has no errors or warnings: > > ```php > class A { > function m(): parent { > return $this; > } > } > ``` > > However, if you run the method you will get an error. I want to add a > light warning such as E_STRICT or E_DEPRECATED at compile time. > > Using CG(active_class_entry)->parent will not work at compile time, so > that strategy is out. > > Given that `zend_compile_func_decl` is not called directly from > `zend_compile_class_decl` it would be difficult to pass the parent's > name in any way other than a compiler global. Is there anything in > particular I need to do if I add another compiler global? I intend to > set and unset it in `zend_compile_class_decl`. > > Anyone have a better way to pass around the parent's name at compile time? >
Please note that for traits and closures it's not possible to determine this at compile time. Nikita

Levi Morrison

8 years ago
On Sun, Jul 22, 2018 at 2:36 AM Nikita Popov <nikita.ppv@gmail.com> wrote:
> > On Sun, Jul 22, 2018 at 2:05 AM, Levi Morrison <levim@php.net> wrote: >> >> This code the illustrates the issue; it has no errors or warnings: >> >> ```php >> class A { >> function m(): parent { >> return $this; >> } >> } >> ``` >> >> However, if you run the method you will get an error. I want to add a >> light warning such as E_STRICT or E_DEPRECATED at compile time. >> >> Using CG(active_class_entry)->parent will not work at compile time, so >> that strategy is out. >> >> Given that `zend_compile_func_decl` is not called directly from >> `zend_compile_class_decl` it would be difficult to pass the parent's >> name in any way other than a compiler global. Is there anything in >> particular I need to do if I add another compiler global? I intend to >> set and unset it in `zend_compile_class_decl`. >> >> Anyone have a better way to pass around the parent's name at compile time? > > > Please note that for traits and closures it's not possible to determine this at compile time. > > Nikita
Thanks for the note. Fortunately I'm already aware of `zend_is_scope_known`. Is another compiler global the best way to add this info? Any gotchas with adding another one?

Stas Malyshev

8 years ago
Hi!
> Please note that for traits and closures it's not possible to determine > this at compile time.
Why not for closures? I thought closures are bound at definition site, and thus should know everything at compile time?
-- Stas Malyshev smalyshev@gmail.com

Levi Morrison

8 years ago
On Tue, Jul 24, 2018 at 1:15 AM Stanislav Malyshev <smalyshev@gmail.com> wrote:
> > Hi! > > > Please note that for traits and closures it's not possible to determine > > this at compile time. > > Why not for closures? I thought closures are bound at definition site, > and thus should know everything at compile time? > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
Closures can be rebound to another scope; see [Closure::bindTo][1]'s second parameter. I have a [pull request][2] that adds a compile-time warning for usages of "parent" without a parent. It need a review; there is a case where the warning is emitted twice and I don't know what to do. [1]: https://secure.php.net/manual/en/closure.bindto.php [2]: https://github.com/php/php-src/pull/3404