Skip to content

Commit 9dae9e4

Browse files
ondrejmirtesclaude
andcommitted
Keep the in-process inference marker after a throwing inference natively
The twin's inferPrivatePropertyType() removes the class's entry in $inferClassConstructorPropertyTypesInProcess only when the inference returns normally; one that throws leaves it set, so later asks for that class infer nothing. The native port removed it unconditionally. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent 6fb5349 commit 9dae9e4

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎turbo-ext/src/PhpClassReflectionExtension.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2599,8 +2599,9 @@ class PhpClassReflectionExtension
25992599
if (issetIn(slot(PT_PCRE_PROP_INFER_IN_PROCESS), className) != NULL) return zv::Val::null();
26002600
setIn(slot(PT_PCRE_PROP_INFER_IN_PROCESS), className, zv::Val::boolean(true));
26012601
zv::Val propertyTypes = inferAndCachePropertyTypes(constructor);
2602-
unsetIn(slot(PT_PCRE_PROP_INFER_IN_PROCESS), className);
2602+
/* like the twin, the marker is removed on the normal return only */
26032603
if (UNEXPECTED(propertyTypes.isUndef())) return zv::Val();
2604+
unsetIn(slot(PT_PCRE_PROP_INFER_IN_PROCESS), className);
26042605
zval *found = keyIn(propertyTypes.raw(), propertyName);
26052606
if (found != NULL) return zv::Val::copyOf(zv::Ref(found));
26062607
return zv::Val::null();

‎turbo-ext/tests/php-class-reflection-family.php‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function normalizeClass(string $class): string
311311
}
312312

313313
/** @return array{PhpClassReflectionExtension, \PHPStanTurbo\PhpClassReflectionExtension} */
314-
$pcreBuildSides = static function (bool $infer, int $memberCacheKeysMax) use ($pcreCollaborators): array {
314+
$pcreBuildSides = static function (bool $infer, int $memberCacheKeysMax, ?\PHPStan\Parser\Parser $parser = null) use ($pcreCollaborators): array {
315315
$make = static fn (string $class) => new $class(
316316
scopeFactory: $pcreCollaborators['scopeFactory'],
317317
phpDocsResolver: $pcreCollaborators['phpDocsResolver'],
@@ -322,7 +322,7 @@ public function normalizeClass(string $class): string
322322
annotationsMethodsClassReflectionExtension: $pcreCollaborators['annotationsMethodsClassReflectionExtension'],
323323
annotationsPropertiesClassReflectionExtension: $pcreCollaborators['annotationsPropertiesClassReflectionExtension'],
324324
signatureMapProvider: $pcreCollaborators['signatureMapProvider'],
325-
parser: $pcreCollaborators['parser'],
325+
parser: $parser ?? $pcreCollaborators['parser'],
326326
stubPhpDocProvider: $pcreCollaborators['stubPhpDocProvider'],
327327
reflectionProviderProvider: $pcreCollaborators['reflectionProviderProvider'],
328328
fileTypeMapper: $pcreCollaborators['fileTypeMapper'],
@@ -533,6 +533,51 @@ public function normalizeClass(string $class): string
533533
);
534534
// }}}
535535

536+
// {{{ an inference that throws: the twin removes the class's in-process
537+
// marker on the normal return only, so a later ask infers nothing for it
538+
$pcreThrowObservations = [];
539+
foreach (['php', 'native'] as $pcreSide) {
540+
$pcreThrowingParser = new class ($pcreCollaborators['parser']) implements \PHPStan\Parser\Parser {
541+
542+
public bool $armed = true;
543+
544+
public function __construct(private \PHPStan\Parser\Parser $inner)
545+
{
546+
}
547+
548+
public function parseFile(string $file): array
549+
{
550+
if ($this->armed) {
551+
$this->armed = false;
552+
throw new \RuntimeException('parser failed');
553+
}
554+
return $this->inner->parseFile($file);
555+
}
556+
557+
public function parseString(string $sourceCode): array
558+
{
559+
return $this->inner->parseString($sourceCode);
560+
}
561+
562+
};
563+
[$pcrePhpSide, $pcreNativeSide] = $pcreBuildSides(true, 4096, $pcreThrowingParser);
564+
$pcreExtension = $pcreSide === 'php' ? $pcrePhpSide : $pcreNativeSide;
565+
$pcreThrowClass = $pcreReflectionProvider->getClass('PhpClassReflectionFamilyFixture\FixtureBase');
566+
foreach (['first', 'second'] as $pcreAttempt) {
567+
try {
568+
$pcreThrowObservations[$pcreSide][$pcreAttempt] = $pcreDescriber->describeProperty($pcreExtension->getProperty($pcreThrowClass, 'inferredFromConstructor', $pcreScopes['outOfClass']));
569+
} catch (\Throwable $e) {
570+
$pcreThrowObservations[$pcreSide][$pcreAttempt] = 'throws ' . $pcreDescriber->normalizeClass(get_class($e)) . ': ' . $e->getMessage();
571+
}
572+
$pcreThrowObservations[$pcreSide][$pcreAttempt . ' in process'] = (new ReflectionProperty($pcreExtension, 'inferClassConstructorPropertyTypesInProcess'))->getValue($pcreExtension);
573+
}
574+
}
575+
check(
576+
$pcreThrowObservations['php'] === $pcreThrowObservations['native'],
577+
'PhpClassReflectionExtension inference after a throwing one: ' . ($pcreFirstDifference($pcreThrowObservations['php'], $pcreThrowObservations['native']) ?? ''),
578+
);
579+
// }}}
580+
536581
if (isset($pcreStandalone)) {
537582
echo $failures === 0 ? "ALL OK\n" : "$failures failure(s)\n";
538583
exit($failures === 0 ? 0 : 1);

0 commit comments

Comments
 (0)