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
15 changes: 14 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,16 +2104,29 @@ static void inferDynamic(ASTContext &ctx, ValueDecl *D) {
return;

// Only introduce 'dynamic' on declarations...
bool isNSManaged = D->getAttrs().hasAttribute<NSManagedAttr>();
if (!isa<ExtensionDecl>(D->getDeclContext())) {
// ...and in classes on decls marked @NSManaged.
if (!D->getAttrs().hasAttribute<NSManagedAttr>())
if (!isNSManaged)
return;
}

// The presence of 'dynamic' or 'final' blocks the inference of 'dynamic'.
if (D->isDynamic() || D->isFinal())
return;

// Variables declared with 'let' cannot be 'dynamic'.
if (auto VD = dyn_cast<VarDecl>(D)) {
if (VD->isLet() && !isNSManaged) return;
}

// The presence of 'final' on a class prevents 'dynamic'.
auto classDecl = D->getDeclContext()->getAsClassOrClassExtensionContext();
if (!classDecl) return;
if (!isNSManaged && classDecl->isFinal() &&
!classDecl->requiresStoredPropertyInits())
return;

// Add the 'dynamic' attribute.
D->getAttrs().add(new (ctx) DynamicAttr(/*isImplicit=*/true));
}
Expand Down
17 changes: 17 additions & 0 deletions test/ClangModules/objc_final_dynamic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-sil -I %S/Inputs/custom-modules %s

// REQUIRES: objc_interop

import Foundation

// Note: make sure we don't get a bogus error from nowhere,
// error: a declaration cannot be both 'final' and 'dynamic'
extension NSObject {
public static let staticIntProperty: Int = 17
}

final class MyClass : NSObject { }

extension MyClass {
public static var otherStaticIntProperty: Int = 17
}