Skip to content

Commit cb2fb91

Browse files
committed
If a package func has a default arg, its symbol is not generated
in TBD, causing a linker error. This is because the default arg is not generated during silgen. This PR adds a check to make sure it's represented by SILDeclRef and its linkage emitted. Resolves rdar://116184295
1 parent 1470023 commit cb2fb91

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,14 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
420420

421421
void visitDefaultArguments(ValueDecl *VD, ParameterList *PL) {
422422
auto moduleDecl = VD->getModuleContext();
423-
auto publicDefaultArgGenerators = moduleDecl->isTestingEnabled() ||
424-
moduleDecl->arePrivateImportsEnabled();
425-
if (Ctx.getOpts().PublicSymbolsOnly && !publicDefaultArgGenerators)
423+
// Check if symbols should be more visible than their declared access level.
424+
// In case of `package` access level, the symbol should be visible by an
425+
// external module in the same package, thus the default argument should be
426+
// generated and its linkage emitted.
427+
auto shouldGenerateDefaultArgs = moduleDecl->isTestingEnabled() ||
428+
moduleDecl->arePrivateImportsEnabled() ||
429+
VD->getFormalAccess() == AccessLevel::Package;
430+
if (Ctx.getOpts().PublicSymbolsOnly && !shouldGenerateDefaultArgs)
426431
return;
427432

428433
// In Swift 3 (or under -enable-testing), default arguments (of public
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-build-swift-dylib(%t/%target-library-name(Utils)) %t/Utils.swift \
5+
// RUN: -module-name Utils -package-name mypkg \
6+
// RUN: -emit-module -emit-module-path %t/Utils.swiftmodule \
7+
// RUN: -emit-tbd -emit-tbd-path %t/libUtils.tbd -Xfrontend -tbd-install_name=%t/libUtils.dylib
8+
9+
// RUN: %FileCheck %s --check-prefix CHECK-TBD < %t/libUtils.tbd
10+
// CHECK-TBD-NOT: _$s5Utils6pubBar3argS2i_tFfA_
11+
// CHECK-TBD-NOT: _$s5Utils11internalBar3argS2i_tF
12+
// CHECK-TBD-NOT: _$s5Utils11internalBar3argS2i_tFfA_
13+
// CHECK-TBD-NOT: _$s5Utils11internalFoo3argS2i_tF
14+
// CHECK-TBD: _$s5Utils6pkgBar3argS2i_tF
15+
// CHECK-TBD: _$s5Utils6pkgBar3argS2i_tFfA_
16+
// CHECK-TBD: _$s5Utils6pkgFoo3argS2i_tF
17+
// CHECK-TBD: _$s5Utils6pubBar3argS2i_tF
18+
// CHECK-TBD: _$s5Utils6pubFoo3argS2i_tF
19+
20+
// RUN: %target-build-swift-dylib(%t/%target-library-name(UtilsForTesting)) %t/Utils.swift \
21+
// RUN: -module-name UtilsForTesting -package-name testpkg \
22+
// RUN: -emit-module -emit-module-path %t/UtilsForTesting.swiftmodule \
23+
// RUN: -emit-tbd -emit-tbd-path %t/libUtilsForTesting.tbd -Xfrontend -tbd-install_name=%t/libUtilsForTesting.dylib \
24+
// RUN: -enable-testing
25+
26+
27+
// RUN: %FileCheck %s --check-prefix CHECK-TEST < %t/libUtilsForTesting.tbd
28+
// CHECK-TEST-NOT: _$s15UtilsForTesting6pubBar3argS2i_tFfA_
29+
// CHECK-TEST: _$s15UtilsForTesting11internalBar3argS2i_tF
30+
// CHECK-TEST: _$s15UtilsForTesting11internalBar3argS2i_tFfA_
31+
// CHECK-TEST: _$s15UtilsForTesting11internalFoo3argS2i_tF
32+
// CHECK-TEST: _$s15UtilsForTesting6pkgBar3argS2i_tF
33+
// CHECK-TEST: _$s15UtilsForTesting6pkgBar3argS2i_tFfA_
34+
// CHECK-TEST: _$s15UtilsForTesting6pkgFoo3argS2i_tF
35+
// CHECK-TEST: _$s15UtilsForTesting6pubBar3argS2i_tF
36+
// CHECK-TEST: _$s15UtilsForTesting6pubFoo3argS2i_tF
37+
38+
// RUN: %target-build-swift -I %t -L %t -lUtils %t/main.swift -o %t/main %target-rpath(%t) -package-name mypkg
39+
40+
// RUN: %target-codesign %t/main
41+
// RUN: %target-run %t/main %t/%target-library-name(Utils) > %t/run-result.output
42+
// RUN: %FileCheck %s < %t/run-result.output
43+
44+
45+
//--- Utils.swift
46+
public func pubBar(arg: Int = 1) -> Int { return arg + 11 }
47+
package func pkgBar(arg: Int = 1) -> Int { return arg + 12 }
48+
func internalBar(arg: Int = 1) -> Int { return arg + 13 }
49+
50+
public func pubFoo(arg: Int) -> Int { return arg + 1 }
51+
package func pkgFoo(arg: Int) -> Int { return arg + 2 }
52+
func internalFoo(arg: Int) -> Int { return arg + 3 }
53+
54+
//--- main.swift
55+
import Utils
56+
57+
let a = pubBar()
58+
let b = pkgBar()
59+
60+
let c = pubFoo(arg: 3)
61+
let d = pkgFoo(arg: 5)
62+
63+
print(a, b, c, d) // CHECK: 12 13 4 7

0 commit comments

Comments
 (0)