Skip to content

Commit ee9b476

Browse files
committed
[Compile Time Constant Extraction] Extract InjectIntoOptional arguments
Resolves rdar://106059663
1 parent a64fc3b commit ee9b476

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ extractFunctionArguments(const ArgumentList *args) {
138138
if (decl->hasDefaultExpr()) {
139139
argExpr = decl->getTypeCheckedDefaultExpr();
140140
}
141+
} else if (auto optionalInject = dyn_cast<InjectIntoOptionalExpr>(argExpr)) {
142+
argExpr = optionalInject->getSubExpr();
141143
}
142144
parameters.push_back({label, type, extractCompileTimeValue(argExpr)});
143145
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "[MyProto]" > %t/protocols.json
3+
4+
// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractLiterals.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
5+
// RUN: cat %t/ExtractLiterals.swiftconstvalues 2>&1 | %FileCheck %s
6+
7+
protocol MyProto {}
8+
struct InjectablePropertyStruct : MyProto {
9+
let init1 = Bat(buz: "hello", fuz: 4)
10+
}
11+
12+
public struct Bat {
13+
let buz: String?
14+
let fuz: Int
15+
16+
init(buz: String? = "", fuz: Int = 0) {
17+
self.buz = buz
18+
self.fuz = fuz
19+
}
20+
}
21+
22+
// CHECK: "arguments": [
23+
// CHECK-NEXT: {
24+
// CHECK-NEXT: "label": "buz",
25+
// CHECK-NEXT: "type": "Swift.Optional<Swift.String>",
26+
// CHECK-NEXT: "valueKind": "RawLiteral",
27+
// CHECK-NEXT: "value": "hello"
28+
// CHECK-NEXT: },
29+
// CHECK-NEXT: {
30+
// CHECK-NEXT: "label": "fuz",
31+
// CHECK-NEXT: "type": "Swift.Int",
32+
// CHECK-NEXT: "valueKind": "RawLiteral",
33+
// CHECK-NEXT: "value": "4"
34+
// CHECK-NEXT: }
35+
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)