Skip to content

Commit 997f410

Browse files
authored
Merge pull request #36040 from slavapestov/vtable-layout-usableFromInline-5.4
SIL: Fix vtable layout with @usableFromInline methods [5.4]
2 parents 164021c + f178b6e commit 997f410

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

include/swift/SIL/SILVTableVisitor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ template <class T> class SILVTableVisitor {
120120
// replace the vtable entry for B.f(); a call to A.f()
121121
// will correctly dispatch to the implementation of B.f()
122122
// in the subclass.
123+
auto *UseDC = declRef.getDecl()->getDeclContext();
123124
if (!baseRef.getDecl()->isAccessibleFrom(
124-
declRef.getDecl()->getDeclContext()))
125+
UseDC,
126+
/*forConformance=*/false,
127+
/*allowUsableFromInline=*/true))
125128
break;
126129

127130
asDerived().addMethodOverride(baseRef, declRef);

lib/AST/Decl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3417,13 +3417,16 @@ static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
34173417
}
34183418

34193419
bool ValueDecl::isMoreVisibleThan(ValueDecl *other) const {
3420-
auto scope = getFormalAccessScope();
3420+
auto scope = getFormalAccessScope(/*UseDC=*/nullptr,
3421+
/*treatUsableFromInlineAsPublic=*/true);
34213422

34223423
// 'other' may have come from a @testable import, so we need to upgrade it's
34233424
// visibility to public here. That is not the same as whether 'other' is
34243425
// being built with -enable-testing though -- we don't want to treat it
34253426
// differently in that case.
3426-
auto otherScope = other->getFormalAccessScope(getDeclContext());
3427+
auto otherScope =
3428+
other->getFormalAccessScope(getDeclContext(),
3429+
/*treatUsableFromInlineAsPublic=*/true);
34273430

34283431
if (scope.isPublic())
34293432
return !otherScope.isPublic();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
open class Base {
2+
internal func internalMethod() {}
3+
@usableFromInline internal func usableFromInlineMethod() {}
4+
}
5+
6+
open class Middle : Base {
7+
open override func internalMethod() {}
8+
open override func usableFromInlineMethod() {}
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -emit-silgen %S/Inputs/accessibility_vtables_usableFromInline_helper.swift | %FileCheck %s --check-prefix=LIBRARY
4+
// RUN: %target-swift-frontend -enable-library-evolution -emit-silgen %S/Inputs/accessibility_vtables_usableFromInline_helper.swift | %FileCheck %s --check-prefix=LIBRARY
5+
6+
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/accessibility_vtables_usableFromInline_helper.swift
7+
// RUN: %target-swift-emit-silgen -primary-file %s -I %t | %FileCheck %s --check-prefix=FRAGILE-CLIENT
8+
9+
// RUN: %target-swift-frontend -enable-library-evolution -emit-module -o %t %S/Inputs/accessibility_vtables_usableFromInline_helper.swift
10+
// RUN: %target-swift-emit-silgen -primary-file %s -I %t | %FileCheck %s --check-prefix=RESILIENT-CLIENT
11+
12+
import accessibility_vtables_usableFromInline_helper
13+
14+
public class Derived : Middle {
15+
open override func internalMethod() {}
16+
open override func usableFromInlineMethod() {}
17+
}
18+
19+
// LIBRARY-LABEL: sil_vtable {{(\[serialized\] )?}}Base {
20+
// LIBRARY-NEXT: #Base.internalMethod: (Base) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper4BaseC14internalMethodyyF
21+
// LIBRARY-NEXT: #Base.usableFromInlineMethod: (Base) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper4BaseC0cdE6MethodyyF
22+
// LIBRARY-NEXT: #Base.init!allocator: (Base.Type) -> () -> Base : @$s45accessibility_vtables_usableFromInline_helper4BaseCACycfC
23+
// LIBRARY-NEXT: #Base.deinit!deallocator: @$s45accessibility_vtables_usableFromInline_helper4BaseCfD
24+
// LIBRARY-NEXT: }
25+
26+
// LIBRARY-LABEL: sil_vtable {{(\[serialized\] )?}}Middle {
27+
// LIBRARY-NEXT: #Base.internalMethod: (Base) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper6MiddleC14internalMethodyyFAA4BaseCADyyFTV [override]
28+
// LIBRARY-NEXT: #Base.usableFromInlineMethod: (Base) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper6MiddleC0cdE6MethodyyF [override]
29+
// LIBRARY-NEXT: #Base.init!allocator: (Base.Type) -> () -> Base : @$s45accessibility_vtables_usableFromInline_helper6MiddleCACycfC [override]
30+
// LIBRARY-NEXT: #Middle.internalMethod: (Middle) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper6MiddleC14internalMethodyyF
31+
// LIBRARY-NEXT: #Middle.deinit!deallocator: @$s45accessibility_vtables_usableFromInline_helper6MiddleCfD
32+
// LIBRARY-NEXT: }
33+
34+
// FRAGILE-CLIENT-LABEL: sil_vtable [serialized] Derived {
35+
// FRAGILE-CLIENT-NEXT: #Base.internalMethod: (Base) -> () -> () : @$s45accessibility_vtables_usableFromInline_helper6MiddleC14internalMethodyyFAA4BaseCADyyFTV [inherited]
36+
// FRAGILE-CLIENT-NEXT: #Base.usableFromInlineMethod: (Base) -> () -> () : @$s38accessibility_vtables_usableFromInline7DerivedC0cdE6MethodyyF [override]
37+
// FRAGILE-CLIENT-NEXT: #Base.init!allocator: (Base.Type) -> () -> Base : @$s45accessibility_vtables_usableFromInline_helper6MiddleCACycfC [inherited]
38+
// FRAGILE-CLIENT-NEXT: #Middle.internalMethod: (Middle) -> () -> () : @$s38accessibility_vtables_usableFromInline7DerivedC14internalMethodyyF [override]
39+
// FRAGILE-CLIENT-NEXT: #Derived.deinit!deallocator: @$s38accessibility_vtables_usableFromInline7DerivedCfD
40+
41+
// FRAGILE-CLIENT-NEXT: }
42+
43+
// RESILIENT-CLIENT-LABEL: sil_vtable [serialized] Derived {
44+
// RESILIENT-CLIENT-NEXT: #Base.usableFromInlineMethod: (Base) -> () -> () : @$s38accessibility_vtables_usableFromInline7DerivedC0cdE6MethodyyF [override]
45+
// RESILIENT-CLIENT-NEXT: #Middle.internalMethod: (Middle) -> () -> () : @$s38accessibility_vtables_usableFromInline7DerivedC14internalMethodyyF [override]
46+
// RESILIENT-CLIENT-NEXT: #Derived.deinit!deallocator: @$s38accessibility_vtables_usableFromInline7DerivedCfD
47+
// RESILIENT-CLIENT-NEXT: }

0 commit comments

Comments
 (0)