8000 Allow trailing comma for function use and parameter lists by mkornaukhov03 · Pull Request #664 · VKCOM/kphp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow trailing comma for function use and parameter lists #664

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include "compiler/data/function-data.h"
#include "compiler/data/lib-data.h"
#include "compiler/data/src-file.h"
#include "compiler/data/vertex-adaptor.h"
#include "compiler/data/generics-mixins.h"
#include "compiler/lambda-utils.h"
#include "compiler/lexer.h"
#include "compiler/name-gen.h"
#include "compiler/phpdoc.h"
#include "compiler/stage.h"
#include "compiler/token.h"
#include "compiler/type-hint.h"
#include "compiler/utils/string-utils.h"
#include "compiler/vertex.h"
Expand Down Expand Up @@ -97,6 +99,9 @@ VertexPtr GenTree::get_foreach_value() {
}

VertexAdaptor<op_var> GenTree::get_function_use_var_name_ref() {
if (cur->type() == tok_clpar) { // for trailing comma
return {};
}
auto result = get_var_name_ref();
kphp_error(result, fmt_format("function use list: expected varname, found {}", cur->str_val));
kphp_error(!result->ref_flag, "references to variables in `use` block are forbidden in lambdas");
Expand Down Expand Up @@ -1377,7 +1382,7 @@ bool GenTree::parse_cur_function_uses() {
}

std::vector<VertexAdaptor<op_var>> uses_as_vars;
gen_list<op_err>(&uses_as_vars, &GenTree::get_function_use_var_name_ref, tok_comma);
gen_list<op_none>(&uses_as_vars, &GenTree::get_function_use_var_name_ref, tok_comma);

for (auto &v : uses_as_vars) {
cur_function->uses_list.emplace_front(v); // the order — use($v1, $v2) or use($v2, $v1) — doesn't matter
Expand Down Expand Up @@ -1494,7 +1499,7 @@ VertexAdaptor<op_func_param_list> GenTree::parse_cur_function_param_list() {
params_next.emplace_back(ClassData::gen_param_this(auto_location()));
}

bool ok_params_next = gen_list<op_err>(&params_next, &GenTree::get_func_param, tok_comma);
bool ok_params_next = gen_list<op_none>(&params_next, &GenTree::get_func_param, tok_comma);
CE(!kphp_error(ok_params_next, "Failed to parse function params"));
CE(expect(tok_clpar, "')'"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok
@ok php8
<?php

// See https://wiki.php.net/rfc/trailing-comma-function-calls
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@kphp_should_fail
@ok php8
<?php

function f($x, $y,) {} // Trailing comma is not allowed in decls
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@ok php8
<?php

require_once 'kphp_tester_include.php';

function foo(
$arg,
$arg2, // Ok
) {
echo $arg . $arg2 . "\n";
}

foo("vk", "com");
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@ok php8
<?php

require_once 'kphp_tester_include.php';

$a = 'test1';
$b = 'test2';
$fn = function () use (
$a,
$b,
) {
echo $a, $b;
};

$fn();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@kphp_should_fail php8
<?php

function foo(
$arg,
$arg2,, // Multiple trailing commas are not allowed
) {
echo $arg . $arg2 . "\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@kphp_should_fail php8
<?php

function foo(,) { // Free-standing commas are not allowed
echo $arg . "\n";
}
0