Skip to content

Commit

Permalink
Merge pull request #10303 from kkmuffme/suppressing-novalue-should-no…
Browse files Browse the repository at this point in the history
…t-treat-code-as-unevaluated

Suppressing NoValue should not treat subsequent code as unevaluated
  • Loading branch information
orklah committed Nov 13, 2023
2 parents b3ef6a0 + b0adeb4 commit d9d08f7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,21 +528,23 @@ public static function analyze(
}

if ($context->vars_in_scope[$var_id]->isNever()) {
if (IssueBuffer::accepts(
if (!IssueBuffer::accepts(
new NoValue(
'All possible types for this assignment were invalidated - This may be dead code',
new CodeLocation($statements_analyzer->getSource(), $assign_var),
),
$statements_analyzer->getSuppressedIssues(),
)) {
return false;
}

$context->vars_in_scope[$var_id] = Type::getNever();

$context->inside_assignment = $was_in_assignment;
// if the error is suppressed, do not treat it as never anymore
$new_mutable = $context->vars_in_scope[$var_id]->getBuilder()->addType(new TMixed);
$new_mutable->removeType('never');
$context->vars_in_scope[$var_id] = $new_mutable->freeze();
$context->has_returned = false;
} else {
$context->inside_assignment = $was_in_assignment;

return $context->vars_in_scope[$var_id];
return $context->vars_in_scope[$var_id];
}
}

if ($statements_analyzer->data_flow_graph) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,13 +806,16 @@ public static function verifyType(
}

if ($input_type->isNever()) {
IssueBuffer::maybeAdd(
if (!IssueBuffer::accepts(
new NoValue(
'All possible types for this argument were invalidated - This may be dead code',
$arg_location,
),
$statements_analyzer->getSuppressedIssues(),
);
)) {
// if the error is suppressed, do not treat it as exited anymore
$context->has_returned = false;
}

return null;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/IntRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,22 @@ function getInt(): int{return 0;}
'$h===' => 'int<-4, 4>',
'$i===' => 'int<min, 0>',
'$j===' => 'int<min, 0>',
'$k===' => 'never',
'$k===' => 'mixed',
'$l===' => 'int',
'$m===' => 'int<0, max>',
'$n===' => 'int<min, 0>',
'$o===' => 'never',
'$o===' => 'mixed',
'$p===' => 'int',
'$q===' => 'never',
'$q===' => 'mixed',
'$r===' => 'int<0, 2>',
'$s===' => 'int<-2, 0>',
'$t===' => 'never',
'$t===' => 'mixed',
'$u===' => 'int<-2, 0>',
'$v===' => 'int<2, 0>',
'$w===' => 'never',
'$w===' => 'mixed',
'$x===' => 'int<0, 2>',
'$y===' => 'int<-2, 0>',
'$z===' => 'never',
'$z===' => 'mixed',
'$aa===' => 'int<-2, 2>',
'$ab===' => 'int<-2, 2>',
],
Expand Down
33 changes: 31 additions & 2 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ public static function get(): a {
return new a;
}
}
final class b {
public function test(): a {
return new a;
}
}
function process(b $handler): a {
if (\extension_loaded("fdsfdsfd")) {
return $handler->test();
Expand Down Expand Up @@ -1323,6 +1323,35 @@ public function b(): void {}
new A;
PHP,
],
'callNeverReturnsSuppressed' => [
'code' => '<?php
namespace Foo;
/**
* @psalm-suppress InvalidReturnType
* @return never
*/
function foo() : void {}
/** @psalm-suppress NoValue */
$a = foo();
print_r($a);',
],
'useNeverReturnsAsArgSuppressed' => [
'code' => '<?php
namespace Foo;
/**
* @psalm-suppress InvalidReturnType
* @return never
*/
function foo() : void {}
/** @psalm-suppress UnusedParam */
function bar(string $s) : void {}
/** @psalm-suppress NoValue */
bar(foo());
echo "hello";',
],
];
}

Expand Down

0 comments on commit d9d08f7

Please sign in to comment.