Skip to content

Commit

Permalink
resolve classes from @param phpdoc (#1015)
Browse files Browse the repository at this point in the history
Purpose: when a class occurs only in @param and is actually never created, 
resolve it in CollectRequiredPass.
Previously, @param weren't analyzed there, because phpdocs haven't been 
parsed up to that execution point, and I didn't want to perform parsing twice.
  • Loading branch information
tolk-vm authored Jun 18, 2024
1 parent b20aead commit b442725
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions compiler/pipes/collect-required-and-classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,23 @@ class CollectRequiredPass final : public FunctionPassBase {
}
}

// Collect classes only from type hints in PHP code, as phpdocs @param/@return haven't been parsed up to this execution point.
// TODO: shall we fix this?
// Collect classes from @param and type hints
inline void require_all_classes_from_func_declaration(FunctionPtr f) {
// note, that @param has't been parsed up to this execution point, so parsing is done twice
// (here and later in ParseAndApplyPhpdocF), it's very ugly, since parsing is quite a heavy operation
// ideally, types in phpdoc should be parsed once in gentree, but actually,
// vkcom has so much syntax-invalid phpdocs, that it's unavailable
// (that "invalid" functions aren't reachable in fact, they just exist in a dead codebase,
// so their phpdocs aren't analyzed later, but trying to parse them in gentree leads to 10k errors)
if (f->phpdoc && !f->is_lambda()) {
for (const PhpDocTag &tag : f->phpdoc->tags) {
if (tag.type == PhpDocType::param) {
if (auto tag_parsed = tag.value_as_type_and_var_name(current_function, current_function->genericTs)) {
require_all_classes_in_phpdoc_type(tag_parsed.type_hint);
}
}
}
}
for (const auto &p: f->get_params()) {
if (p.as<op_func_param>()->type_hint) {
require_all_classes_in_phpdoc_type(p.as<op_func_param>()->type_hint);
Expand Down
8 changes: 8 additions & 0 deletions tests/phpt/cl/030_resolve_from_phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ function f2() {
else echo "a null\n";
}

/**
* @param ?\Classes\Z3Infer $b
*/
function f3($b) {
if ($b) $b->thisHasInfer(1,2);
}

f1(null);
f2();
f3(null);
(new BB)->f();

0 comments on commit b442725

Please sign in to comment.