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

Fix crash due to unresolvable constant. #7490

Merged
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
Expand Up @@ -831,7 +831,11 @@ public static function checkReturnType(
$classlike_storage->name ?? null,
$parent_class,
true,
true
true,
false,
false,
false,
true,
);
} catch (UnresolvableConstantException $e) {
IssueBuffer::maybeAdd(
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -1025,7 +1025,9 @@ private function processParams(
true,
false,
false,
true
true,
false,
true,
);
} catch (UnresolvableConstantException $e) {
if ($function_param->type_location !== null) {
Expand Down
78 changes: 52 additions & 26 deletions src/Psalm/Internal/Type/TypeExpander.php
Expand Up @@ -66,7 +66,8 @@ public static function expandUnion(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): Union {
$return_type = clone $return_type;

Expand All @@ -85,7 +86,8 @@ public static function expandUnion(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($return_type_part instanceof TTypeAlias || count($parts) > 1) {
Expand Down Expand Up @@ -134,7 +136,8 @@ public static function expandAtomic(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): array {
if ($return_type instanceof TNamedObject
|| $return_type instanceof TTemplateParam
Expand All @@ -152,7 +155,8 @@ public static function expandAtomic(
$evaluate_class_constants,
$evaluate_conditional_types,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($extra_type instanceof TNamedObject && $extra_type->extra_types) {
Expand Down Expand Up @@ -197,7 +201,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($new_as_type instanceof TNamedObject) {
Expand All @@ -215,7 +220,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if ($expand_templates) {
Expand Down Expand Up @@ -324,7 +330,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$recursively_fleshed_out_types = array_merge(
Expand All @@ -349,7 +356,9 @@ public static function expandAtomic(
}

if ($evaluate_class_constants) {
if (!$codebase->classOrInterfaceExists($return_type->fq_classlike_name)) {
if ($throw_on_unresolvable_constant
&& !$codebase->classOrInterfaceExists($return_type->fq_classlike_name)
) {
throw new UnresolvableConstantException($return_type->fq_classlike_name, $return_type->const_name);
}

Expand Down Expand Up @@ -379,7 +388,7 @@ public static function expandAtomic(
return array_values($const_type_atomic->type_params[1]->getAtomicTypes());
}
}
} else {
} elseif ($throw_on_unresolvable_constant) {
throw new UnresolvableConstantException($return_type->fq_classlike_name, $return_type->const_name);
}
}
Expand All @@ -405,7 +414,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$new_value_type = reset($new_value_type);
Expand Down Expand Up @@ -437,7 +447,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$potential_ints = [];
Expand All @@ -464,7 +475,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}

Expand All @@ -484,7 +496,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
} elseif ($return_type instanceof TKeyedArray) {
Expand All @@ -499,7 +512,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
} elseif ($return_type instanceof TList) {
Expand All @@ -513,7 +527,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}

Expand All @@ -529,7 +544,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand All @@ -550,7 +566,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand All @@ -566,7 +583,8 @@ public static function expandAtomic(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);
}
}
Expand Down Expand Up @@ -683,7 +701,8 @@ private static function expandConditional(
bool $evaluate_conditional_types = false,
bool $final = false,
bool $expand_generic = false,
bool $expand_templates = false
bool $expand_templates = false,
bool $throw_on_unresolvable_constant = false
): array {
$new_as_type = self::expandUnion(
$codebase,
Expand All @@ -695,7 +714,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->as_type = $new_as_type;
Expand All @@ -715,7 +735,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

if (count($candidate) === 1) {
Expand All @@ -737,7 +758,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$if_conditional_return_types = array_merge(
Expand All @@ -759,7 +781,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$else_conditional_return_types = array_merge(
Expand Down Expand Up @@ -852,7 +875,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->if_type = self::expandUnion(
Expand All @@ -865,7 +889,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

$return_type->else_type = self::expandUnion(
Expand All @@ -878,7 +903,8 @@ private static function expandConditional(
$evaluate_conditional_types,
$final,
$expand_generic,
$expand_templates
$expand_templates,
$throw_on_unresolvable_constant,
);

return [$return_type];
Expand Down