Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppressing NoValue should not treat subsequent code as unevaluated #10303

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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