Skip to content
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 @@ -15,11 +15,13 @@
#include "compiler/data/lib-data.h"
#include "compiler/data/src-file.h"
#include "compiler/data/generics-mixins.h"
#include "compiler/data/vertex-adaptor.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 {};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems strange, because get_func_param() and other list-item parsing function don't handle ')' separately

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";
}