-
Notifications
You must be signed in to change notification settings - Fork 659
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
Associated mutable in immutable class breaks immutability #2805
Comments
I found these snippets: https://psalm.dev/r/a6ac151a01<?php
class Item {
private int $i = 0;
public function mutate(): void {
$this->i++;
}
public function get(): int
{
return $this->i;
}
}
/**
* @psalm-immutable
*/
class Immutable {
private $item;
public function __construct(Item $item)
{
$this->item = $item;
}
public function get(): int
{
return $this->item->get();
}
}
$item = new Item();
$immutable = new Immutable($item);
$memory = $immutable->get();
$item->mutate();
if ($immutable->get() !== $memory) {
// will be true
}
|
@muglug The fix looks a bit too radical. IMO this should not fail: https://psalm.dev/r/8b58a212bc. |
I found these snippets: https://psalm.dev/r/8b58a212bc<?php
class Item {
private int $i = 0;
public function mutate(): void {
$this->i++;
}
public function get(): int
{
return $this->i;
}
}
/**
* @psalm-immutable
*/
class Immutable {
public function get(Item $item): int
{
return $item->get();
}
}
$item = new Item();
$immutable = new Immutable();
$memory = $immutable->get($item);
$item->mutate();
if ($immutable->get($item) !== $memory) {
// will be true, and it's fine
}
|
Yup, it doesn't anymore |
In retrospect I think this was the wrong fix. The error here is allowing the assignment |
cc @sebastianbergmann and @ADmad who were caught up in this, among others. Upon further reflection the only issue is when an immutable class stores a reference to a mutable one. |
yeah, that's the point – whether or not the object created was immutable, that should not have been reported as an issue |
I've actually gone back totally on this: I don't believe it was a bug in the first instance. Issue was raised here: #4126 Basically there's no way for any immutable/pure promises to be broken. |
https://psalm.dev/r/a6ac151a01
IMO psalm should report mutable error when not immutable class is injected.
The text was updated successfully, but these errors were encountered: