Skip to content

Commit 45a3a53

Browse files
committed
[CSGen] If assignment destination is r-value record fix early
If type of the assignment destination expression has been determined to be a r-value type or a type variable which can't be bound to an l-value type, let's fix the type and record a fix for that early.
1 parent dda0167 commit 45a3a53

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/Sema/CSGen.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,10 +2757,25 @@ namespace {
27572757
}
27582758
return TupleType::get(destTupleTypes, CS.getASTContext());
27592759
} else {
2760-
Type destTy = CS.createTypeVariable(CS.getConstraintLocator(expr),
2761-
TVO_CanBindToNoEscape);
2762-
CS.addConstraint(ConstraintKind::Bind, LValueType::get(destTy), CS.getType(expr),
2763-
CS.getConstraintLocator(expr));
2760+
auto *locator = CS.getConstraintLocator(expr);
2761+
2762+
auto isOrCanBeLValueType = [](Type type) {
2763+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
2764+
return typeVar->getImpl().canBindToLValue();
2765+
}
2766+
return type->is<LValueType>();
2767+
};
2768+
2769+
auto exprType = CS.getType(expr);
2770+
if (!isOrCanBeLValueType(exprType)) {
2771+
// Pretend that destination is an l-value type.
2772+
exprType = LValueType::get(exprType);
2773+
(void)CS.recordFix(TreatRValueAsLValue::create(CS, locator));
2774+
}
2775+
2776+
auto *destTy = CS.createTypeVariable(locator, TVO_CanBindToNoEscape);
2777+
CS.addConstraint(ConstraintKind::Bind, LValueType::get(destTy),
2778+
exprType, locator);
27642779
return destTy;
27652780
}
27662781
}

0 commit comments

Comments
 (0)