Skip to content

Commit 2e53bc2

Browse files
committed
[CSSimplify] Implement LValueObject constraint simplification
1 parent 5a93255 commit 2e53bc2

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,23 +3510,10 @@ namespace {
35103510
} else {
35113511
auto *locator = CS.getConstraintLocator(expr);
35123512

3513-
auto isOrCanBeLValueType = [](Type type) {
3514-
if (auto *typeVar = type->getAs<TypeVariableType>()) {
3515-
return typeVar->getImpl().canBindToLValue();
3516-
}
3517-
return type->is<LValueType>();
3518-
};
3519-
35203513
auto exprType = CS.getType(expr);
3521-
if (!isOrCanBeLValueType(exprType)) {
3522-
// Pretend that destination is an l-value type.
3523-
exprType = LValueType::get(exprType);
3524-
(void)CS.recordFix(TreatRValueAsLValue::create(CS, locator));
3525-
}
3526-
35273514
auto *destTy = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
3528-
CS.addConstraint(ConstraintKind::Bind, LValueType::get(destTy),
3529-
exprType, locator);
3515+
CS.addConstraint(ConstraintKind::LValueObject, exprType, destTy,
3516+
locator);
35303517
return destTy;
35313518
}
35323519
}

lib/Sema/CSSimplify.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14155,7 +14155,48 @@ ConstraintSystem::SolutionKind
1415514155
ConstraintSystem::simplifyLValueObjectConstraint(
1415614156
Type type1, Type type2, TypeMatchOptions flags,
1415714157
ConstraintLocatorBuilder locator) {
14158-
return SolutionKind::Error;
14158+
auto lvalueTy = simplifyType(type1);
14159+
14160+
auto formUnsolved = [&]() {
14161+
// If we're supposed to generate constraints, do so.
14162+
if (flags.contains(TMF_GenerateConstraints)) {
14163+
auto *lvalueObject =
14164+
Constraint::create(*this, ConstraintKind::LValueObject,
14165+
type1, type2, getConstraintLocator(locator));
14166+
14167+
addUnsolvedConstraint(lvalueObject);
14168+
return SolutionKind::Solved;
14169+
}
14170+
14171+
return SolutionKind::Unsolved;
14172+
};
14173+
14174+
auto isOrCanBeLValueType = [](Type type) {
14175+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
14176+
return typeVar->getImpl().canBindToLValue();
14177+
}
14178+
return type->is<LValueType>();
14179+
};
14180+
14181+
if (!isOrCanBeLValueType(lvalueTy)) {
14182+
if (!shouldAttemptFixes())
14183+
return SolutionKind::Error;
14184+
14185+
if (recordFix(
14186+
TreatRValueAsLValue::create(*this, getConstraintLocator(locator))))
14187+
return SolutionKind::Error;
14188+
14189+
lvalueTy = LValueType::get(lvalueTy);
14190+
}
14191+
14192+
if (lvalueTy->isTypeVariableOrMember())
14193+
return formUnsolved();
14194+
14195+
// TODO: This operation deserves its own locator just like OptionalObject.
14196+
addConstraint(ConstraintKind::Equal,
14197+
lvalueTy->castTo<LValueType>()->getObjectType(), type2,
14198+
getConstraintLocator(locator));
14199+
return SolutionKind::Solved;
1415914200
}
1416014201

1416114202
static llvm::PointerIntPair<Type, 3, unsigned>

0 commit comments

Comments
 (0)