Skip to content

Commit 9101bdc

Browse files
committed
[embedded] Support default arguments with metatypes-as-type-hints in Embedded Swift
Materializing metatypes is not allowed in Embedded Swift, but it's still useful to use metatype functions arguments as type hints (unused in the function body). This change enables such function arguments to have default values, and achieves that by marking metatype-returning default arg generators as @_transparent.
1 parent 6e055d3 commit 9101bdc

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,28 @@ void SILGenModule::postEmitFunction(SILDeclRef constant,
12801280
F->verifyIncompleteOSSA();
12811281

12821282
emitDifferentiabilityWitnessesForFunction(constant, F);
1283+
1284+
// To support using metatypes as type hints in Embedded Swift. A default
1285+
// argument generator might be returning a metatype, which we normally don't
1286+
// support in Embedded Swift, but to still allow metatypes as type hints, we
1287+
// make the generator always inline to the callee by marking it transparent.
1288+
if (M.getOptions().EmbeddedSwift) {
1289+
if (constant.isDefaultArgGenerator()) {
1290+
bool isReturningMetatype = false;
1291+
if (F->getLoweredFunctionType()->getNumResults() == 1) {
1292+
if (F->getLoweredFunctionType()
1293+
->getSingleResult()
1294+
.getSILStorageInterfaceType()
1295+
.isMetatype()) {
1296+
isReturningMetatype = true;
1297+
}
1298+
}
1299+
1300+
if (isReturningMetatype) {
1301+
F->setTransparent(IsTransparent);
1302+
}
1303+
}
1304+
}
12831305
}
12841306

12851307
void SILGenModule::emitDifferentiabilityWitnessesForFunction(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo) | %FileCheck %s
2+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -O) | %FileCheck %s
3+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -wmo -Osize) | %FileCheck %s
4+
5+
// REQUIRES: swift_in_compiler
6+
// REQUIRES: executable_test
7+
// REQUIRES: optimized_stdlib
8+
// REQUIRES: OS=macosx || OS=linux-gnu
9+
10+
public struct AsyncStream2<Element> {
11+
var x: Int
12+
var y: Int
13+
}
14+
15+
extension AsyncStream2 {
16+
public static func makeStream2(of elementType: Element.Type = Element.self) -> AsyncStream2<Element> {
17+
return AsyncStream2<Element>()
18+
}
19+
20+
public init(
21+
_ elementType: Element.Type = Element.self
22+
) {
23+
fatalError()
24+
}
25+
}
26+
27+
struct MyStruct<T> {
28+
static func makeStruct(of t: T.Type = T.self) -> MyStruct<T> {
29+
var s = MyStruct.init()
30+
return s
31+
}
32+
public init(_ t: T.Type = T.self) {
33+
print("x")
34+
}
35+
}
36+
37+
@main
38+
struct Main {
39+
static func main() {
40+
_ = MyStruct<String>.makeStruct()
41+
_ = MyStruct.makeStruct(of: String.self)
42+
print("OK!")
43+
// CHECK: OK!
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
5+
// RUN: %target-swift-frontend -c -o %t/Main.o -I %t %t/Main.swift -enable-experimental-feature Embedded -parse-as-library
6+
7+
// REQUIRES: swift_in_compiler
8+
// REQUIRES: executable_test
9+
// REQUIRES: optimized_stdlib
10+
// REQUIRES: OS=macosx || OS=linux-gnu
11+
12+
// BEGIN MyModule.swift
13+
14+
public struct MyStruct<T> {
15+
var x: Int
16+
}
17+
18+
extension MyStruct {
19+
public static func makeStruct(of: T.Type) -> MyStruct<T> {
20+
return MyStruct<T>()
21+
}
22+
23+
public init(_: T.Type = T.self) {
24+
self.x = 42
25+
}
26+
}
27+
28+
// BEGIN Main.swift
29+
30+
import MyModule
31+
32+
@main
33+
struct Main {
34+
static func main() {
35+
_ = MyStruct.makeStruct(of: String.self)
36+
}
37+
}

0 commit comments

Comments
 (0)