Skip to content

Commit cf81f05

Browse files
authored
Merge pull request swiftlang#76681 from kubamracek/embedded-default-arg-metatypes
[embedded] Support default arguments with metatypes-as-type-hints in Embedded Swift
2 parents 15ca322 + 4c33af9 commit cf81f05

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,20 @@ bool SILDeclRef::isTransparent() const {
820820
}
821821
}
822822

823+
// To support using metatypes as type hints in Embedded Swift. A default
824+
// argument generator might be returning a metatype, which we normally don't
825+
// support in Embedded Swift, but to still allow metatypes as type hints, we
826+
// make the generator always inline to the callee by marking it transparent.
827+
if (getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
828+
if (isDefaultArgGenerator() && hasDecl()) {
829+
auto *decl = getDecl();
830+
auto *param = getParameterAt(decl, defaultArgIndex);
831+
Type paramType = param->getTypeOfDefaultExpr();
832+
if (paramType && paramType->is<MetatypeType>())
833+
return true;
834+
}
835+
}
836+
823837
if (hasDecl()) {
824838
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))
825839
return AFD->isTransparent();
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)