Skip to content
Permalink
Browse files

Fix #1886 - derived types from templates in new calls should be coerc…

…able
  • Loading branch information...
muglug committed Jul 9, 2019
1 parent aa0efcf commit 884a03078b085a74d15f4e9102d832047739396b
@@ -414,17 +414,21 @@ public static function analyze(
foreach ($storage->template_types as $template_name => $base_type) {
if (isset($found_generic_params[$template_name][$fq_class_name])) {
$generic_param_types[] = $found_generic_params[$template_name][$fq_class_name][0];
$generic_param_type = $found_generic_params[$template_name][$fq_class_name][0];
} elseif ($storage->template_type_extends && $found_generic_params) {
$generic_param_types[] = self::getGenericParamForOffset(
$generic_param_type = self::getGenericParamForOffset(
$declaring_fq_class_name,
$template_name,
$storage->template_type_extends,
$found_generic_params
);
} else {
$generic_param_types[] = array_values($base_type)[0][0];
$generic_param_type = array_values($base_type)[0][0];
}
$generic_param_type->had_template = true;
$generic_param_types[] = $generic_param_type;
}
}
@@ -1618,6 +1618,21 @@ public function __construct() {
}
}',
],
'classTemplatedPropertyAssignmentWithMoreSpecificArray' => [
'<?php
/** @template T */
class Foo {
/** @param \Closure():T $closure */
public function __construct($closure) {}
}
class Bar {
/** @var Foo<array> */
private $FooArray;
public function __construct() {
$this->FooArray = new Foo(function(): array { return []; });
}
}',
],
'insideClosureVarTemplate' => [
'<?php
/**
@@ -1637,7 +1652,46 @@ function() {
};
}
}',
]
],
'allowBoundedType' => [
'<?php
class Base {}
class Child extends Base {}
/**
* @template T
*/
class Foo
{
/** @param Closure():T $t */
public function __construct(Closure $t) {}
}
/**
* @return Foo<Base>
*/
function returnFooBase() : Foo {
$f = new Foo(function () { return new Child(); });
return $f;
}',
],
'allowMoreSpecificArray' => [
'<?php
/** @template T */
class Foo {
/** @param \Closure():T $closure */
public function __construct($closure) {}
}
class Bar {
/** @var Foo<array> */
private $FooArray;
public function __construct() {
$this->FooArray = new Foo(function(): array { return ["foo" => "bar"]; });
}
}'
],
];
}

0 comments on commit 884a030

Please sign in to comment.
You can’t perform that action at this time.