Skip to content

Commit cbc050a

Browse files
authored
Merge pull request #69414 from apple/es-tbd
Emit default arg for a package func during silgen
2 parents eea3d66 + eefe516 commit cbc050a

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 -Xfrontend -validate-tbd-against-ir=all
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 -Xfrontend -validate-tbd-against-ir=all
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-swift-frontend -typecheck %t/main.swift -I %t -L %t -lUtils -package-name mypkg -verify
39+
40+
41+
//--- Utils.swift
42+
public func pubBar(arg: Int = 1) -> Int { return arg + 11 }
43+
package func pkgBar(arg: Int = 1) -> Int { return arg + 12 }
44+
func internalBar(arg: Int = 1) -> Int { return arg + 13 }
45+
46+
public func pubFoo(arg: Int) -> Int { return arg + 1 }
47+
package func pkgFoo(arg: Int) -> Int { return arg + 2 }
48+
func internalFoo(arg: Int) -> Int { return arg + 3 }
49+
50+
//--- main.swift
51+
import Utils
52+
53+
let a = pubBar()
54+
let b = pkgBar()
55+
56+
let c = pubFoo(arg: 3)
57+
let d = pkgFoo(arg: 5)
58+
59+
print(a, b, c, d)

0 commit comments

Comments
 (0)