Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ private static function intersectButNotNever(Type $nativeType, Type $inferredTyp
return $result;
}

public function enterMatch(Expr\Match_ $expr): self
public function enterMatch(Expr\Match_ $expr, ?Type $condType, ?Type $condNativeType): self
{
if ($expr->cond instanceof Variable) {
return $this;
Expand All @@ -3001,8 +3001,8 @@ public function enterMatch(Expr\Match_ $expr): self
return $this;
}

$type = $this->getType($cond);
$nativeType = $this->getNativeType($cond);
$type = $condType ?? $this->getType($cond);
$nativeType = $condNativeType ?? $this->getNativeType($cond);
$condExpr = new AlwaysRememberedExpr($cond, $type, $nativeType);
$expr->cond = $condExpr;

Expand Down
7 changes: 6 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4175,14 +4175,19 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
$hasYield = true;
} elseif ($expr instanceof Expr\Match_) {
$deepContext = $context->enterDeep();
$condIsIncDec = $expr->cond instanceof Expr\PreInc
|| $expr->cond instanceof Expr\PostInc
|| $expr->cond instanceof Expr\PreDec
|| $expr->cond instanceof Expr\PostDec;
$condType = $scope->getType($expr->cond);
$condNativeType = $scope->getNativeType($expr->cond);
$condResult = $this->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $deepContext);
$scope = $condResult->getScope();
$hasYield = $condResult->hasYield();
$throwPoints = $condResult->getThrowPoints();
$impurePoints = $condResult->getImpurePoints();
$isAlwaysTerminating = $condResult->isAlwaysTerminating();
$matchScope = $scope->enterMatch($expr);
$matchScope = $scope->enterMatch($expr, $condIsIncDec ? $condType : null, $condIsIncDec ? $condNativeType : null);
$armNodes = [];
$hasDefaultCond = false;
$hasAlwaysTrueCond = false;
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,15 @@ public function testBug9534(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug11310(): void
{
$this->analyse([__DIR__ . '/data/bug-11310.php'], [
[
'Match arm comparison between int<1, max> and 0 is always false.',
24,
],
]);
}

}
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-11310.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php // lint >= 8.0

namespace Bug11310;

/** @param int<0, max> $i */
function foo(int $i): void {
echo match ($i++) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, max> $i */
function bar(int $i): void {
echo match ($i--) {
0 => 'zero',
default => 'default',
};
}

/** @param int<0, max> $i */
function baz(int $i): void {
echo match (++$i) {
0 => 'zero',
1 => 'one',
default => 'default',
};
}
Loading