Exhaustive `constexpr` for `switch`

Open
#787 0 comments 0 reactions 1 assignee View on GitHub

@tolk-vm is already working on this.

Since Mar 16, 2023.

Assessment

This issue has not been assessed yet.

Description

constexpr for switch was supported in #727 and #770.

The problem

Consider following snippet

f(new Foo());

/**
 * @kphp-generic T
 * @param T $o
 */
function f($o) {
  switch (classof($foo)) {
    case Bar::class:
      // doing some work
      break;
    
    // No case for Foo::class.
    // No `default` too.
    // We have completely forgotten about Foo :(
  }
}

Now, trying to compile this code and... everything compiles just fine. For Foo we'll get following codegen:

  static_cast<void>(v$matched_with_one_case$ub9cfc05ca56516ed_0);
  do {
    v$condition_on_switch$ub9cfc05ca56516ed_0 = v$const_string$us2098c7d17029d8a0;
    v$matched_with_one_case$ub9cfc05ca56516ed_0 = false;
    {
    }
  } while(false);
  ;

That's it, this is basically "Do nothing for Foo::class". No errors, no warnings.

Solution

Require to cover all T in switch OR default branch, fail compilation otherwise (just like in Rust).

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.