Skip to content
Permalink
Browse files

Fix #1609 - warn when using covariant template type as input

  • Loading branch information...
muglug committed May 14, 2019
1 parent 6f90e65 commit 7fbbe964cbf39bfdd190a5032656a12eb01f30f1
@@ -415,7 +415,9 @@ function (FunctionLikeParameter $p) {
$function_param->type_location,
$storage->suppressed_issues,
[],
false
false,
$this->function instanceof ClassMethod
&& strtolower($this->function->name->name) !== '__construct'
) === false) {
$check_stmts = false;
}
@@ -280,7 +280,8 @@ public function check(
CodeLocation $code_location,
array $suppressed_issues,
array $phantom_classes = [],
$inferred = true
bool $inferred = true,
bool $prevent_template_covariance = false
) {
if ($this->checked) {
return;
@@ -359,6 +360,32 @@ public function check(
}
if ($this instanceof TTemplateParam) {
if ($prevent_template_covariance && $this->defining_class) {
$codebase = $source->getCodebase();
$class_storage = $codebase->classlike_storage_provider->get($this->defining_class);
$template_offset = $class_storage->template_types
? array_search($this->param_name, array_keys($class_storage->template_types))
: false;
if ($template_offset !== false
&& $class_storage->template_covariants
&& $class_storage->template_covariants[$template_offset]
) {
if (IssueBuffer::accepts(
new InvalidTemplateParam(
'Template param ' . $this->defining_class . ' is marked covariant and cannot be used'
. ' as input to a function',
$code_location
),
$source->getSuppressedIssues()
)) {
// fall through
}
}
}
$this->as->check($source, $code_location, $suppressed_issues, $phantom_classes, $inferred);
}
@@ -424,7 +451,8 @@ public function check(
$code_location,
$suppressed_issues,
$phantom_classes,
$inferred
$inferred,
$prevent_template_covariance
);
}
}
@@ -436,7 +464,8 @@ public function check(
$code_location,
$suppressed_issues,
$phantom_classes,
$inferred
$inferred,
$prevent_template_covariance
);
}
}
@@ -499,7 +528,8 @@ public function check(
$code_location,
$suppressed_issues,
$phantom_classes,
$inferred
$inferred,
$prevent_template_covariance
) === false) {
return false;
}
@@ -1469,7 +1469,8 @@ public function check(
CodeLocation $code_location,
array $suppressed_issues,
array $phantom_classes = [],
$inferred = true
bool $inferred = true,
bool $prevent_template_covariance = false
) {
if ($this->checked) {
return;
@@ -1483,7 +1484,8 @@ public function check(
$code_location,
$suppressed_issues,
$phantom_classes,
$inferred
$inferred,
$prevent_template_covariance
) === false) {
$all_good = false;
}
@@ -2788,6 +2788,19 @@ function takesDogList(Collection $list) : void {
}',
'error_message' => 'InvalidArgument',
],
'preventCovariantParamUsage' => [
'<?php
/**
* @template-covariant T
*/
class Covariant {
/**
* @param T $value
*/
public function set($value): void {}
}',
'error_message' => 'InvalidTemplateParam',
],
];
}
}

0 comments on commit 7fbbe96

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