Skip to content
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
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,13 @@ ERROR(attr_objc_implementation_must_be_imported,none,
"defined by a Swift 'class' declaration, not an imported Objective-C "
"'@interface' declaration",
(ValueDecl *))
ERROR(attr_objc_implementation_must_have_super,none,
"'@_objcImplementation' cannot be used to implement root %kind0; declare "
"its superclass in the header",
(ValueDecl *))
ERROR(objc_implementation_cannot_have_generics,none,
"'@_objcImplementation' cannot be used to implement %kind0",
(ValueDecl *))
ERROR(attr_objc_implementation_category_not_found,none,
"could not find category %0 on Objective-C class %1; make sure your "
"umbrella or bridging header imports the header that declares it",
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,8 @@ llvm::Optional<Identifier>
ExtensionDecl::getCategoryNameForObjCImplementation() const {
assert(isObjCImplementation());

auto attr = getAttrs().getAttribute<ObjCImplementationAttr>();
auto attr = getAttrs()
.getAttribute<ObjCImplementationAttr>(/*AllowInvalid=*/true);
if (attr->isCategoryNameInvalid())
return llvm::None;

Expand Down
13 changes: 13 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,19 @@ visitObjCImplementationAttr(ObjCImplementationAttr *attr) {
return;
}

if (!CD->hasSuperclass()) {
diagnoseAndRemoveAttr(attr, diag::attr_objc_implementation_must_have_super,
CD);
CD->diagnose(diag::decl_declared_here, CD);
return;
}

if (CD->isTypeErasedGenericClass()) {
diagnoseAndRemoveAttr(attr, diag::objc_implementation_cannot_have_generics,
CD);
CD->diagnose(diag::decl_declared_here, CD);
}

if (!attr->isCategoryNameInvalid() && !ED->getImplementedObjCDecl()) {
diagnose(attr->getLocation(),
diag::attr_objc_implementation_category_not_found,
Expand Down
8 changes: 8 additions & 0 deletions test/decl/ext/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@

@end

@interface ObjCImplRootClass

@end

@interface ObjCImplGenericClass<T> : NSObject

@end



struct ObjCStruct {
Expand Down
8 changes: 8 additions & 0 deletions test/decl/ext/objc_implementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,14 @@ protocol EmptySwiftProto {}
@_objcImplementation(WTF) extension SwiftClass {} // expected
// expected-error@-1 {{'@_objcImplementation' cannot be used to extend class 'SwiftClass' because it was defined by a Swift 'class' declaration, not an imported Objective-C '@interface' declaration}} {{1-27=}}

@_objcImplementation extension ObjCImplRootClass {
// expected-error@-1 {{'@_objcImplementation' cannot be used to implement root class 'ObjCImplRootClass'; declare its superclass in the header}}
}

@_objcImplementation extension ObjCImplGenericClass {
// expected-error@-1 {{'@_objcImplementation' cannot be used to implement generic class 'ObjCImplGenericClass'}}
}

func usesAreNotAmbiguous(obj: ObjCClass) {
obj.method(fromHeader1: 1)
obj.method(fromHeader2: 2)
Expand Down