Skip to content

Commit be33968

Browse files
committed
[AST] Print init accessors when they are ABI-public
Using init accessors from inlinable code requires `@usableFromInline` annotation, which means that we need to print them in swift interfaces and serialize them.
1 parent 123068c commit be33968

File tree

3 files changed

+202
-5
lines changed

3 files changed

+202
-5
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,6 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
353353
}
354354
}
355355

356-
if (auto *accessor = dyn_cast<AccessorDecl>(D)) {
357-
if (accessor->isInitAccessor() && !options.PrintForSIL)
358-
return false;
359-
}
360-
361356
return ShouldPrintChecker::shouldPrint(D, options);
362357
}
363358
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: split-file %s %t/src
3+
4+
// RUN: %target-build-swift %t/src/Library.swift -swift-version 5 -emit-module -emit-library \
5+
// RUN: -enable-library-evolution \
6+
// RUN: -module-name Library \
7+
// RUN: -o %t/%target-library-name(Library) \
8+
// RUN: -emit-module-interface-path %t/Library.swiftinterface
9+
10+
// RUN: %target-codesign %t/%target-library-name(Library)
11+
12+
// RUN: %target-build-swift -I %t -L %t -l Library %t/src/main.swift %target-rpath(%t) -o %t/main.out
13+
// RUN: %target-codesign %t/main.out
14+
// RUN: %target-run %t/main.out %t/%target-library-name(Library) 2>&1 | %FileCheck %t/src/main.swift
15+
16+
// RUN: rm %t/Library.swiftmodule
17+
18+
// RUN: %target-build-swift -I %t -L %t -l Library %t/src/main.swift %target-rpath(%t) -o %t/main.out
19+
// RUN: %target-codesign %t/main.out
20+
// RUN: %target-run %t/main.out %t/%target-library-name(Library) 2>&1 | %FileCheck %t/src/main.swift
21+
22+
// REQUIRES: executable_test
23+
24+
//--- Library.swift
25+
@frozen
26+
public struct Inlinable {
27+
var _x: Int
28+
29+
public var x: Int {
30+
@usableFromInline
31+
@storageRestrictions(initializes: _x)
32+
init {
33+
self._x = newValue
34+
}
35+
36+
get {
37+
_x
38+
}
39+
}
40+
41+
@inlinable
42+
public init(x: Int) {
43+
self.x = x
44+
}
45+
}
46+
47+
@frozen
48+
public struct Transparent {
49+
@usableFromInline
50+
var _x: Int
51+
52+
public var x: Int {
53+
@_alwaysEmitIntoClient
54+
@storageRestrictions(initializes: _x)
55+
init {
56+
self._x = newValue
57+
}
58+
59+
get {
60+
_x
61+
}
62+
}
63+
64+
@_alwaysEmitIntoClient
65+
public init(x: Int) {
66+
self.x = x
67+
}
68+
}
69+
70+
//--- main.swift
71+
import Library
72+
73+
let inlinable = Inlinable(x: 42)
74+
print("Inlinable.x = \(inlinable.x)")
75+
// CHECK: Inlinable.x = 42
76+
77+
let transparent = Transparent(x: -1)
78+
print("Transparent.x = \(transparent.x)")
79+
// CHECK: Transparent.x = -1
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// RUN: %empty-directory(%t/src)
2+
// RUN: split-file %s %t/src
3+
4+
/// Build the library A
5+
// RUN: %target-swift-frontend -emit-module %t/src/A.swift \
6+
// RUN: -module-name A -swift-version 5 -enable-library-evolution \
7+
// RUN: -emit-module-path %t/A.swiftmodule \
8+
// RUN: -emit-module-interface-path %t/A.swiftinterface
9+
10+
// RUN: %FileCheck %t/src/A.swift < %t/A.swiftinterface
11+
12+
// Build the client using module
13+
// RUN: %target-swift-emit-sil -verify -module-name Client -I %t %t/src/Client.swift | %FileCheck %t/src/Client.swift
14+
15+
// RUN: rm %t/A.swiftmodule
16+
17+
// Re-build the client using interface
18+
// RUN: %target-swift-emit-sil -verify -module-name Client -I %t %t/src/Client.swift | %FileCheck %t/src/Client.swift
19+
20+
// REQUIRES: asserts
21+
22+
//--- A.swift
23+
@frozen
24+
public struct Inlinable {
25+
var _x: Int
26+
27+
// CHECK: public var x: Swift.Int {
28+
// CHECK-NEXT: @usableFromInline
29+
// CHECK-NEXT: @storageRestrictions(initializes: _x) init
30+
// CHECK-NEXT: get
31+
// CHECK-NEXT }
32+
33+
public var x: Int {
34+
@usableFromInline
35+
@storageRestrictions(initializes: _x)
36+
init {
37+
self._x = newValue
38+
}
39+
40+
get {
41+
_x
42+
}
43+
}
44+
45+
@inlinable
46+
public init(x: Int) {
47+
self.x = x
48+
}
49+
}
50+
51+
public struct Internal {
52+
// CHECK: public var y: Swift.Int {
53+
// CHECK-NEXT: get
54+
// CHECK-NEXT: }
55+
56+
public var y: Int {
57+
init {
58+
}
59+
60+
get { 0 }
61+
}
62+
63+
init(y: Int) {
64+
self.y = y
65+
}
66+
}
67+
68+
@frozen
69+
public struct Transparent {
70+
@usableFromInline
71+
var _x: Int
72+
73+
// CHECK: public var x: Swift.Int {
74+
// CHECK-NEXT: @_alwaysEmitIntoClient @storageRestrictions(initializes: _x) init {
75+
// CHECK-NEXT: self._x = newValue
76+
// CHECK-NEXT: }
77+
// CHECK-NEXT: get
78+
// CHECK-NEXT }
79+
80+
public var x: Int {
81+
@_alwaysEmitIntoClient
82+
@storageRestrictions(initializes: _x)
83+
init {
84+
self._x = newValue
85+
}
86+
87+
get {
88+
_x
89+
}
90+
}
91+
92+
@_alwaysEmitIntoClient
93+
public init(x: Int) {
94+
self.x = x
95+
}
96+
}
97+
98+
//--- Client.swift
99+
import A
100+
101+
// CHECK-LABEL: sil hidden @$s6Client15testTransparentyyF : $@convention(thin) () -> ()
102+
// CHECK: [[X:%.*]] = struct $Int (%1 : $Builtin.Int64)
103+
// CHECK-NEXT: // function_ref Transparent.init(x:)
104+
// CHECK-NEXT: [[TRANSPARENT_REF:%.*]] = function_ref @$s1A11TransparentV1xACSi_tcfC : $@convention(method) (Int, @thin Transparent.Type) -> Transparent
105+
// CHECK-NEXT: apply [[TRANSPARENT_REF]]([[X]], %0) : $@convention(method) (Int, @thin Transparent.Type) -> Transparent
106+
func testTransparent() {
107+
_ = Transparent(x: 42)
108+
}
109+
110+
// CHECK-LABEL: sil shared @$s1A11TransparentV1xACSi_tcfC : $@convention(method) (Int, @thin Transparent.Type) -> Transparent
111+
112+
// CHECK-LABEL: sil hidden @$s6Client13testInlinableyyF : $@convention(thin) () -> ()
113+
// CHECK: [[X:%.*]] = struct $Int (%1 : $Builtin.Int64)
114+
// CHECK-NEXT: // function_ref Inlinable.init(x:)
115+
// CHECK-NEXT: [[INLINABLE_REF:%.*]] = function_ref @$s1A9InlinableV1xACSi_tcfC : $@convention(method) (Int, @thin Inlinable.Type) -> Inlinable
116+
// CHECK-NEXT: apply [[INLINABLE_REF]]([[X]], %0) : $@convention(method) (Int, @thin Inlinable.Type) -> Inlinable
117+
func testInlinable() {
118+
_ = Inlinable(x: 42)
119+
}
120+
121+
// CHECK-LABEL: sil @$s1A9InlinableV1xACSi_tcfC : $@convention(method) (Int, @thin Inlinable.Type) -> Inlinable
122+
123+
// CHECK-LABEL: sil shared @$s1A11TransparentV1xSivi : $@convention(thin) (Int, @thin Transparent.Type) -> @out Int

0 commit comments

Comments
 (0)