Skip to content

[Compile Time Constant Extraction] Support for open existential expressions #83008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,8 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {

case ExprKind::Call: {
auto callExpr = cast<CallExpr>(expr);
auto functionKind = callExpr->getFn()->getKind();

if (functionKind == ExprKind::DeclRef) {
auto declRefExpr = cast<DeclRefExpr>(callExpr->getFn());
if (auto declRefExpr = dyn_cast<DeclRefExpr>(callExpr->getFn())) {
auto identifier =
declRefExpr->getDecl()->getName().getBaseIdentifier().str().str();

Expand All @@ -314,17 +312,15 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
return std::make_shared<FunctionCallValue>(identifier, parameters);
}

if (functionKind == ExprKind::ConstructorRefCall) {
if (auto constructorRefCall = dyn_cast<ConstructorRefCallExpr>(callExpr->getFn())) {
std::vector<FunctionParameter> parameters =
extractFunctionArguments(callExpr->getArgs(), declContext);
return std::make_shared<InitCallValue>(callExpr->getType(), parameters);
}

if (functionKind == ExprKind::DotSyntaxCall) {
auto dotSyntaxCallExpr = cast<DotSyntaxCallExpr>(callExpr->getFn());
if (auto dotSyntaxCallExpr = dyn_cast<DotSyntaxCallExpr>(callExpr->getFn())) {
auto fn = dotSyntaxCallExpr->getFn();
if (fn->getKind() == ExprKind::DeclRef) {
auto declRefExpr = cast<DeclRefExpr>(fn);
if (auto declRefExpr = dyn_cast<DeclRefExpr>(fn)) {
auto baseIdentifierName =
declRefExpr->getDecl()->getName().getBaseIdentifier().str().str();

Expand Down Expand Up @@ -355,14 +351,24 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
}
}

if (auto functionConversionExpr = dyn_cast<FunctionConversionExpr>(callExpr->getFn())) {
if (auto declRefExpr = dyn_cast<DeclRefExpr>(functionConversionExpr->getSubExpr())) {
auto identifier =
declRefExpr->getDecl()->getName().getBaseIdentifier().str().str();

std::vector<FunctionParameter> parameters =
extractFunctionArguments(callExpr->getArgs(), declContext);
return std::make_shared<FunctionCallValue>(identifier, parameters);
}
}

break;
}

case ExprKind::DotSyntaxCall: {
auto dotSyntaxCallExpr = cast<DotSyntaxCallExpr>(expr);
auto fn = dotSyntaxCallExpr->getFn();
if (fn->getKind() == ExprKind::DeclRef) {
auto declRefExpr = cast<DeclRefExpr>(fn);
if (auto declRefExpr = dyn_cast<DeclRefExpr>(fn)) {
auto caseName =
declRefExpr->getDecl()->getName().getBaseIdentifier().str().str();
return std::make_shared<EnumValue>(caseName, std::nullopt);
Expand Down Expand Up @@ -507,6 +513,12 @@ extractCompileTimeValue(Expr *expr, const DeclContext *declContext) {
auto derivedExpr = cast<DerivedToBaseExpr>(expr);
return extractCompileTimeValue(derivedExpr->getSubExpr(), declContext);
}

case ExprKind::OpenExistential: {
auto openExistentialExpr = cast<OpenExistentialExpr>(expr);
return extractCompileTimeValue(openExistentialExpr->getExistentialValue(), declContext);
}

default: {
break;
}
Expand Down
70 changes: 70 additions & 0 deletions test/ConstExtraction/ExtractOpenExistential.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// RUN: %empty-directory(%t)
// RUN: echo "[MyProto]" > %t/protocols.json

// RUN: %target-swift-frontend -typecheck -emit-const-values-path %t/ExtractOpenExistential.swiftconstvalues -const-gather-protocols-file %t/protocols.json -primary-file %s
// RUN: cat %t/ExtractOpenExistential.swiftconstvalues 2>&1 | %FileCheck %s

protocol MyProto {}

protocol ExampleProtocol {
var protocolProperty: String { get }
}

struct ConcreteType: ExampleProtocol {
let protocolProperty: String = "Concrete implementation"
}

func useExistential(_ example: any ExampleProtocol) -> String {
return example.protocolProperty
}

public struct External: MyProto {
static let existentialValue = useExistential(ConcreteType())
}


// CHECK: [
// CHECK-NEXT: {
// CHECK-NEXT: "typeName": "ExtractOpenExistential.External",
// CHECK-NEXT: "mangledTypeName": "22ExtractOpenExistential8ExternalV",
// CHECK-NEXT: "kind": "struct",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractOpenExistential.swift",
// CHECK-NEXT: "line": 21,
// CHECK-NEXT: "conformances": [
// CHECK-NEXT: "ExtractOpenExistential.MyProto"
// CHECK-NEXT: ],
// CHECK-NEXT: "allConformances": [
// CHECK-NEXT: {
// CHECK-NEXT: "protocolName": "ExtractOpenExistential.MyProto"
// CHECK-NEXT: "conformanceDefiningModule": "ExtractOpenExistential"
// CHECK-NEXT: }
// CHECK-NEXT: ],
// CHECK-NEXT: "associatedTypeAliases": [],
// CHECK-NEXT: "properties": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "existentialValue",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "mangledTypeName": "n/a - deprecated",
// CHECK-NEXT: "isStatic": "true",
// CHECK-NEXT: "isComputed": "false",
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractOpenExistential.swift",
// CHECK-NEXT: "line": 22,
// CHECK-NEXT: "valueKind": "FunctionCall",
// CHECK-NEXT: "value": {
// CHECK-NEXT: "name": "useExistential",
// CHECK-NEXT: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "",
// CHECK-NEXT: "type": "any ExtractOpenExistential.ExampleProtocol",
// CHECK-NEXT: "valueKind": "InitCall",
// CHECK-NEXT: "value": {
// CHECK-NEXT: "type": "ExtractOpenExistential.ConcreteType",
// CHECK-NEXT: "arguments": []
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }
// CHECK-NEXT: ]