Skip to content

Commit c42aca1

Browse files
committed
[Compile Time Constant Extraction] Support for open existential expressions
1 parent 96900f9 commit c42aca1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
355355
}
356356
}
357357

358+
if (functionKind == ExprKind::FunctionConversion) {
359+
auto functionConversionExpr = cast<FunctionConversionExpr>(callExpr->getFn());
360+
if (functionConversionExpr->getSubExpr()->getKind() == ExprKind::DeclRef) {
361+
auto declRefExpr = cast<DeclRefExpr>(functionConversionExpr->getSubExpr());
362+
auto identifier =
363+
declRefExpr->getDecl()->getName().getBaseIdentifier().str().str();
364+
365+
std::vector<FunctionParameter> parameters =
366+
extractFunctionArguments(callExpr->getArgs(), declContext);
367+
return std::make_shared<FunctionCallValue>(identifier, parameters);
368+
}
369+
}
370+
358371
break;
359372
}
360373

@@ -507,6 +520,12 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
507520
auto derivedExpr = cast<DerivedToBaseExpr>(expr);
508521
return extractCompileTimeValue(derivedExpr->getSubExpr(), declContext);
509522
}
523+
524+
case ExprKind::OpenExistential: {
525+
auto openExistentialExpr = cast<OpenExistentialExpr>(expr);
526+
return extractCompileTimeValue(openExistentialExpr->getExistentialValue(), declContext);
527+
}
528+
510529
default: {
511530
break;
512531
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "[MyProto]" > %t/protocols.json
3+
4+
// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractOpenExistential.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
5+
// RUN: cat %t/ExtractOpenExistential.swiftconstvalues 2>&1 | %FileCheck %s
6+
7+
protocol MyProto {}
8+
9+
protocol ExampleProtocol {
10+
var protocolProperty: String { get }
11+
}
12+
13+
struct ConcreteType: ExampleProtocol {
14+
let protocolProperty: String = "Concrete implementation"
15+
}
16+
17+
func useExistential(_ example: any ExampleProtocol) -> String {
18+
return example.protocolProperty
19+
}
20+
21+
public struct External: MyProto {
22+
static let existentialValue = useExistential(ConcreteType())
23+
}
24+
25+
26+
// CHECK: [
27+
// CHECK-NEXT: {
28+
// CHECK-NEXT: "typeName": "ExtractOpenExistential.External",
29+
// CHECK-NEXT: "mangledTypeName": "22ExtractOpenExistential8ExternalV",
30+
// CHECK-NEXT: "kind": "struct",
31+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractOpenExistential.swift",
32+
// CHECK-NEXT: "line": 21,
33+
// CHECK-NEXT: "conformances": [
34+
// CHECK-NEXT: "ExtractOpenExistential.MyProto"
35+
// CHECK-NEXT: ],
36+
// CHECK-NEXT: "allConformances": [
37+
// CHECK-NEXT: {
38+
// CHECK-NEXT: "protocolName": "ExtractOpenExistential.MyProto"
39+
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpenExistential"
40+
// CHECK-NEXT: }
41+
// CHECK-NEXT: ],
42+
// CHECK-NEXT: "associatedTypeAliases": [],
43+
// CHECK-NEXT: "properties": [
44+
// CHECK-NEXT: {
45+
// CHECK-NEXT: "label": "existentialValue",
46+
// CHECK-NEXT: "type": "Swift.String",
47+
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
48+
// CHECK-NEXT: "isStatic": "true",
49+
// CHECK-NEXT: "isComputed": "false",
50+
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractOpenExistential.swift",
51+
// CHECK-NEXT: "line": 22,
52+
// CHECK-NEXT: "valueKind": "FunctionCall",
53+
// CHECK-NEXT: "value": {
54+
// CHECK-NEXT: "name": "useExistential",
55+
// CHECK-NEXT: "arguments": [
56+
// CHECK-NEXT: {
57+
// CHECK-NEXT: "label": "",
58+
// CHECK-NEXT: "type": "any ExtractOpenExistential.ExampleProtocol",
59+
// CHECK-NEXT: "valueKind": "InitCall",
60+
// CHECK-NEXT: "value": {
61+
// CHECK-NEXT: "type": "ExtractOpenExistential.ConcreteType",
62+
// CHECK-NEXT: "arguments": []
63+
// CHECK-NEXT: }
64+
// CHECK-NEXT: }
65+
// CHECK-NEXT: ]
66+
// CHECK-NEXT: }
67+
// CHECK-NEXT: }
68+
// CHECK-NEXT: ]
69+
// CHECK-NEXT: }
70+
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)