Skip to content

Commit afd249b

Browse files
authored
Merge pull request #26669 from theblixguy/fix/SR-11298
[Sema] Setter has incorrect mutating-ness inside class-constrained protocol extension
2 parents 2d86f23 + 304449b commit afd249b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,8 +1964,17 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {
19641964
}
19651965

19661966
bool swift::doesContextHaveValueSemantics(DeclContext *dc) {
1967-
if (Type contextTy = dc->getDeclaredInterfaceType())
1968-
return !contextTy->hasReferenceSemantics();
1967+
if (Type contextTy = dc->getDeclaredInterfaceType()) {
1968+
// If the decl context is an extension, then it could be imposing a class
1969+
// constraint (ex: where Self: SomeClass). Make sure we include that
1970+
// in our check as well.
1971+
auto extensionRequiresClass = false;
1972+
if (auto ED = dyn_cast<ExtensionDecl>(dc)) {
1973+
extensionRequiresClass =
1974+
ED->getGenericSignature()->requiresClass(ED->getSelfInterfaceType());
1975+
}
1976+
return !contextTy->hasReferenceSemantics() && !extensionRequiresClass;
1977+
}
19691978
return false;
19701979
}
19711980

test/decl/ext/extensions.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,23 @@ struct WrapperContext {
124124
static let propUsingMember = originalValue
125125
}
126126
}
127+
128+
// SR-11298
129+
130+
protocol SR_11298_P {}
131+
132+
class SR_11298_C: SR_11298_P {
133+
var property: String = ""
134+
}
135+
136+
// Self: SR_11298_C requirement constrains this extension to SR_11298C and its subclasses.
137+
// Since this implies a class constraint, the setter should be implicitly nonmutating.
138+
extension SR_11298_P where Self: SR_11298_C {
139+
var wrappingProperty: String {
140+
get { return property }
141+
set { property = newValue }
142+
}
143+
}
144+
145+
let instance = SR_11298_C()
146+
instance.wrappingProperty = "" // Okay

0 commit comments

Comments
 (0)