Skip to content

Commit ef25c91

Browse files
authored
Merge branch refs/heads/2.1.x into 2.2.x
2 parents 90089b5 + 03e0417 commit ef25c91

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,7 @@ private static function intersectButNotNever(Type $nativeType, Type $inferredTyp
29872987
return $result;
29882988
}
29892989

2990-
public function enterMatch(Expr\Match_ $expr): self
2990+
public function enterMatch(Expr\Match_ $expr, Type $condType, Type $condNativeType): self
29912991
{
29922992
if ($expr->cond instanceof Variable) {
29932993
return $this;
@@ -3001,8 +3001,8 @@ public function enterMatch(Expr\Match_ $expr): self
30013001
return $this;
30023002
}
30033003

3004-
$type = $this->getType($cond);
3005-
$nativeType = $this->getNativeType($cond);
3004+
$type = $condType;
3005+
$nativeType = $condNativeType;
30063006
$condExpr = new AlwaysRememberedExpr($cond, $type, $nativeType);
30073007
$expr->cond = $condExpr;
30083008

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4184,13 +4184,14 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
41844184
} elseif ($expr instanceof Expr\Match_) {
41854185
$deepContext = $context->enterDeep();
41864186
$condType = $scope->getType($expr->cond);
4187+
$condNativeType = $scope->getNativeType($expr->cond);
41874188
$condResult = $this->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $deepContext);
41884189
$scope = $condResult->getScope();
41894190
$hasYield = $condResult->hasYield();
41904191
$throwPoints = $condResult->getThrowPoints();
41914192
$impurePoints = $condResult->getImpurePoints();
41924193
$isAlwaysTerminating = $condResult->isAlwaysTerminating();
4193-
$matchScope = $scope->enterMatch($expr);
4194+
$matchScope = $scope->enterMatch($expr, $condType, $condNativeType);
41944195
$armNodes = [];
41954196
$hasDefaultCond = false;
41964197
$hasAlwaysTrueCond = false;

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,4 +446,23 @@ public function testBug9534(): void
446446
]);
447447
}
448448

449+
#[RequiresPhp('>= 8.0')]
450+
public function testBug11310(): void
451+
{
452+
$this->analyse([__DIR__ . '/data/bug-11310.php'], [
453+
[
454+
'Match arm comparison between int<1, max> and 0 is always false.',
455+
24,
456+
],
457+
[
458+
'Match arm comparison between int<1, 6>|int<8, 14> and 0 is always false.',
459+
49,
460+
],
461+
[
462+
'Match arm comparison between 1|2|3|4|5|6 and 0 is always false.',
463+
74,
464+
],
465+
]);
466+
}
467+
449468
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug11310;
4+
5+
/** @param int<0, max> $i */
6+
function foo(int $i): void {
7+
echo match ($i++) {
8+
0 => 'zero',
9+
default => 'default',
10+
};
11+
}
12+
13+
/** @param int<0, max> $i */
14+
function bar(int $i): void {
15+
echo match ($i--) {
16+
0 => 'zero',
17+
default => 'default',
18+
};
19+
}
20+
21+
/** @param int<0, max> $i */
22+
function baz(int $i): void {
23+
echo match (++$i) {
24+
0 => 'zero',
25+
1 => 'one',
26+
default => 'default',
27+
};
28+
}
29+
30+
/** @param int<0, 5>|int<7, 13> $i */
31+
function foo2(int $i): void {
32+
echo match ($i++) {
33+
0 => 'zero',
34+
default => 'default',
35+
};
36+
}
37+
38+
/** @param int<0, 5>|int<7, 13> $i */
39+
function bar2(int $i): void {
40+
echo match ($i--) {
41+
0 => 'zero',
42+
default => 'default',
43+
};
44+
}
45+
46+
/** @param int<0, 5>|int<7, 13> $i */
47+
function baz2(int $i): void {
48+
echo match (++$i) {
49+
0 => 'zero',
50+
1 => 'one',
51+
default => 'default',
52+
};
53+
}
54+
55+
/** @param 0|1|2|3|4|5 $i */
56+
function foo3(int $i): void {
57+
echo match ($i++) {
58+
0 => 'zero',
59+
default => 'default',
60+
};
61+
}
62+
63+
/** @param 0|1|2|3|4|5 $i */
64+
function bar3(int $i): void {
65+
echo match ($i--) {
66+
0 => 'zero',
67+
default => 'default',
68+
};
69+
}
70+
71+
/** @param 0|1|2|3|4|5 $i */
72+
function baz3(int $i): void {
73+
echo match (++$i) {
74+
0 => 'zero',
75+
1 => 'one',
76+
default => 'default',
77+
};
78+
}

0 commit comments

Comments
 (0)