Skip to content

Commit 0a4ff7d

Browse files
committed
[CSBindings] Allow inference of l-value type from its object type
This aims to help with cases like `_ = { x in x = 0 }` or `.test = 42` which are currently supported because the member is eagerly bound to an `@lvalue $T` during constraint generation.
1 parent 2e53bc2 commit 0a4ff7d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,24 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
15861586
return std::nullopt;
15871587
}
15881588

1589+
if (constraint->getKind() == ConstraintKind::LValueObject) {
1590+
// Allow l-value type inference from its object type, but
1591+
// not the other way around, that would be handled by constraint
1592+
// simplification.
1593+
if (kind == AllowedBindingKind::Subtypes) {
1594+
if (type->isTypeVariableOrMember())
1595+
return std::nullopt;
1596+
1597+
type = LValueType::get(type);
1598+
} else {
1599+
// Right-hand side of the l-value object constraint can only
1600+
// be bound via constraint simplification when l-value type
1601+
// is inferred or contextually from other constraints.
1602+
DelayedBy.push_back(constraint);
1603+
return std::nullopt;
1604+
}
1605+
}
1606+
15891607
// If the source of the binding is 'OptionalObject' constraint
15901608
// and type variable is on the left-hand side, that means
15911609
// that it _has_ to be of optional type, since the right-hand
@@ -1757,7 +1775,8 @@ void PotentialBindings::infer(Constraint *constraint) {
17571775
case ConstraintKind::ArgumentConversion:
17581776
case ConstraintKind::OperatorArgumentConversion:
17591777
case ConstraintKind::OptionalObject:
1760-
case ConstraintKind::UnresolvedMemberChainBase: {
1778+
case ConstraintKind::UnresolvedMemberChainBase:
1779+
case ConstraintKind::LValueObject: {
17611780
auto binding = inferFromRelational(constraint);
17621781
if (!binding)
17631782
break;
@@ -1797,11 +1816,6 @@ void PotentialBindings::infer(Constraint *constraint) {
17971816
// Constraints from which we can't do anything.
17981817
break;
17991818

1800-
case ConstraintKind::LValueObject: {
1801-
DelayedBy.push_back(constraint);
1802-
break;
1803-
}
1804-
18051819
// For now let's avoid inferring protocol requirements from
18061820
// this constraint, but in the future we could do that to
18071821
// to filter bindings.

0 commit comments

Comments
 (0)