Skip to content

[6.0] [Sema] Let protocols define getter requirements for @_staticExclusiveOnly types #75296

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 1 commit into from
Jul 17, 2024
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,8 @@ ERROR(attr_static_exclusive_only_let_only_param,none,
"parameter of type %0 must be declared as either 'borrowing' or 'consuming'", (Type))
ERROR(attr_static_exclusive_only_mutating,none,
"type %0 cannot have mutating function %1", (Type, ValueDecl *))
ERROR(attr_static_exclusive_no_setters,none,
"variable of type %0 must not have a setter", (Type))

// @extractConstantsFromMembers
ERROR(attr_extractConstantsFromMembers_experimental,none,
Expand Down
30 changes: 21 additions & 9 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2585,13 +2585,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

TypeChecker::checkDeclAttributes(VD);

auto DC = VD->getDeclContext();

if (!checkOverrides(VD)) {
// If a property has an override attribute but does not override
// anything, complain.
auto overridden = VD->getOverriddenDecl();
if (auto *OA = VD->getAttrs().getAttribute<OverrideAttr>()) {
if (!overridden) {
auto DC = VD->getDeclContext();
auto isClassContext = DC->getSelfClassDecl() != nullptr;
auto isStructOrEnumContext = DC->getSelfEnumDecl() != nullptr ||
DC->getSelfStructDecl() != nullptr;
Expand Down Expand Up @@ -2646,15 +2647,26 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

// @_staticExclusiveOnly types cannot be put into 'var's, only 'let'.
if (auto SD = VD->getInterfaceType()->getStructOrBoundGenericStruct()) {
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>() &&
!VD->isLet()) {
if (SD->getAttrs().hasAttribute<StaticExclusiveOnlyAttr>()) {
auto isProtocolContext = isa<ProtocolDecl>(DC);

if (isProtocolContext && !VD->supportsMutation()) {
return;
}

if (VD->isLet()) {
return;
}

auto diagMsg = isProtocolContext
? diag::attr_static_exclusive_no_setters
: diag::attr_static_exclusive_only_let_only;

Ctx.Diags.diagnoseWithNotes(
VD->diagnose(diag::attr_static_exclusive_only_let_only,
VD->getInterfaceType()),
[&]() {
SD->diagnose(diag::attr_static_exclusive_only_type_nonmutating,
SD->getDeclaredInterfaceType());
});
VD->diagnose(diagMsg, VD->getInterfaceType()), [&]() {
SD->diagnose(diag::attr_static_exclusive_only_type_nonmutating,
SD->getDeclaredInterfaceType());
});
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/attr/attr_static_exclusive_only.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ struct B: ~Copyable { // expected-note {{'B' is a non-mutable type}}
// expected-note@-2 {{'B' is a non-mutable type}}
// expected-note@-3 {{'B' is a non-mutable type}}
// expected-note@-4 {{'B' is a non-mutable type}}
// expected-note@-5 {{'B' is a non-mutable type}}
// expected-note@-6 {{'B' is a non-mutable type}}
// expected-note@-7 {{'B' is a non-mutable type}}
mutating func change() { // expected-error {{type 'B' cannot have mutating function 'change()'}}
print("123")
}
Expand Down Expand Up @@ -68,3 +71,31 @@ func p(_: (consuming B) -> ()) {} // OK
struct Q<T>: ~Copyable {} // expected-note {{'Q<T>' is a non-mutable type}}

var r0 = Q<Int>() // expected-error {{variable of type 'Q<Int>' must be declared with a 'let'}}

protocol S {
var t0: B { get } // OK

var t1: B { get set } // expected-error {{variable of type 'B' must not have a setter}}
}

protocol U: ~Copyable {
var v: B { get } // OK
}

struct W: ~Copyable {}

extension W: U {
var v: B { // expected-error {{variable of type 'B' must be declared with a 'let'}}
B()
}
}

struct X: ~Copyable, U {
let v: B // OK
}

extension U {
var y: B { // expected-error {{variable of type 'B' must be declared with a 'let'}}
B()
}
}