Skip to content

Commit 123b356

Browse files
Merge pull request swiftlang#35247 from LucianoPAlmeida/SR-14003-Self
[Sema][Diagnostics] Avoid recording treat as rvalue fix if destination expr type is a type var that can bind to lvalue
2 parents 4083cbb + 1c94fb4 commit 123b356

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,7 +3748,12 @@ bool ConstraintSystem::repairFailures(
37483748
// a property with a function type or a method reference,
37493749
// e.g. `foo.bar = 42` neither can be used if the destination
37503750
// is not l-value.
3751-
if (!getType(destExpr)->is<LValueType>() && rhs->is<FunctionType>()) {
3751+
auto destType = getType(destExpr);
3752+
auto destTypeVar = destType->getAs<TypeVariableType>();
3753+
bool destIsOrCanBindToLValue =
3754+
destType->is<LValueType>() ||
3755+
(destTypeVar && destTypeVar->getImpl().canBindToLValue());
3756+
if (!destIsOrCanBindToLValue && rhs->is<FunctionType>()) {
37523757
conversionsOrFixes.push_back(
37533758
TreatRValueAsLValue::create(*this, getConstraintLocator(locator)));
37543759
return true;
@@ -3772,9 +3777,9 @@ bool ConstraintSystem::repairFailures(
37723777
TMF_ApplyingFix, locator);
37733778

37743779
auto *loc = getConstraintLocator(locator);
3775-
if (getType(destExpr)->is<LValueType>() || result.isFailure()) {
3776-
// Let this asignment failure be diagnosed by the AllowTupleTypeMismatch
3777-
// fix already recorded.
3780+
if (destIsOrCanBindToLValue || result.isFailure()) {
3781+
// Let this assignment failure be diagnosed by the
3782+
// AllowTupleTypeMismatch fix already recorded.
37783783
if (hasFixFor(loc, FixKind::AllowTupleTypeMismatch))
37793784
return true;
37803785

test/Constraints/assignment.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ func f23798944() {
7777

7878
func returnsVoid() {}
7979
_ = returnsVoid() // expected-warning {{using '_' to ignore the result of a Void-returning function is redundant}}{{1-5=}}
80+
81+
// SR-14003
82+
class SR14003 {
83+
var callback: ((SR14003) -> Void)!
84+
85+
func setCallback(_ callback: @escaping (Self) -> Void) {
86+
self.callback = callback // expected-error {{cannot assign value of type '(Self) -> Void' to type '((SR14003) -> Void)?'}}
87+
}
88+
}

0 commit comments

Comments
 (0)