Skip to content

[Sema] Setter has incorrect mutating-ness inside class-constrained protocol extension #26669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1964,8 +1964,17 @@ void TypeChecker::validateDecl(OperatorDecl *OD) {
}

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

Expand Down
20 changes: 20 additions & 0 deletions test/decl/ext/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,23 @@ struct WrapperContext {
static let propUsingMember = originalValue
}
}

// SR-11298

protocol SR_11298_P {}

class SR_11298_C: SR_11298_P {
var property: String = ""
}

// Self: SR_11298_C requirement constrains this extension to SR_11298C and its subclasses.
// Since this implies a class constraint, the setter should be implicitly nonmutating.
extension SR_11298_P where Self: SR_11298_C {
var wrappingProperty: String {
get { return property }
set { property = newValue }
}
}

let instance = SR_11298_C()
instance.wrappingProperty = "" // Okay