Repository navigation
Let @psalm- purity tags win over @pure and @phpstan-pure - #14071
Conversation
✅ Performance: unchanged · Types: unchanged
How to read this table
head e5fcb73 · base 67cfa84 (merge-base) · workflow run |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 The docblock fix is correct for @pure and @phpstan-pure, but the same bug remains for the #[JetBrains\PhpStorm\Pure] attribute, so the fix is incomplete.
I checked against current master (2d8a48d), with this branch merged in (clean merge). The example from #13607 fails on master and is clean with the branch. The same holds for @phpstan-pure + @psalm-mutation-free, and @pure + @psalm-impure is now impure. A lone @pure still forbids reading props (your pureAloneStillForbidsReadingProps guard).
Findings
src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php(the attribute loop,Psalm\Pure/JetBrains\PhpStorm\Pure): the generic#[JetBrains\PhpStorm\Pure]attribute setscapabilities = NONEand still overrides an explicit@psalm-capabilities. This is the same library scenario as the issue (pure for other tools,read-propsfor Psalm 7), with the PhpStorm attribute instead of the tag. On the branch:The docs section you edit listsinterface M { /** @psalm-capabilities read-props */ #[\JetBrains\PhpStorm\Pure] public function body(): string; } final class MM implements M { public function __construct(private string $body) {} public function body(): string { return $this->body; } } // ImmutableDependency: M::body is pure, but MM::body additionally requires read-props
#[Psalm\Pure]and#[JetBrains\PhpStorm\Pure]next to@pure, so users will expect the new precedence rule to apply to the attribute too.#[Psalm\Pure]is Psalm's own and can keep behaving like@psalm-pure. TheJetBrainsone should not override a@psalm-capability tag. Please apply the same precedence there and add a test.
Recommendations (non-blocking)
FunctionLikeDocblockParser: the newelseifrepeats the two assignments of the first branch. A local$generic_pure = isset(...phpstan-pure) || isset(...pure)consulted as the last link of the chain would keep one copy and make the precedence order readable in one place.- The docs sentence says "A
@psalm-tag from the table wins over@pure".@psalm-pureis in that table too, and@psalm-pure+@psalm-capabilitieson one method still resolves to@psalm-pure. Saying "a@psalm-capabilitiesor@psalm-impuretag" would be more accurate (the mutation-free tags are parse-only and should stay out of the docs). UPGRADING.md: a docblock with both@pureand a@psalm-capability tag changes meaning, so it deserves an entry.
|
Thanks for the review, all done except one point:
|
orklah
left a comment
There was a problem hiding this comment.
Thanks for the update. The docblock change and the PhpStorm attribute handling look right, and each new
case fails on master and passes here.
One issue with the new attribute guard: !$storage->has_mutations_annotation is also true after an earlier
#[Psalm\ExternalMutationFree] (or #[Psalm\Pure]/#[Psalm\Immutable]) in the same loop, not only after a
docblock tag, so the result now depends on attribute order:
#[\Psalm\ExternalMutationFree]
#[\JetBrains\PhpStorm\Immutable]
final class A {
private int $n = 0;
public function next(): int {
return ++$this->n; // no issue on this branch
}
}
#[\JetBrains\PhpStorm\Immutable]
#[\Psalm\ExternalMutationFree]
final class B {
private int $n = 0;
public function next(): int {
return ++$this->n; // InaccessibleProperty + ImpurePropertyAssignment
}
}The same happens on a method with #[Psalm\ExternalMutationFree] and #[JetBrains\PhpStorm\Pure]. On master
both orders behave the same. Could you read the docblock state once before the attribute loop
($docblock_has_mutations_annotation = $storage->has_mutations_annotation;) and test that in both
FunctionLikeNodeScanner and ClassLikeNodeScanner, with a test case for one of these combinations?
|
Good catch, thanks. Both scanners now read |
5edc8aa to
b2f486b
Compare
b2f486b to
e5fcb73
Compare
orklah
left a comment
There was a problem hiding this comment.
Thanks for the fix and the follow-up changes.
@pureand@phpstan-pure@pureand@phpstan-pureoverride@psalm-capabilitiesand@psalm-impureon the same function or method, whatever the tag order.Before
After
The PhpStorm
PureandImmutableattributes#[JetBrains\PhpStorm\Pure]overrides@psalm-capabilitiesthe same way, and#[JetBrains\PhpStorm\Immutable]narrows a class's@psalm-capabilitiestoread-props.Before
After
Fixes #13607