-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #5383 - prevent unsound use of new static for generics
- Loading branch information
Showing
15 changed files
with
619 additions
and
91 deletions.
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
157 changes: 157 additions & 0 deletions
157
docs/running_psalm/issues/UnsafeGenericInstantiation.md
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,157 @@ | ||
# UnsafeGenericInstantiation | ||
|
||
Emitted when an attempt is made to instantiate a class using `new static` without a constructor that's final: | ||
|
||
```php | ||
<?php | ||
|
||
/** | ||
* @template T | ||
* @psalm-consistent-constructor | ||
*/ | ||
class Container { | ||
/** | ||
* @var T | ||
*/ | ||
public $t; | ||
|
||
/** | ||
* @param T $t | ||
*/ | ||
public function __construct($t) { | ||
$this->t = $t; | ||
} | ||
|
||
/** | ||
* @template U | ||
* @param U $u | ||
* @return static<U> | ||
*/ | ||
public function getInstance($u) : static | ||
{ | ||
return new static($u); | ||
} | ||
} | ||
``` | ||
|
||
|
||
## What’s wrong here? | ||
|
||
The problem comes when extending the class: | ||
|
||
```php | ||
<?php | ||
|
||
/** | ||
* @template T | ||
* @psalm-consistent-constructor | ||
*/ | ||
class Container { | ||
/** | ||
* @var T | ||
*/ | ||
public $t; | ||
|
||
/** | ||
* @param T $t | ||
*/ | ||
public function __construct($t) { | ||
$this->t = $t; | ||
} | ||
|
||
/** | ||
* @template U | ||
* @param U $u | ||
* @return static<U> | ||
*/ | ||
public function getInstance($u) : static | ||
{ | ||
return new static($u); | ||
} | ||
} | ||
|
||
/** | ||
* @extends Container<string> | ||
*/ | ||
class StringContainer extends Container {} | ||
|
||
$c = StringContainer::getInstance(new stdClass()); | ||
// creates StringContainer<stdClass>, clearly invalid | ||
``` | ||
|
||
## How to fix | ||
|
||
Either use `new self` instead of `new static`: | ||
|
||
```php | ||
<?php | ||
|
||
/** | ||
* @template T | ||
* @psalm-consistent-constructor | ||
*/ | ||
class Container { | ||
/** | ||
* @var T | ||
*/ | ||
public $t; | ||
|
||
/** | ||
* @param T $t | ||
*/ | ||
public function __construct($t) { | ||
$this->t = $t; | ||
} | ||
|
||
/** | ||
* @template U | ||
* @param U $u | ||
* @return self<U> | ||
*/ | ||
public function getInstance($u) : self | ||
{ | ||
return new self($u); | ||
} | ||
} | ||
``` | ||
|
||
Or you can add a `@psalm-consistent-templates` annotation which ensures that any child class has the same generic params: | ||
|
||
```php | ||
<?php | ||
|
||
/** | ||
* @template T | ||
* @psalm-consistent-constructor | ||
* @psalm-consistent-templates | ||
*/ | ||
class Container { | ||
/** | ||
* @var T | ||
*/ | ||
public $t; | ||
|
||
/** | ||
* @param T $t | ||
*/ | ||
public function __construct($t) { | ||
$this->t = $t; | ||
} | ||
|
||
/** | ||
* @template U | ||
* @param U $u | ||
* @return static<U> | ||
*/ | ||
public function getInstance($u) : static | ||
{ | ||
return new static($u); | ||
} | ||
} | ||
|
||
/** | ||
* @template T | ||
* @psalm-extends Container<T> | ||
*/ | ||
class LazyLoadingContainer extends Container {} | ||
``` |
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
Oops, something went wrong.