Skip to content

[6.0][CSBindings] Optional object type variable should be connected to the optional #73983

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

Merged
merged 2 commits into from
May 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,16 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
break;
}

case ConstraintKind::OptionalObject: {
// Type variable that represents an object type of
// an un-inferred optional is adjacent to a type
// variable that presents such optional (`bindingTypeVar`
// in this case).
if (kind == AllowedBindingKind::Supertypes)
AdjacentVars.insert({bindingTypeVar, constraint});
break;
}

default:
break;
}
Expand Down
10 changes: 8 additions & 2 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5425,8 +5425,14 @@ bool ConstraintSystem::repairFailures(
auto contextualTy = simplifyType(rhs)->getOptionalObjectType();
if (!lhs->getOptionalObjectType() && !lhs->hasTypeVariable() &&
contextualTy && !contextualTy->isTypeVariableOrMember()) {
conversionsOrFixes.push_back(IgnoreContextualType::create(
*this, lhs, rhs, getConstraintLocator(OEE->getSubExpr())));
auto *fixLocator = getConstraintLocator(OEE->getSubExpr());
// If inner expression already has a fix, consider this two-way
// mismatch as un-salvageable.
if (hasFixFor(fixLocator))
return false;

conversionsOrFixes.push_back(
IgnoreContextualType::create(*this, lhs, rhs, fixLocator));
return true;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/stmt/foreach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,13 @@ do {
}
}
}

// https://github.com/apple/swift/issues/73207
do {
func test(_ levels: [Range<Int>]) {
for (i, leaves): (Int, Range<Int>) in levels[8 ..< 15].enumerated() { // Ok
_ = i
_ = leaves
}
}
}