Skip to content

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

Open
@nielsdos

Description

@nielsdos

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 krakjoe/pcov#87

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

cc @iluuu1994

PHP Version

8.3+

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions