Skip to content
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

allowed catch without variable #309

Open
wants to merge 1 commit 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
20 changes: 18 additions & 2 deletions compiler/code-gen/vertex-compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,15 @@ void compile_try(VertexAdaptor<op_try> root, CodeGenerator &W) {
if (!is_first_catch) {
W << "else " << BEGIN << NL;
}
move_exception(caught_class, catch_op->var());

// if 'catch' is used without a variable, then
// the current exception can simply be destroyed
if (catch_op->without_variable) {
W << "CurException.destroy();" << NL;
} else {
move_exception(caught_class, catch_op->var());
}

W << catch_op->cmd() << NL;
if (!is_first_catch) {
W << END << NL;
Expand All @@ -277,7 +285,15 @@ void compile_try(VertexAdaptor<op_try> root, CodeGenerator &W) {
}
std::string e = gen_unique_name("e");
W << BEGIN;
move_exception(caught_class, catch_op->var());

// if 'catch' is used without a variable, then
// the current exception can simply be destroyed
if (catch_op->without_variable) {
W << "CurException.destroy();" << NL;
} else {
move_exception(caught_class, catch_op->var());
}

W << catch_op->cmd() << NL << END << NL;
}

Expand Down
10 changes: 9 additions & 1 deletion compiler/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,14 @@ VertexAdaptor<op_catch> GenTree::get_catch() {
auto exception_class = cur->str_val;
CE (expect(tok_func_name, "type that implements Throwable"));
auto exception_var_name = get_expression();
CE (!kphp_error(exception_var_name, "Cannot parse catch"));
auto without_variable = false;

// if 'catch' without variable
if (!exception_var_name) {
exception_var_name = create_superlocal_var("catch_");
without_variable = true;
}

CE (!kphp_error(exception_var_name->type() == op_var, "Expected variable name in 'catch'"));

CE (expect(tok_clpar, "')'"));
Expand All @@ -2008,6 +2015,7 @@ VertexAdaptor<op_catch> GenTree::get_catch() {

auto catch_op = VertexAdaptor<op_catch>::create(exception_var_name.as<op_var>(), embrace(catch_body));
catch_op->type_declaration = resolve_uses(cur_function, static_cast<std::string>(exception_class), '\\');
catch_op->without_variable = without_variable;

return catch_op;
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/vertex-desc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,9 @@
},
"exception_class": {
"type": "ClassPtr"
},
"without_variable": {
"type": "bool"
}
},
"props": {
Expand Down
16 changes: 16 additions & 0 deletions tests/phpt/exceptions/18_catch_without_variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@ok php8
<?php

try {
throw new Exception();
} catch (Exception) {
echo "Exception\n";
}

try {
throw new Exception();
} catch (Exception) {
echo "Exception\n";
} catch (Error) {
echo "FAIL\n";
}