Static method `new` is unsupported in generics

Open
#973 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, php
Domain
compilers

Research direction

Reproduce the issue with the PHP generic example and inspect the parser path reported in gentree.cpp:1333 and gentree.cpp:1641. Compare handling of $t::new() with the later ::abc() diagnostic from replace-extern-func-calls.cpp:91. Done means the generic class-string example compiles while unresolved methods still produce the expected error.

Written by the indexing model from the issue text.

Description

<?php

class Foo {
}

_main();

function _main() {
  new_t(Foo::class);
}

/**
 * @kphp-generic T
 * @param class-string<T> $t
 */
function new_t($t) {
  $t::new();
}

fails to compile with following errors:

Compilation error at stage: Parse file, gen by gentree.cpp:1333
  4dbee3a5.php:18  in new_t<T>
    $t::new();

Unrecognized syntax after '$t::'


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compilation error at stage: Parse file, gen by gentree.cpp:1641
  4dbee3a5.php:18  in new_t<T>
    $t::new();

Failed to parse statement. Expected `;`

Changing call of ::new() to ::abc() gives correct error:

Compilation error at stage: Check func calls and vararg, gen by replace-extern-func-calls.cpp:91
  dd8b4e3e.php:18  in new_t<Foo> (instantiated at dd8b4e3e.php:10)
    $t::abc();

Syntax '$class_name::method()' not resolved: method abc not found in class Foo
Dominant language
C++
Stars
1.5k
Forks
116
Avg merge
5d 17h
Merged PRs (30d)
11

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from VKCOM/kphp

All issues in VKCOM/kphp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.