php / php/php-src

Inner expression compilation (for match) can result in wrong line numbers

Open
#18,985 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Category: Engine Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
echo match(15) {
    13 => "A",
    15 => "B",
    default => "C",
};

Resulted in this output:

L0002 0000 MATCH int(15) 13: 0001, 15: 0003, default: 0005
L0003 0001 T1 = QM_ASSIGN string("A")
L0003 0002 JMP 0007
L0004 0003 T1 = QM_ASSIGN string("B")
L0004 0004 JMP 0007
L0005 0005 T1 = QM_ASSIGN string("C")
L0005 0006 JMP 0007
L0005 0007 ECHO T1    <<<<<<<<<<<<<<<<<<<<<<<< line 5 instead of 2!
L0007 0008 RETURN int(1)

But I expected this output instead:

L0002 0000 MATCH int(15) 13: 0001, 15: 0003, default: 0005
L0003 0001 T1 = QM_ASSIGN string("A")
L0003 0002 JMP 0007
L0004 0003 T1 = QM_ASSIGN string("B")
L0004 0004 JMP 0007
L0005 0005 T1 = QM_ASSIGN string("C")
L0005 0006 JMP 0007
L0002 0007 ECHO T1
L0007 0008 RETURN int(1)

The echo is on line 2, not line 5. This is because the line no gets overwritten. There's probably many more of these cases.
Discovered via https://github.com/krakjoe/pcov/issues/87

Possible fix idea (may be incomplete):
https://gist.github.com/nielsdos/fe1a4b30aee84efff23f32dc4b0e5ffe

cc @iluuu1994

PHP Version
8.3+
Operating System

No response

Contributor guide

Open the contributing guide

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.

Research direction

Start by reproducing the shown PHP 8.3+ match expression and inspecting the inner-expression compilation behavior described in the issue. Compare the generated output with the expected line numbers and use the linked possible fix as context; done means the ECHO instruction reports line 2 rather than line 5 without regressing the other displayed instructions.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.