Skip to content

fix(templates): an item of a template array at its key-of is its value-of - #14586

Merged
orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/14047-key-of-template-offset
Nov 24, 2026
Merged

orklah merged 4 commits into
vimeo:masterfrom
jirkace:fix/14047-key-of-template-offset

Conversation

@jirkace

@jirkace jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Reading a template array T at a key-of<T> offset gives the bound's value type instead of value-of<T>, and key-of<T> is rejected as an array key in a literal.

Before

<?php
/**
 * @template T of array<string, int>
 * @param T $arr
 * @param key-of<T> $k
 * @return value-of<T>
 */
function item(array $arr, $k) {
    return $arr[$k]; // InvalidReturnStatement: The inferred type 'int' does not match the declared return type 'value-of<T:fn-item as array<string, int>>' for item
}

/**
 * @template T of array<string, int>
 * @param key-of<T> $k
 * @param value-of<T> $v
 * @return array<key-of<T>, value-of<T>>
 */
function pair($k, $v): array {
    return [$k => $v]; // InvalidArrayOffset: Cannot create offset of type key-of<T>, expecting array-key
                       // InvalidReturnStatement
}

After

    return $arr[$k]; // no issue
    return [$k => $v]; // no issue

Fixes #14047

…e-of vimeo#14047

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • $arr[$k] with $arr: T and $k: key-of<T> (the same template) is value-of<T>, for a T bound to a generic array, a list or a shape. Before, it was the bound's value type (int, int|string). With a key-of of another template the bound's value type is kept (new invalid case).
  • key-of<T> is accepted as the key of an array literal ([$k => $v] is non-empty-array<key-of<T>, value-of<T>>). Before, it reported InvalidArrayOffset and the key became array-key.
  • Verified: the new valid case fails on master and passes on the branch; the provider cases reading an array at a key-of offset pass; self-analysis reports only the two known environment MixedArguments.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1143592. Configure here.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 749 58.9 $\color{gray}{\textsf{+2.8\%}}$ 25 907 53 460 682
✅ symfony 3 599 216.7 $\color{gray}{\textsf{+0.6\%}}$ 50 610 98 403 1 528
✅ doctrine-orm 380 19.8 $\color{gray}{\textsf{+0.5\%}}$ 3 481 6 143 0
✅ psl 617 25.2 $\color{gray}{\textsf{-0.2\%}}$ 1 375 5 615 1
✅ phpunit 455 19.6 $\color{gray}{\textsf{+0.3\%}}$ 1 731 3 410 18
✅ php-parser 226 13.5 $\color{gray}{\textsf{+1.1\%}}$ 17 154 21 245 0
✅ tempest 895 36.1 $\color{gray}{\textsf{+0.5\%}}$ 5 047 10 528 252
✅ amphp-amp 45 1.2 $\color{gray}{\textsf{+0.8\%}}$ 29 49 0
✅ webmozarts-assert 273 3.6 $\color{gray}{\textsf{+0.8\%}}$ 577 2 720 0
✅ guzzle-promises 44 $\color{gray}{\textsf{+0.1\%}}$ 1.3 $\color{gray}{\textsf{+0.8\%}}$ 155 434 2
✅ monolog 135 4.5 $\color{gray}{\textsf{-2.2\%}}$ 913 2 479 10
✅ drupal 4 842 182.9 $\color{gray}{\textsf{+0.9\%}}$ 97 591 169 293 583
Total 13 260 583.2 $\color{gray}{\textsf{+0.8\%}}$ 204 570 373 779 3 076
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head 91dea62 · base e82bca1 (merge-base) · workflow run

jirkace and others added 3 commits November 22, 2026 00:02
…array

The value-of<T> of an item is for reads: a write goes through the keyed array handler as before.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 24, 2026
@orklah

orklah commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks @jirkace!

@orklah
orklah merged commit f597240 into vimeo:master Nov 24, 2026
70 of 71 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Template array offsets: $arr[$k] with key-of<T> is not value-of<T>, and [$k => $v] rejects a key-of<T> key

2 participants