From 843ce585a3b6ea93e7494de6866780a136272016 Mon Sep 17 00:00:00 2001 From: Greg Titus Date: Sun, 9 Jun 2024 16:11:19 -0700 Subject: [PATCH] Set fix impact for non-settable overload choices higher, so we get diagnoses from better choices. --- lib/Sema/CSSimplify.cpp | 7 ++++++- test/Sema/immutability.swift | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 9a9b2cdb365a4..a6c5234afda80 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -15201,7 +15201,12 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint( locator.endsWith()) ? 1 : 0; - + // An overload choice that isn't settable is least interesting for diagnosis. + if (auto overload = findSelectedOverloadFor(getCalleeLocator(fix->getLocator()))) { + if (auto *var = dyn_cast_or_null(overload->choice.getDeclOrNull())) { + impact += !var->isSettableInSwift(DC) ? 1 : 0; + } + } return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved; } diff --git a/test/Sema/immutability.swift b/test/Sema/immutability.swift index 00026f169983a..7669a9275b74d 100644 --- a/test/Sema/immutability.swift +++ b/test/Sema/immutability.swift @@ -762,3 +762,16 @@ struct S2 { // expected-note@-1 {{add explicit 'S2.' to refer to mutable static property of 'S2'}} {{5-5=S2.}} } } + +// SR-3680, https://github.com/apple/swift/issues/46265 +protocol HasFoo { + var foo: String { get } +} +protocol CanSetFoo { + var foo: String { get set } +} +extension HasFoo where Self: CanSetFoo { + func bar() { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-3=mutating }} + self.foo = "bar" // expected-error {{cannot assign to property: 'self' is immutable}} + } +}