Skip to content

Sema: Don't infer 'dynamic' for static methods and properties #10473

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
Jun 22, 2017
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
23 changes: 19 additions & 4 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1245,12 +1245,14 @@ static void validatePatternBindingEntries(TypeChecker &tc,

void swift::makeFinal(ASTContext &ctx, ValueDecl *D) {
if (D && !D->isFinal()) {
assert(!D->isDynamic());
D->getAttrs().add(new (ctx) FinalAttr(/*IsImplicit=*/true));
}
}

void swift::makeDynamic(ASTContext &ctx, ValueDecl *D) {
if (D && !D->isDynamic()) {
assert(!D->isFinal());
D->getAttrs().add(new (ctx) DynamicAttr(/*IsImplicit=*/true));
}
}
Expand Down Expand Up @@ -2556,15 +2558,29 @@ static void inferDynamic(ASTContext &ctx, ValueDecl *D) {

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

// The presence of 'static' bocks the inference of 'dynamic'.
if (VD->isStatic() && staticSpelling == StaticSpellingKind::KeywordStatic)
return;

if (VD->isLet() && !isNSManaged)
return;
}

// Accessors should not infer 'dynamic' on their own; they can get it from
// their storage decls.
if (auto FD = dyn_cast<FuncDecl>(D))
if (auto FD = dyn_cast<FuncDecl>(D)) {
if (FD->isAccessor())
return;

auto staticSpelling = FD->getStaticSpelling();

// The presence of 'static' bocks the inference of 'dynamic'.
if (FD->isStatic() && staticSpelling == StaticSpellingKind::KeywordStatic)
return;
}

// The presence of 'final' on a class prevents 'dynamic'.
auto classDecl = D->getDeclContext()->getAsClassOrClassExtensionContext();
if (!classDecl) return;
Expand Down Expand Up @@ -5176,8 +5192,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

// If the storage is dynamic or final, propagate to this accessor.
if (isObjC &&
storage->isDynamic() &&
!storage->isFinal())
storage->isDynamic())
makeDynamic(TC.Context, FD);

if (storage->isFinal())
Expand Down
8 changes: 7 additions & 1 deletion test/attr/attr_dynamic_infer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension Sub {
@objc class FinalTests {}

extension FinalTests {
// CHECK: @objc final func foo
// CHECK: @objc final func foo
final func foo() { }

// CHECK: @objc final var prop: Super
Expand All @@ -77,5 +77,11 @@ extension FinalTests {
// CHECK: @objc final set
set { }
}

// CHECK: static @objc var x
static var x: Int = 0

// CHECK: @objc static func bar
static func bar() { }
}