Skip to content

Fix bug preventing parsing attributes on AST_CLASS #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

TysonAndre
Copy link
Collaborator

@TysonAndre TysonAndre commented Sep 6, 2020

Unexpectedly, the attributes were found at index 4, not index 3.
Index 3 was always null.
Use a different name to make it obvious if refactoring bugs cause the
placeholder index to have child nodes get unintentionally emitted.

Tests pass locally in 8.0, and in 7.0-7.4 in travis

Unexpectedly, the attributes were found at index 4, not index 3.
Index 3 was always null.
Use a different name to make it obvious if refactoring bugs cause the
placeholder index to have child nodes get unintentionally emitted.
@nikic
Copy link
Owner

nikic commented Sep 6, 2020

Hm, I'm thinking it would make more sense to change the structure in php-src. All the function types have consistent AST structure, but there's no reason at all why classes should be consistent with functions.

@TysonAndre
Copy link
Collaborator Author

Hm, I'm thinking it would make more sense to change the structure in php-src. All the function types have consistent AST structure, but there's no reason at all why classes should be consistent with functions.

That's probably also fine with me, extensions that use the ast output other than this should be rare.
I didn't know if there were other reasons, and didn't know if there'd be objections to changing the internal structure after the feature freeze

@TysonAndre
Copy link
Collaborator Author

It doesn't actually save any memory, apparently, but is easy enough to move to position 3

ZEND_API zend_ast *zend_ast_create_decl(
	zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
	zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
) {
	zend_ast_decl *ast;

	ast = zend_ast_alloc(sizeof(zend_ast_decl));
	ast->kind = kind;
	ast->attr = 0;
	ast->start_lineno = start_lineno;
	ast->end_lineno = CG(zend_lineno);
	ast->flags = flags;
	ast->lex_pos = LANG_SCNG(yy_text);
	ast->doc_comment = doc_comment;
	ast->name = name;
	ast->child[0] = child0;
	ast->child[1] = child1;
	ast->child[2] = child2;
	ast->child[3] = child3;
	ast->child[4] = child4;

	return (zend_ast *) ast;
}
/* Separate structure for function and class declaration, as they need extra information. */
typedef struct _zend_ast_decl {
	zend_ast_kind kind;
	zend_ast_attr attr; /* Unused - for structure compatibility */
	uint32_t start_lineno;
	uint32_t end_lineno;
	uint32_t flags;
	unsigned char *lex_pos;
	zend_string *doc_comment;
	zend_string *name;
	zend_ast *child[5];
} zend_ast_decl;

TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Sep 6, 2020
See nikic/php-ast#181

> Hm, I'm thinking it would make more sense to change the structure in php-src.
> All the function types have consistent AST structure, but there's no reason at
> all why classes should be consistent with functions.

It's unusual to have an unused child node between other child nodes that are
used (for name, extends, implements, and attributes of AST_CLASS)
php-pulls pushed a commit to php/php-src that referenced this pull request Sep 10, 2020
See nikic/php-ast#181

> Hm, I'm thinking it would make more sense to change the structure in php-src.
> All the function types have consistent AST structure, but there's no reason at
> all why classes should be consistent with functions.

It's unusual to have an unused child node between other child nodes that are
used (for name, extends, implements, and attributes of AST_CLASS)

> That gap is a leftover from a previous refactoring. An earlier version of
> attributes extended `zend_ast_decl` with a new member called `attributes` and
> therefore did not need to handle functions and classes in different ways.

Closes GH-6088
@TysonAndre TysonAndre closed this Sep 10, 2020
@TysonAndre TysonAndre deleted the fix-class-attributes-parsing branch September 12, 2020 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants