Skip to content

Diagnose associated types inside '@objc' protocols #11764

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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,10 @@ WARNING(associated_type_override_typealias,none,
"associated type %0 is redundant with type %0 declared in inherited "
"%1 %2", (DeclName, DescriptiveDeclKind, Type))

ERROR(associated_type_objc,none,
"associated type %0 cannot be declared inside '@objc' protocol %1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rephrase this to make it clearer that they're not allowed at all? In particular, mentioning the names makes it sounds like a problem specific to this protocol or this associated type.

(DeclName, DeclName))

ERROR(generic_param_access,none,
"%0 %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
Expand Down
3 changes: 0 additions & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2720,9 +2720,6 @@ void ArchetypeType::populateNestedTypes() const {
llvm::SmallPtrSet<Identifier, 4> knownNestedTypes;
ProtocolType::visitAllProtocols(getConformsTo(),
[&](ProtocolDecl *proto) -> bool {
// Objective-C protocols don't have type members.
if (proto->isObjC()) return false;

for (auto member : proto->getMembers()) {
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member)) {
if (knownNestedTypes.insert(assocType->getName()).second)
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4299,6 +4299,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
void visitAssociatedTypeDecl(AssociatedTypeDecl *assocType) {
if (!assocType->hasValidationStarted())
TC.validateDecl(assocType);

auto *proto = assocType->getProtocol();
if (proto->isObjC()) {
TC.diagnose(assocType->getLoc(),
diag::associated_type_objc,
assocType->getName(),
proto->getName());
}
}

void checkUnsupportedNestedType(NominalTypeDecl *NTD) {
Expand Down
10 changes: 10 additions & 0 deletions test/decl/protocol/req/associated_type_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift -disable-objc-attr-requires-foundation-module

@objc protocol P {
associatedtype T
// expected-error@-1 {{associated type 'T' cannot be declared inside '@objc' protocol 'P'}}
}

extension P {
func takesT(_: T) {}
}
1 change: 1 addition & 0 deletions test/decl/protocol/req/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ protocol optErrorProtocol {
optional func foo(_ x: Int) // expected-error{{'optional' requirements are an Objective-C compatibility feature; add '@objc'}}{{3-3=@objc }}
optional var bar: Int { get } // expected-error{{'optional' requirements are an Objective-C compatibility feature; add '@objc'}}{{3-3=@objc }}
optional associatedtype Assoc // expected-error{{'optional' modifier cannot be applied to this declaration}} {{3-12=}}
// expected-error@-1 {{associated type 'Assoc' cannot be declared inside '@objc' protocol 'optObjcAttributeProtocol'}}
}

@objc protocol optionalInitProto {
Expand Down