-
Notifications
You must be signed in to change notification settings - Fork 680
Something weird. #8018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I found these snippets: https://psalm.dev/r/f81173405a<?php
/**
* @psalm-type Test = string
*/
class Foo
{
/** @var array<string, Test> */
private array $stuff = [];
/**
* @param non-empty-list<string> $param
* @return Test
*/
public function foo(array $param)
{
$arr = &$this->stuff[$param[0]]; // This seems to cause Psalm to blackout.
/** @var Test */
$x = $arr;
// This trace doesn't work, and $x's type appears to be mixed at this point.
/** @psalm-trace $x */
return $x;
}
}
https://psalm.dev/r/718c05c153<?php
/**
* @psalm-type Test = string
*/
class Foo
{
/** @var array<string, Test> */
private array $stuff = [];
/**
* @return Test
*/
public function foo(string $param)
{
$arr = &$this->stuff[$param]; // This seems to cause Psalm to blackout.
/** @var Test */
$x = $arr;
// This trace doesn't work, and $x's type appears to be mixed at this point.
/** @psalm-trace $x */
return $x;
}
}
https://psalm.dev/r/6ebda981ae<?php
/**
* @psalm-type Test = string
*/
class Foo
{
/** @var array<string, Test> */
private array $stuff = [];
/**
* @param non-empty-list<string> $param
* @return Test
*/
public function foo(array $param)
{
$arr = $this->stuff[$param[0]]; // This seems to cause Psalm to blackout.
/** @var Test */
$x = $arr;
// This trace doesn't work, and $x's type appears to be mixed at this point.
/** @psalm-trace $x */
return $x;
}
}
|
Oh joy, references! References are only somewhat supported. This issue is probably my fault because I actually made references work in most cases instead of being ignored, but that caused a lot of edge cases that were previously fine to be broken. FYI that code is actually not sound (maybe your actual code is though), PHP references are really weird and imo should be avoided except for trivial cases, but otoh this does look like it should be a trivial case since the reference only exists locally and isn't used in an array or object. It's undoubtedly a bug that everything after that reference is just ignored. |
Yup, I just came up with that example to show the issue. |
Haven't looked at the code, but I'm guessing the reason this works but your original example doesn't has something to do with the fact that Psalm tracks array offset types for I'm guessing the solution to this (if we actually want to fix it) is to make it so the rest of the analysis continues without bothering to account for the reference. Maybe we should have some sort of |
I found these snippets: https://psalm.dev/r/ed1a4b5cdd<?php
/**
* @psalm-type Test = string
*/
class Foo
{
/** @var array<string, Test> */
private array $stuff = [];
/**
* @param non-empty-list<string> $param
* @return Test
*/
public function foo(array $param)
{
$tmp = $param[0];
$arr = &$this->stuff[$tmp];
/** @var Test */
$x = $arr;
// This trace doesn't work, and $x's type appears to be mixed at this point.
/** @psalm-trace $x */
return $x;
}
}
https://psalm.dev/r/b636ed9fe4<?php
/** @var array<string, string> */
$arr = [];
/** @var string */
$foo = "foo";
$arr[$foo] = "bar";
/** @psalm-trace $arr, $arr[$foo] */;
https://psalm.dev/r/d4c6d40dab<?php
/** @var array<string, string> */
$arr = [];
/** @var non-empty-list<string> */
$foo = ["foo"];
$arr[$foo[0]] = "bar";
/** @psalm-trace $arr, $arr[$foo[0]] */;
$bar = $arr[$foo[0]];
/** @psalm-trace $bar */;
|
Actually, any reference to an array is dangerous due to that PHP bug: https://3v4l.org/vaCAp I think I'll add a warning about that as well. |
I wouldn't mind a warning with an explanation. I came up with the same solution, of copying the value at that index in a local variable. |
…re-case-for-references Improve handling of unsupported references (fixes #8018).
Please rename this issue to something more appropriate, once you figure out what it is. I didn't know what to name it.
https://psalm.dev/r/f81173405a
Converting
$param
to string, works: https://psalm.dev/r/718c05c153Same if we remove the reference on the offending line: https://psalm.dev/r/6ebda981ae
The text was updated successfully, but these errors were encountered: