Skip to content

Commit 4468d54

Browse files
committed
Sema: Diagnose '@objc' protocols containing associated types
Fixes <https://bugs.swift.org/browse/SR-3850>, <rdar://problem/32634928>.
1 parent 5de9aaf commit 4468d54

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,10 @@ WARNING(associated_type_override_typealias,none,
17491749
"associated type %0 is redundant with type %0 declared in inherited "
17501750
"%1 %2", (DeclName, DescriptiveDeclKind, Type))
17511751

1752+
ERROR(associated_type_objc,none,
1753+
"associated type %0 cannot be declared inside '@objc' protocol %1",
1754+
(DeclName, DeclName))
1755+
17521756
ERROR(generic_param_access,none,
17531757
"%0 %select{must be declared %select{"
17541758
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,6 +4299,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
42994299
void visitAssociatedTypeDecl(AssociatedTypeDecl *assocType) {
43004300
if (!assocType->hasValidationStarted())
43014301
TC.validateDecl(assocType);
4302+
4303+
auto *proto = assocType->getProtocol();
4304+
if (proto->isObjC()) {
4305+
TC.diagnose(assocType->getLoc(),
4306+
diag::associated_type_objc,
4307+
assocType->getName(),
4308+
proto->getName());
4309+
}
43024310
}
43034311

43044312
void checkUnsupportedNestedType(NominalTypeDecl *NTD) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift -disable-objc-attr-requires-foundation-module
2+
3+
@objc protocol P {
4+
associatedtype T
5+
// expected-error@-1 {{associated type 'T' cannot be declared inside '@objc' protocol 'P'}}
6+
}
7+
8+
extension P {
9+
func takesT(_: T) {}
10+
}

test/decl/protocol/req/optional.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ protocol optErrorProtocol {
224224
optional func foo(_ x: Int) // expected-error{{'optional' requirements are an Objective-C compatibility feature; add '@objc'}}{{3-3=@objc }}
225225
optional var bar: Int { get } // expected-error{{'optional' requirements are an Objective-C compatibility feature; add '@objc'}}{{3-3=@objc }}
226226
optional associatedtype Assoc // expected-error{{'optional' modifier cannot be applied to this declaration}} {{3-12=}}
227+
// expected-error@-1 {{associated type 'Assoc' cannot be declared inside '@objc' protocol 'optObjcAttributeProtocol'}}
227228
}
228229

229230
@objc protocol optionalInitProto {

0 commit comments

Comments
 (0)