Skip to content

[Bug] preg_replace_callback callback parameter not recognized as array #183

@nahime0

Description

@nahime0

Description

When using preg_replace_callback, the callback receives a parameter that the type checker does not recognize as an array. This causes errors such as "Cannot index non-array" inside the callback body.

Reproduction

$result = preg_replace_callback(
    '/(\d+)/',
    function($matches) {
        return '[' . $matches[0] . ']';   // Type error on $matches[0]
    },
    'price: 123 and 456'
);

Expected behavior

The code compiles and runs. $matches is a standard PCRE matches array ($matches[0] = full match, $matches[1] = first capture, etc.).

Actual behavior

Compile error: Cannot index non-array

Environment

elephc 0.21.8

Possible root cause

The builtin signature and/or type inference for preg_replace_callback does not correctly model the type of the array passed to the callback (it should be array<int, string> or array<string> depending on implementation).

See:

  • src/types/checker/builtins/ (regex / preg category)
  • src/types/signatures.rs

Additional context

Part of Round 2 preg stress testing.

This makes a very common PHP pattern (preg_replace_callback) unusable without casts or workarounds.

Suggested labels: bug, php-compatibility, type-checker, regex

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingphp-compatibilityPHP compatibility / deviation from PHP behaviorregexRegular expressions and preg_* functionstype-checkerStatic type checker / inference issues

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions