Skip to content

Fix handling of named label for goto. #345

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

Merged
merged 1 commit into from
Sep 13, 2020

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Sep 13, 2020

The label is a standalone statement just like any other statement.
It isn't associated with the next statement by PHP's parser.

E.g. while (false) label: echo "test\n"; echoes "test".

Fixes #154

@TysonAndre
Copy link
Contributor Author

TysonAndre commented Sep 13, 2020

Actually, I think it's incorrect for a goto label to be associated with a statement.

php > while(false) label: echo "test";
test

That example gets parsed equivalently to while(false) { label: } echo "test"; by php, but tolerant-php-parser incorrectly treats it as while (false) {label: echo "test"}

Looking at the php-src parser implementation, a goto label is a standalone statement just like any other statement

	|	';'	/* empty statement */ { $$ = NULL; }
	|	T_TRY '{' inner_statement_list '}' catch_list finally_statement
			{ $$ = zend_ast_create(ZEND_AST_TRY, $3, $5, $6); }
	|	T_GOTO T_STRING ';' { $$ = zend_ast_create(ZEND_AST_GOTO, $2); }
	|	T_STRING ':' { $$ = zend_ast_create(ZEND_AST_LABEL, $1); }
;

EDIT: The php-src implementation is also consistent with the documented https://github.com/php/php-langspec/blob/master/spec/11-statements.md#grammar-named-label-statement specification

php-language-server doesn't seem to be affected by this change because NamedLabelStatement is handled like any other node - it should fix the bugs mentioned in the linked ticket
.
Phan hardcodes an exact version (e.g. "0.0.22", so it is also unaffected because it'll be fixed when the release is manually updated)

The label is a standalone statement just like any other statement.
It isn't associated with the next statement by PHP's parser.

E.g. `while (false) label: echo "test\n";` echoes "test".

Fixes microsoft#154
@TysonAndre TysonAndre changed the title Check if goto label has no corresponding statement Fix handling of named label for goto. Sep 13, 2020
Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I wouldn't have expected that behavior.

@roblourens roblourens merged commit 1d76657 into microsoft:master Sep 13, 2020
@TysonAndre TysonAndre deleted the goto-empty branch March 27, 2021 16:50
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.

goto parsing fails with empty target
2 participants