Allow dynamic properties (e.g. endLineno) without deprecation notice in ast\Node - #217
Merged
Merged
Conversation
nikic
reviewed
Nov 27, 2021
| ast_declare_property(ast_node_ce, AST_STR(str_children), &zv_null); | ||
| #ifdef ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES | ||
| zend_add_class_attribute(ast_node_ce, zend_ce_allow_dynamic_properties->name, 0); | ||
| ast_node_ce->ce_flags |= ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES; |
Owner
There was a problem hiding this comment.
Setting the flag explicitly should not be necessary.
Collaborator
Author
There was a problem hiding this comment.
I tried removing the line. Deprecated: Creation of dynamic property ast\Node::$undeclaredDynamic is deprecated in /home/tyson/programming/php-ast/tests/php82_metadata.php on line 18 is the result.
This is the same as what https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions.c does
at the time of writing:
Owner
There was a problem hiding this comment.
Ah yes, looks like the callback doesn't get called on interned classes for some reason.
nikic
reviewed
Nov 27, 2021
…in ast\Node (But use the default deprecation behavior for ast\Metadata) 1. php-ast itself sets the dynamic property on endLineno right now, causing deprecation notices in PHP 8.2. Because all declaration types are associative arrays(not lists), a future AST version number may add this as a property of $node->children instead to avoid this notice. 2. WeakMap is only available in PHP 8.0+ (and WeakReference 7.4), but https://github.com/phan/phan targets PHP 7.2+. 3. Because some nodes make `$node->children` a list, applications such as Phan using php-ast can't associate string keys with that. (e.g. to mark nodes that were already processed in a certain step) https://github.com/nikic/php-parser solves that by adding `attributes`, `setAttribute`, `getAttribute`, etc. separately from children, but php-ast currently doesn't have an attributes node. That may be worth it in a future release. 4. When processing a large number of ast nodes in a large number of files, the deprecation notices have a noticeable performance impact, even when suppressed. Related to nikic#214
TysonAndre
force-pushed
the
allow-dynamic-properties
branch
from
November 27, 2021 15:16
7001c93 to
d246ddd
Compare
nikic
approved these changes
Nov 27, 2021
nikic
left a comment
Owner
There was a problem hiding this comment.
Fine by me, allowing custom node extensions seems like a pragmatic choice.
TysonAndre
added a commit
to TysonAndre/php-ast
that referenced
this pull request
Nov 27, 2021
Make it possible to test with php 8.2 without deprecation notes for endLineno, etc. See nikic#217 Also, document how to properly update cache slot numbers if new properties are added to ast\Node in the future.
TysonAndre
added a commit
that referenced
this pull request
Nov 27, 2021
* Update php versions tested in CI PHP '8.1' isn't published yet on docker hub. Start testing with php 8.1 in appveyor * Prepare for a 1.0.15 release Make it possible to test with php 8.2 without deprecation notes for endLineno, etc. See #217 Also, document how to properly update cache slot numbers if new properties are added to ast\Node in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(But use the default deprecation behavior for ast\Metadata)
php-ast itself sets the dynamic property on endLineno right now,
causing deprecation notices in PHP 8.2.
Because all declaration types are associative arrays(not lists),
a future AST version number may add this as a property of
$node->children instead to avoid this notice.
WeakMap is only available in PHP 8.0+ (and WeakReference 7.4),
but https://github.com/phan/phan supports PHP 7.2+.
Having a Phan version (or external plugins) that can work (and be fast) with a wider range of php versions is useful for organizations trying to detect uses of undeclared properties that need to migrate.
Because some nodes make
$node->childrena list,applications such as Phan using php-ast can't associate string keys
with that.
(e.g. to mark nodes that were already processed in a certain step)
https://github.com/nikic/php-parser solves that by adding
attributes,setAttribute,getAttribute, etc. separately fromchildren, but php-ast currently doesn't currently have an attributes node.
That may be useful to add (and to remove the AllowsDynamicProperty annotation in an incompatible php-ast major release)
That may be worth it in a future release.
When processing a large number of ast nodes in a large number of
files, the deprecation notices have a noticeable performance impact,
even when suppressed.
Related to #214
Phan has a lot of uses of dynamic properties - e.g.
?:syntax, trailing commas).