-
Notifications
You must be signed in to change notification settings - Fork 506
Fix imprecise types after assignment when strict-types=1 #3945
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
Merged
+306
−4
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ad88033
Fix imprecise types after assignment when strict-types=1
staabm 96f8a78
fix
staabm 602ff9c
namespace
staabm 7b99a87
Update bug-12902.php
staabm e78b85a
works only on instance methods
staabm 7b43638
Update bug-12902.php
staabm eeb6150
TypeInferenceTestCase: support declare(); before // lint
staabm b2a788c
coerce native/non-native both or none
staabm 75fafa7
implement feedback
staabm be51451
Update NodeScopeResolver.php
staabm c6f0e16
fix name collision
staabm d0a40f8
fix
staabm 7fdb85b
static property fetch
staabm 52e4b42
test nullable
staabm 179f0d9
Discard changes to src/Testing/TypeInferenceTestCase.php
staabm 4e0fe58
fix lint condition
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php // lint >= 8.1 | ||
|
||
declare(strict_types = 0); | ||
|
||
namespace Bug12902NonStrict; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class NarrowsNativeConstantValue | ||
{ | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = 1; | ||
} | ||
|
||
public function doFoo(): void | ||
{ | ||
assertType('1', $this->i); | ||
assertNativeType('1', $this->i); | ||
} | ||
} | ||
|
||
class NarrowsNativeReadonlyUnion { | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
class NarrowsNativeUnion { | ||
private int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
|
||
$this->impureCall(); | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impureCall(): void {} | ||
} | ||
|
||
class NarrowsStaticNativeUnion { | ||
private static int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
self::$i = getInt(); | ||
assertType('int', self::$i); | ||
assertNativeType('int', self::$i); | ||
|
||
$this->impureCall(); | ||
assertType('int', self::$i); // should be float|int | ||
assertNativeType('int', self::$i); // should be float|int | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', self::$i); | ||
assertNativeType('float|int', self::$i); | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impureCall(): void {} | ||
} | ||
|
||
function getInt(): int { | ||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php // lint >= 8.1 | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Bug12902; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class NarrowsNativeConstantValue | ||
{ | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = 1; | ||
} | ||
|
||
public function doFoo(): void | ||
{ | ||
assertType('1', $this->i); | ||
assertNativeType('1', $this->i); | ||
} | ||
} | ||
|
||
class NarrowsNativeReadonlyUnion { | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
class NarrowsNativeUnion { | ||
private int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
|
||
$this->impureCall(); | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impureCall(): void {} | ||
} | ||
|
||
class NarrowsStaticNativeUnion { | ||
private static int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
self::$i = getInt(); | ||
assertType('int', self::$i); | ||
assertNativeType('int', self::$i); | ||
|
||
$this->impureCall(); | ||
assertType('int', self::$i); // should be float|int | ||
assertNativeType('int', self::$i); // should be float|int | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', self::$i); | ||
assertNativeType('float|int', self::$i); | ||
} | ||
|
||
/** @phpstan-impure */ | ||
public function impureCall(): void {} | ||
} | ||
|
||
function getInt(): int { | ||
return 1; | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/PHPStan/Analyser/nsrt/remember-non-nullable-property-non-strict.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php // lint >= 8.1 | ||
|
||
declare(strict_types = 0); | ||
|
||
namespace RememberNonNullablePropertyWhenStrictTypesDisabled; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class KeepsPropertyNonNullable { | ||
private readonly int $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
class DontCoercePhpdocType { | ||
/** @var int */ | ||
private $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('mixed', $this->i); | ||
} | ||
} | ||
|
||
function getIntOrNull(): ?int { | ||
if (rand(0, 1) === 0) { | ||
return null; | ||
} | ||
return 1; | ||
} | ||
|
||
|
||
class KeepsPropertyNonNullable2 { | ||
private int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getIntOrFloatOrNull(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('float|int', $this->i); | ||
assertNativeType('float|int', $this->i); | ||
} | ||
} | ||
|
||
function getIntOrFloatOrNull(): null|int|float { | ||
if (rand(0, 1) === 0) { | ||
return null; | ||
} | ||
|
||
if (rand(0, 10) === 0) { | ||
return 1.0; | ||
} | ||
return 1; | ||
} | ||
|
||
class NarrowsNativeUnion { | ||
private readonly int|float $i; | ||
|
||
public function __construct() | ||
{ | ||
$this->i = getInt(); | ||
} | ||
|
||
public function doFoo(): void { | ||
assertType('int', $this->i); | ||
assertNativeType('int', $this->i); | ||
} | ||
} | ||
|
||
function getInt(): int { | ||
return 1; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a unrelated bug, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix in #3950