Skip to content

Sema: Print property accessors for non-mutating setter requirements #27896

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
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
19 changes: 14 additions & 5 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2627,13 +2627,13 @@ printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,
Options.FunctionDefinitions = true;
Options.PrintAccessorBodiesInProtocols = true;

bool AdopterIsClass = Adopter->getSelfClassDecl() != nullptr;
// Skip 'mutating' only inside classes: mutating methods usually
// don't have a sensible non-mutating implementation.
bool isClass = Adopter->getSelfClassDecl() != nullptr;
if (isClass)
if (AdopterIsClass)
Options.ExcludeAttrList.push_back(DAK_Mutating);
// 'nonmutating' is only meaningful on value type member accessors.
if (isClass || !isa<AbstractStorageDecl>(Requirement))
if (AdopterIsClass || !isa<AbstractStorageDecl>(Requirement))
Options.ExcludeAttrList.push_back(DAK_NonMutating);

// FIXME: Once we support move-only types, remove this if the
Expand All @@ -2650,10 +2650,19 @@ printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,
};
Options.setBaseType(AdopterTy);
Options.CurrentModule = Adopter->getParentModule();
if (!isa<ExtensionDecl>(Adopter)) {
if (isa<NominalTypeDecl>(Adopter)) {
// Create a variable declaration instead of a computed property in
// nominal types
// nominal types...
Options.PrintPropertyAccessors = false;

// ...but a non-mutating setter requirement will force us into a
// computed property in non-class adopters; don't leave the user
// wondering why a conformance fails.
if (!AdopterIsClass)
if (const auto VD = dyn_cast<VarDecl>(Requirement))
if (const auto Set = VD->getAccessor(AccessorKind::Set))
if (Set->getAttrs().hasAttribute<NonMutatingAttr>())
Options.PrintPropertyAccessors = true;
}
Requirement->print(Printer, Options);
Printer << "\n";
Expand Down
25 changes: 20 additions & 5 deletions test/decl/protocol/conforms/fixit_stub_editor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class C2 : P4 {} // expected-error{{type 'C2' does not conform to protocol 'P4'}
// in structs.
// =============================================================================

protocol LocalMutabilityProto {
mutating func foo()
subscript() -> Int { get nonmutating set }
protocol MutabilityProto {
mutating func foo()
subscript() -> Int { get nonmutating set }
}

class Class1: LocalMutabilityProto { // expected-error{{type 'Class1' does not conform to protocol 'LocalMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{37-37=\n func foo() {\n <#code#>\n \}\n\n subscript() -> Int {\n get {\n <#code#>\n \}\n set {\n <#code#>\n \}\n \}\n}}
class Class1: MutabilityProto { // expected-error{{type 'Class1' does not conform to protocol 'MutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{32-32=\n func foo() {\n <#code#>\n \}\n\n subscript() -> Int {\n get {\n <#code#>\n \}\n set {\n <#code#>\n \}\n \}\n}}
}

struct Struct1: LocalMutabilityProto { // expected-error{{type 'Struct1' does not conform to protocol 'LocalMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{39-39=\n mutating func foo() {\n <#code#>\n \}\n\n subscript() -> Int {\n get {\n <#code#>\n \}\n nonmutating set {\n <#code#>\n \}\n \}\n}}
struct Struct1: MutabilityProto { // expected-error{{type 'Struct1' does not conform to protocol 'MutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{34-34=\n mutating func foo() {\n <#code#>\n \}\n\n subscript() -> Int {\n get {\n <#code#>\n \}\n nonmutating set {\n <#code#>\n \}\n \}\n}}
}

import fixit_stub_mutability_proto_module
Expand All @@ -57,3 +57,18 @@ class Class2: ExternalMutabilityProto { // expected-error{{type 'Class2' does no

struct Struct2: ExternalMutabilityProto { // expected-error{{type 'Struct2' does not conform to protocol 'ExternalMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{42-42=\n mutating func foo() {\n <#code#>\n \}\n\n subscript() -> Int {\n mutating get {\n <#code#>\n \}\n nonmutating set(newValue) {\n <#code#>\n \}\n \}\n}}
}

protocol PropertyMutabilityProto {
var computed: Int { mutating get nonmutating set }
var stored: Int { mutating get set }
}

class Class3: PropertyMutabilityProto { // expected-error{{type 'Class3' does not conform to protocol 'PropertyMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{40-40=\n var computed: Int\n\n var stored: Int\n}}
}

struct Struct3: PropertyMutabilityProto { // expected-error{{type 'Struct3' does not conform to protocol 'PropertyMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{42-42=\n var computed: Int {\n mutating get {\n <#code#>\n \}\n nonmutating set {\n <#code#>\n \}\n \}\n\n var stored: Int\n}}
}

class Class4 {}
extension Class4: PropertyMutabilityProto { // expected-error{{type 'Class4' does not conform to protocol 'PropertyMutabilityProto'}} expected-note{{do you want to add protocol stubs?}} {{44-44=\n var computed: Int {\n get {\n <#code#>\n \}\n set {\n <#code#>\n \}\n \}\n\n var stored: Int {\n get {\n <#code#>\n \}\n set {\n <#code#>\n \}\n \}\n}}
}