Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions checker/internal/type_inference_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,30 +327,30 @@ bool TypeInferenceContext::IsAssignableInternal(
// that has a prospective type assignment.
to.kind() == TypeKind::kTypeParam &&
prospective_substitutions.contains(to.GetTypeParam().name())) {
TypeParamType to_param = to.GetTypeParam();
absl::string_view to_name = to_param.name();
// Types have special unification: type(A) and type(B) unify to type(dyn)
// if A and B are not co-assignable (at any depth, e.g. list(type(int)) and
// list(type(uint)) unify to list(type(dyn))). This is the only widening
// other than for union types.
SubstitutionMap join_subs = prospective_substitutions;
if (std::optional<Type> joined =
JoinTypeTypes(from_subs, to_subs, join_subs);
joined.has_value() &&
!OccursWithin(to.GetTypeParam().name(), *joined, join_subs)) {
join_subs[to.GetTypeParam().name()] = *std::move(joined);
joined.has_value() && !OccursWithin(to_name, *joined, join_subs)) {
join_subs[to_name] = *std::move(joined);
prospective_substitutions = std::move(join_subs);
return true;
}
SubstitutionMap prospective_subs_cpy = prospective_substitutions;
if (CompareGenerality(from_subs, to_subs, prospective_subs_cpy) ==
RelativeGenerality::kMoreGeneral) {
if (IsAssignableInternal(to_subs, from_subs, prospective_subs_cpy) &&
!OccursWithin(to.GetTypeParam().name(), from_subs,
prospective_subs_cpy)) {
prospective_subs_cpy[to.GetTypeParam().name()] = from_subs;
!OccursWithin(to_name, from_subs, prospective_subs_cpy)) {
prospective_subs_cpy[to_name] = from_subs;
prospective_substitutions = std::move(prospective_subs_cpy);
return true;
// otherwise, continue with normal assignability check.
}
// otherwise, continue with normal assignability check.
}
}

Expand Down Expand Up @@ -637,9 +637,12 @@ bool TypeInferenceContext::IsAssignableWithConstraints(
SubstitutionMap& prospective_substitutions) {
if (to.kind() == TypeKind::kTypeParam &&
from.kind() == TypeKind::kTypeParam) {
if (to.AsTypeParam()->name() != from.AsTypeParam()->name()) {
TypeParamType to_param = to.GetTypeParam();
TypeParamType from_param = from.GetTypeParam();
absl::string_view from_name = from_param.name();
if (to_param.name() != from_name) {
// Simple case, bind from to 'to' if both are free.
prospective_substitutions[from.AsTypeParam()->name()] = to;
prospective_substitutions[from_name] = to;
}
return true;
}
Expand Down
Loading