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

handle true/false reconciliation consistently, fix #8795 #8796

Merged
merged 2 commits into from Nov 30, 2022
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
41 changes: 15 additions & 26 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Expand Up @@ -17,7 +17,6 @@
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEnumCase;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIterable;
Expand All @@ -30,7 +29,6 @@
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Reconciler;
use Psalm\Type\Union;

Expand Down Expand Up @@ -93,30 +91,21 @@ public static function reconcile(
$existing_var_atomic_types = $existing_var_type->getAtomicTypes();
$existing_var_type = $existing_var_type->getBuilder();

if ($assertion_type instanceof TFalse && isset($existing_var_atomic_types['bool'])) {
$existing_var_type->removeType('bool');
$existing_var_type->addType(new TTrue);
} elseif ($assertion_type instanceof TTrue && isset($existing_var_atomic_types['bool'])) {
$existing_var_type = $existing_var_type->getBuilder();
$existing_var_type->removeType('bool');
$existing_var_type->addType(new TFalse);
} else {
$simple_negated_type = SimpleNegatedAssertionReconciler::reconcile(
$statements_analyzer->getCodebase(),
$assertion,
$existing_var_type->freeze(),
$key,
$negated,
$code_location,
$suppressed_issues,
$failed_reconciliation,
$is_equality,
$inside_loop
);

if ($simple_negated_type) {
return $simple_negated_type;
}
$simple_negated_type = SimpleNegatedAssertionReconciler::reconcile(
$statements_analyzer->getCodebase(),
$assertion,
$existing_var_type->freeze(),
$key,
$negated,
$code_location,
$suppressed_issues,
$failed_reconciliation,
$is_equality,
$inside_loop
);

if ($simple_negated_type) {
return $simple_negated_type;
}

$assertion_type = $assertion->getAtomicType();
Expand Down
197 changes: 197 additions & 0 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Expand Up @@ -41,6 +41,7 @@
use Psalm\Type\Atomic\TClassConstant;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEmptyMixed;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TInt;
Expand Down Expand Up @@ -425,6 +426,32 @@ public static function reconcile(
);
}

if ($assertion_type && $assertion_type instanceof TTrue) {
return self::reconcileTrue(
$assertion,
$existing_var_type,
$key,
$negated,
$code_location,
$suppressed_issues,
$failed_reconciliation,
$is_equality
);
}

if ($assertion_type && $assertion_type instanceof TFalse) {
return self::reconcileFalse(
$assertion,
$existing_var_type,
$key,
$negated,
$code_location,
$suppressed_issues,
$failed_reconciliation,
$is_equality
);
}

if ($assertion_type && get_class($assertion_type) === TString::class) {
return self::reconcileString(
$assertion,
Expand Down Expand Up @@ -1142,6 +1169,176 @@ private static function reconcileBool(
: Type::getNever();
}

/**
* @param string[] $suppressed_issues
* @param Reconciler::RECONCILIATION_* $failed_reconciliation
*/
private static function reconcileFalse(
Assertion $assertion,
Union $existing_var_type,
?string $key,
bool $negated,
?CodeLocation $code_location,
array $suppressed_issues,
int &$failed_reconciliation,
bool $is_equality
): Union {
if ($existing_var_type->hasMixed()) {
return Type::getFalse();
}
if ($existing_var_type->hasScalar()) {
return Type::getFalse();
}

$old_var_type_string = $existing_var_type->getId();
$existing_var_atomic_types = $existing_var_type->getAtomicTypes();

$false_types = [];
$did_remove_type = false;

foreach ($existing_var_atomic_types as $type) {
if ($type instanceof TFalse) {
$false_types[] = $type;
} elseif ($type instanceof TBool) {
$false_types[] = new TFalse();
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam && $type->as->isMixed()) {
$type = $type->replaceAs(Type::getFalse());
$false_types[] = $type;
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam) {
if ($type->as->hasScalar() || $type->as->hasMixed() || $type->as->hasBool()) {
$type = $type->replaceAs(self::reconcileFalse(
$assertion,
$type->as,
null,
false,
null,
$suppressed_issues,
$failed_reconciliation,
$is_equality
));

$false_types[] = $type;
}

$did_remove_type = true;
} else {
$did_remove_type = true;
}
}

if ((!$false_types || !$did_remove_type) && !$is_equality) {
if ($key && $code_location) {
self::triggerIssueForImpossible(
$existing_var_type,
$old_var_type_string,
$key,
$assertion,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
}
}

if ($false_types) {
return new Union($false_types);
}

$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;

return $existing_var_type->from_docblock
? Type::getMixed()
: Type::getNever();
}

/**
* @param string[] $suppressed_issues
* @param Reconciler::RECONCILIATION_* $failed_reconciliation
*/
private static function reconcileTrue(
Assertion $assertion,
Union $existing_var_type,
?string $key,
bool $negated,
?CodeLocation $code_location,
array $suppressed_issues,
int &$failed_reconciliation,
bool $is_equality
): Union {
if ($existing_var_type->hasMixed()) {
return Type::getTrue();
}
if ($existing_var_type->hasScalar()) {
return Type::getTrue();
}

$old_var_type_string = $existing_var_type->getId();
$existing_var_atomic_types = $existing_var_type->getAtomicTypes();

$true_types = [];
$did_remove_type = false;

foreach ($existing_var_atomic_types as $type) {
if ($type instanceof TTrue) {
$true_types[] = $type;
} elseif ($type instanceof TBool) {
$true_types[] = new TTrue();
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam && $type->as->isMixed()) {
$type = $type->replaceAs(Type::getTrue());
$true_types[] = $type;
$did_remove_type = true;
} elseif ($type instanceof TTemplateParam) {
if ($type->as->hasScalar() || $type->as->hasMixed() || $type->as->hasBool()) {
$type = $type->replaceAs(self::reconcileTrue(
$assertion,
$type->as,
null,
false,
null,
$suppressed_issues,
$failed_reconciliation,
$is_equality
));

$true_types[] = $type;
}

$did_remove_type = true;
} else {
$did_remove_type = true;
}
}

if ((!$true_types || !$did_remove_type) && !$is_equality) {
if ($key && $code_location) {
self::triggerIssueForImpossible(
$existing_var_type,
$old_var_type_string,
$key,
$assertion,
!$did_remove_type,
$negated,
$code_location,
$suppressed_issues
);
}
}

if ($true_types) {
return new Union($true_types);
}

$failed_reconciliation = Reconciler::RECONCILIATION_EMPTY;

return $existing_var_type->from_docblock
? Type::getMixed()
: Type::getNever();
}

/**
* @param string[] $suppressed_issues
* @param Reconciler::RECONCILIATION_* $failed_reconciliation
Expand Down