Skip to content

[5.1] Don't use implicit dynamic on declarations if they are transpar… #25011

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
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/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,14 @@ TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(const Decl *D) {
return None;
}

static bool shouldBlockImplicitDynamic(Decl *D) {
if (D->getAttrs().hasAttribute<NonObjCAttr>() ||
D->getAttrs().hasAttribute<SILGenNameAttr>() ||
D->getAttrs().hasAttribute<TransparentAttr>() ||
D->getAttrs().hasAttribute<InlinableAttr>())
return true;
return false;
}
void TypeChecker::addImplicitDynamicAttribute(Decl *D) {
if (!D->getModuleContext()->isImplicitDynamicEnabled())
return;
Expand All @@ -2618,10 +2626,9 @@ void TypeChecker::addImplicitDynamicAttribute(Decl *D) {
isa<AccessorDecl>(D))
return;

if (D->getAttrs().hasAttribute<NonObjCAttr>() ||
D->getAttrs().hasAttribute<TransparentAttr>() ||
D->getAttrs().hasAttribute<InlinableAttr>())
return;
// Don't add dynamic if decl is inlinable or tranparent.
if (shouldBlockImplicitDynamic(D))
return;

if (auto *FD = dyn_cast<FuncDecl>(D)) {
// Don't add dynamic to defer bodies.
Expand All @@ -2632,6 +2639,14 @@ void TypeChecker::addImplicitDynamicAttribute(Decl *D) {
return;
}

// Don't add dynamic if accessor is inlinable or tranparent.
if (auto *asd = dyn_cast<AbstractStorageDecl>(D)) {
for (auto *accessor : asd->getAllAccessors()) {
if (!accessor->isImplicit() && shouldBlockImplicitDynamic(accessor))
return;
}
}

if (auto *VD = dyn_cast<VarDecl>(D)) {
// Don't turn stored into computed properties. This could conflict with
// exclusivity checking.
Expand Down
57 changes: 57 additions & 0 deletions test/attr/implicit_dynamic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// RUN: %target-swift-frontend -swift-version 5 -enable-implicit-dynamic -I %t -emit-silgen %s | %FileCheck %s

// Make sure that these functions are not implicitly marked dynamic.

public struct NotImplicitDynamic {
@inlinable
public var x : Int {
// CHECK: sil [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV1xSivg
// CHECK: sil [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV1xSivs
get {
return 1
}
set {
}
}

@inlinable
public var y : Int {
// CHECK: sil [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV1ySivg
return 1
}

public var z : Int {
// CHECK: sil [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV1zSivg
// CHECK: sil [ossa] @$s16implicit_dynamic18NotImplicitDynamicV1zSivs
@inlinable
get {
return 1
}
set {
}
}

@_transparent
public var x2 : Int {
// CHECK: sil [transparent] [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV2x2Sivg
// CHECK: sil [transparent] [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicV2x2Sivs
get {
return 1
}
set {
}
}

public subscript() -> Int {
// CHECK: sil [transparent] [serialized] [ossa] @$s16implicit_dynamic18NotImplicitDynamicVSiycig
@_transparent
get{
return 1
}
}
}

// CHECK: sil [ossa] @foobar
@_silgen_name("foobar")
public func noImplicitDynamicFunc() {
}