Skip to content

Commit 8411207

Browse files
ondrejmirtesclaude
andcommitted
Keep the normalized branches of a traversed ConditionalType normalized
ConditionalType::traverse() maps the normalized branches and builds a new ConditionalType from them, which normalized them again. When the subject is narrowed in a branch of an enclosing conditional type on the same template type, every nested conditional type gets a new subject, is rebuilt, and each rebuilt level normalizes - traverses - the rest of the chain again. A chain of n nested conditions took about 4^n: 15 levels exhausted any memory limit. The mapped branches are images of normalized branches: the references to the subject in them were already narrowed, and follow whatever the subject maps to. Normalizing them again against the mapped subject would only narrow unrelated types that happen to equal it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uob4ZH23SzXKt3uM5tDUrQ
1 parent fadf8d6 commit 8411207

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎src/Type/ConditionalType.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function traverse(callable $cb): Type
153153
return $this;
154154
}
155155

156-
return new self($subject, $target, $if, $else, $this->negated);
156+
return $this->withNormalizedBranches($subject, $target, $if, $else);
157157
}
158158

159159
public function traverseSimultaneously(Type $right, callable $cb): Type
@@ -176,7 +176,7 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
176176
return $this;
177177
}
178178

179-
return new self($subject, $target, $if, $else, $this->negated);
179+
return $this->withNormalizedBranches($subject, $target, $if, $else);
180180
}
181181

182182
public function toPhpDocNode(): TypeNode
@@ -190,6 +190,21 @@ public function toPhpDocNode(): TypeNode
190190
);
191191
}
192192

193+
/**
194+
* The branches passed in are images of normalized branches, so the references to the
195+
* subject in them have already been narrowed, and follow whatever the subject maps to.
196+
* Normalizing them again against the mapped subject would narrow unrelated types that
197+
* happen to equal it, and re-traverse every conditional type nested in the branches.
198+
*/
199+
private function withNormalizedBranches(Type $subject, Type $target, Type $if, Type $else): self
200+
{
201+
$type = new self($subject, $target, $if, $else, $this->negated);
202+
$type->normalizedIf = $if;
203+
$type->normalizedElse = $else;
204+
205+
return $type;
206+
}
207+
193208
private function getNormalizedIf(): Type
194209
{
195210
return $this->normalizedIf ??= $this->narrowSubjectIn($this->if, !$this->negated);

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,15 @@ public function testBug15252AnalysedFileWithInvalidClassName(): void
16991699
$this->assertSame(10, $errors[0]->getLine());
17001700
}
17011701

1702+
#[RequiresPhp('>= 8.1.0')]
1703+
public function testBug15310(): void
1704+
{
1705+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-15310.php');
1706+
$this->assertCount(1, $errors);
1707+
$this->assertSame('Function Bug15310\\reproduce() return type with generic class Bug15310\\ExampleType does not specify its types: TValue', $errors[0]->getMessage());
1708+
$this->assertSame(39, $errors[0]->getLine());
1709+
}
1710+
17021711
/**
17031712
* @param string[]|null $allAnalysedFiles
17041713
* @return list<Error>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug15310;
4+
5+
enum ExampleIdentifier { case A; case B; case C; case D; case E; case F; case G; case H; case I; case J; case K; case L; case M; case N; case O; }
6+
7+
/** @template-covariant TValue */
8+
abstract class ExampleType
9+
{
10+
}
11+
12+
/**
13+
* @template T of ExampleIdentifier
14+
* @extends ExampleType<(
15+
* T is ExampleIdentifier::A ? array :
16+
* T is ExampleIdentifier::B ? bool :
17+
* T is ExampleIdentifier::C ? callable :
18+
* T is ExampleIdentifier::D ? false :
19+
* T is ExampleIdentifier::E ? float :
20+
* T is ExampleIdentifier::F ? int :
21+
* T is ExampleIdentifier::G ? iterable :
22+
* T is ExampleIdentifier::H ? mixed :
23+
* T is ExampleIdentifier::I ? null :
24+
* T is ExampleIdentifier::J ? object :
25+
* T is ExampleIdentifier::K ? resource :
26+
* T is ExampleIdentifier::L ? string :
27+
* T is ExampleIdentifier::M ? true :
28+
* T is ExampleIdentifier::N ? never :
29+
* T is ExampleIdentifier::O ? void :
30+
* mixed
31+
* )>
32+
*/
33+
final class ExampleBuiltinType extends ExampleType
34+
{
35+
/** @param T $identifier */
36+
public function __construct(public ExampleIdentifier $identifier) {}
37+
}
38+
39+
function reproduce(): ExampleType
40+
{
41+
return new ExampleBuiltinType(ExampleIdentifier::A);
42+
}

0 commit comments

Comments
 (0)