Skip to content

Commit ef7300a

Browse files
author
Greg Titus
committed
Set fix impact for non-settable overload choices higher, so we get diagnoses from better choices.
1 parent b07a2aa commit ef7300a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15201,7 +15201,16 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1520115201
locator.endsWith<LocatorPathElt::SubscriptMember>())
1520215202
? 1
1520315203
: 0;
15204-
15204+
// An overload choice that isn't settable is least interesting for diagnosis.
15205+
if (auto overload = findSelectedOverloadFor(getCalleeLocator(fix->getLocator()))) {
15206+
if (auto decl = overload->choice.getDeclOrNull()) {
15207+
if (auto VD = dyn_cast<VarDecl>(decl)) {
15208+
if (!VD->isSettableInSwift(DC)) {
15209+
impact += 10;
15210+
}
15211+
}
15212+
}
15213+
}
1520515214
return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved;
1520615215
}
1520715216

test/Sema/immutability.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,16 @@ struct S2<T> {
762762
// expected-note@-1 {{add explicit 'S2<T>.' to refer to mutable static property of 'S2<T>'}} {{5-5=S2<T>.}}
763763
}
764764
}
765+
766+
// SR-3680, https://github.com/apple/swift/issues/46265
767+
protocol HasFoo {
768+
var foo: String { get }
769+
}
770+
protocol CanSetFoo {
771+
var foo: String { get set }
772+
}
773+
extension HasFoo where Self: CanSetFoo {
774+
func bar() { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-3=mutating }}
775+
self.foo = "bar" // expected-error {{cannot assign to property: 'self' is immutable}}
776+
}
777+
}

0 commit comments

Comments
 (0)