Skip to content

Commit 28ab565

Browse files
authored
'open' classes within 'public' structs should be considered open. (swiftlang#11997)
Without this, our hack to give all open class vtable members public linkage at the LLVM level wasn't kicking in, and subclasses from other modules were getting link errors. rdar://problem/32885384
1 parent 00e34e4 commit 28ab565

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/AST/Decl.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,17 +1973,31 @@ Accessibility ValueDecl::getEffectiveAccess() const {
19731973
break;
19741974
}
19751975

1976+
auto restrictToEnclosing =
1977+
[this](Accessibility effectiveAccess,
1978+
Accessibility enclosingAccess) -> Accessibility {
1979+
if (effectiveAccess == Accessibility::Open &&
1980+
enclosingAccess == Accessibility::Public &&
1981+
isa<NominalTypeDecl>(this)) {
1982+
// Special case: an open class may be contained in a public
1983+
// class/struct/enum. Leave effectiveAccess as is.
1984+
return effectiveAccess;
1985+
}
1986+
return std::min(effectiveAccess, enclosingAccess);
1987+
};
1988+
19761989
if (auto enclosingNominal = dyn_cast<NominalTypeDecl>(getDeclContext())) {
1977-
effectiveAccess = std::min(effectiveAccess,
1978-
enclosingNominal->getEffectiveAccess());
1990+
effectiveAccess =
1991+
restrictToEnclosing(effectiveAccess,
1992+
enclosingNominal->getEffectiveAccess());
19791993

19801994
} else if (auto enclosingExt = dyn_cast<ExtensionDecl>(getDeclContext())) {
19811995
// Just check the base type. If it's a constrained extension, Sema should
19821996
// have already enforced access more strictly.
19831997
if (auto extendedTy = enclosingExt->getExtendedType()) {
19841998
if (auto nominal = extendedTy->getAnyNominal()) {
1985-
effectiveAccess = std::min(effectiveAccess,
1986-
nominal->getEffectiveAccess());
1999+
effectiveAccess =
2000+
restrictToEnclosing(effectiveAccess, nominal->getEffectiveAccess());
19872001
}
19882002
}
19892003

test/IRGen/Inputs/vtable_symbol_linkage_base.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ open class Base {
1010
private var privateVar: Int = 29
1111
internal var internalVar: Int = 30
1212
}
13+
14+
15+
public struct Namespace {
16+
open class Nested {
17+
public init() {}
18+
var storedProp: Int?
19+
}
20+
}
21+
22+
extension Namespace {
23+
open class ExtNested {
24+
public init() {}
25+
var storedProp: Int?
26+
}
27+
}

test/IRGen/vtable_symbol_linkage.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ import BaseModule
99
public class Derived : Base {
1010
}
1111

12-
12+
public class DerivedNested : Namespace.Nested {}
13+
public class DerivedExtNested : Namespace.ExtNested {}

0 commit comments

Comments
 (0)