mago icon indicating copy to clipboard operation
mago copied to clipboard

False positive error[template-constraint-violation]

Open mathroc opened this issue 1 month ago • 0 comments

🐞 Describe the Bug

Getting a error[template-constraint-violation] false positive in the example below

🔄 Steps to Reproduce

  • Run mago analyse with the (default) config and sample below
  • the following error appears:
error[template-constraint-violation]: Argument type mismatch for template `S2`.
   ┌─ src/index.php:16:15
   │
16 │     $i->f($b, false);
   │               ^^^^^ This argument has type `false`, which is not compatible with the required template constraint `('S1.i::f() extends scalar)`.
   │
   = Template parameter `S2` is constrained with `('S1.i::f() extends scalar)`.

but the code is correct afaict

⚙️ Configuration (mago.toml)

# Welcome to Mago!
# For full documentation, see https://mago.carthage.software/tools/overview
php-version = "8.5.0"

[source]
paths = ["src"]
includes = ["vendor"]
excludes = []

[formatter]
print-width = 120
tab-width = 4
use-tabs = false

[linter]
integrations = []

[linter.rules]
ambiguous-function-call = { enabled = false }
literal-named-argument = { enabled = false }
halstead = { effort-threshold = 7000 }

[analyzer]
find-unused-definitions = true
find-unused-expressions = false
analyze-dead-code = false
check-throws = true
allow-possibly-undefined-array-keys = true
perform-heuristic-checks = true

📜 Command Output

$ MAGO_LOG=trace ./mago analyse
DEBUG Sourcing configuration from /home/dev/mago.toml.
DEBUG Configuration specifies 16 threads.
DEBUG Configuration specifies a stack size of 12582912 bytes.
error[template-constraint-violation]: Argument type mismatch for template `S2`.
   ┌─ src/index.php:16:15
   │
16 │     $i->f($b, false);
   │               ^^^^^ This argument has type `false`, which is not compatible with the required template constraint `('S1.i::f() extends scalar)`.
   │
   = Template parameter `S2` is constrained with `('S1.i::f() extends scalar)`.
   = Help: Ensure the argument's type satisfies the template constraint.

error: found 1 issues: 1 error(s)

📂 PHP Code Sample (If Applicable)

<?php declare(strict_types=1);

interface I
{
    /**
     * @template S1 of scalar
     * @template S2 of S1
     * @param S1 $s1
     * @param S2 $s2
     */
    function f(mixed $s1, mixed $s2): void;
}

function f(I $i, bool $b): void
{
    $i->f($b, false);
}

🖥️ Operating System

Linux

📦 How did you install Mago?

Installation Script (curl | bash)

📝 Additional Context

the goal in the example is to indicate that the type of $s2 should be a subtype of $s1 (which is itself a subtype of scalar)

mathroc avatar Nov 28 '25 12:11 mathroc