Skip to content

Commit f515cd7

Browse files
dendizDeniz Dizman
andauthored
[Compile Time Constant Extraction] Extraction support for force unwrapped optionals (#85047)
Adds support for extracting force unwrapped optionals. ``` let prop: MyType = MyType()! ``` In this case the value kind was being extracted as a Runtime value. resolves rdar://146046727 Co-authored-by: Deniz Dizman <ddizman@apple.com>
1 parent 66eef68 commit f515cd7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
519519
return extractCompileTimeValue(openExistentialExpr->getExistentialValue(), declContext);
520520
}
521521

522+
case ExprKind::ForceValue: {
523+
auto forceValueExpr = cast<ForceValueExpr>(expr);
524+
return extractCompileTimeValue(forceValueExpr->getSubExpr(), declContext);
525+
}
526+
522527
default: {
523528
break;
524529
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "[MyProto]" > %t/protocols.json
3+
4+
// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractForceValue.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
5+
// RUN: cat %t/ExtractForceValue.swiftconstvalues 2>&1 | %FileCheck %s
6+
7+
protocol MyProto {}
8+
9+
class CustomKey {
10+
init?() {
11+
12+
}
13+
}
14+
struct MyStruct: MyProto {
15+
let prop1: CustomKey = CustomKey()!
16+
let prop2: CustomKey! = CustomKey()
17+
}
18+
19+
// CHECK: "properties": [
20+
// CHECK-NEXT: {
21+
// CHECK-NEXT: "label": "prop1",
22+
// CHECK-NEXT: "type": "ExtractForceValue.CustomKey",
23+
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
24+
// CHECK-NEXT: "isStatic": "false",
25+
// CHECK-NEXT: "isComputed": "false",
26+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractForceValue.swift",
27+
// CHECK-NEXT: "line": 15,
28+
// CHECK-NEXT: "valueKind": "InitCall",
29+
// CHECK-NEXT: "value": {
30+
// CHECK-NEXT: "type": "Swift.Optional<ExtractForceValue.CustomKey>",
31+
// CHECK-NEXT: "arguments": []
32+
// CHECK-NEXT: }
33+
// CHECK-NEXT: },
34+
// CHECK-NEXT: {
35+
// CHECK-NEXT: "label": "prop2",
36+
// CHECK-NEXT: "type": "Swift.Optional<ExtractForceValue.CustomKey>",
37+
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
38+
// CHECK-NEXT: "isStatic": "false",
39+
// CHECK-NEXT: "isComputed": "false",
40+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractForceValue.swift",
41+
// CHECK-NEXT: "line": 16,
42+
// CHECK-NEXT: "valueKind": "InitCall",
43+
// CHECK-NEXT: "value": {
44+
// CHECK-NEXT: "type": "Swift.Optional<ExtractForceValue.CustomKey>",
45+
// CHECK-NEXT: "arguments": []
46+
// CHECK-NEXT: }
47+
// CHECK-NEXT: }
48+
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)