Skip to content

[Compile Time Constant Extraction] Look through Optional injection and underlying-to-opaque conversions #64004

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

Merged
merged 3 commits into from
Mar 2, 2023
Merged
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
26 changes: 26 additions & 0 deletions lib/ConstExtract/ConstExtract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ extractFunctionArguments(const ArgumentList *args) {
if (decl->hasDefaultExpr()) {
argExpr = decl->getTypeCheckedDefaultExpr();
}
} else if (auto optionalInject = dyn_cast<InjectIntoOptionalExpr>(argExpr)) {
argExpr = optionalInject->getSubExpr();
}
parameters.push_back({label, type, extractCompileTimeValue(argExpr)});
}
Expand Down Expand Up @@ -304,6 +306,30 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
return std::make_shared<TypeValue>(dotSelfExpr->getType());
}

case ExprKind::UnderlyingToOpaque: {
auto underlyingToOpaque = cast<UnderlyingToOpaqueExpr>(expr);
return extractCompileTimeValue(underlyingToOpaque->getSubExpr());
}

case ExprKind::DefaultArgument: {
auto defaultArgExpr = cast<DefaultArgumentExpr>(expr);
auto *decl = defaultArgExpr->getParamDecl();
// If there is a default expr, we should have looked through to it
assert(!decl->hasDefaultExpr());
switch (decl->getDefaultArgumentKind()) {
case DefaultArgumentKind::NilLiteral:
return std::make_shared<RawLiteralValue>("nil");
case DefaultArgumentKind::EmptyArray:
return std::make_shared<ArrayValue>(
std::vector<std::shared_ptr<CompileTimeValue>>());
case DefaultArgumentKind::EmptyDictionary:
return std::make_shared<DictionaryValue>(
std::vector<std::shared_ptr<TupleValue>>());
default:
break;
}
} break;

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

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

protocol MyProto {}
struct InjectablePropertyStruct : MyProto {
let init1 = Bat(buz: "hello", fuz: 4)
}

public struct Bat {
let buz: String?
let fuz: Int

init(buz: String? = "", fuz: Int = 0) {
self.buz = buz
self.fuz = fuz
}
}

// CHECK: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "buz",
// CHECK-NEXT: "type": "Swift.Optional<Swift.String>",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "hello"
// CHECK-NEXT: },
// CHECK-NEXT: {
// CHECK-NEXT: "label": "fuz",
// CHECK-NEXT: "type": "Swift.Int",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "4"
// CHECK-NEXT: }
// CHECK-NEXT: ]
36 changes: 36 additions & 0 deletions test/ConstExtraction/ExtractUnderlyingToOpaque.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %empty-directory(%t)
// RUN: echo "[MyProto]" > %t/protocols.json

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

protocol MyProto {}
protocol Bird {}

@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
struct UnderlyingToOpaquePropertyStruct : MyProto {
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
var warbler: some Bird {
Warbler("blue")
}
}

public struct Warbler : Bird {
let belly: String = "yellow"
init(_ color: String = "red") {
self.belly = color
}
}

// CHECK: "valueKind": "InitCall",
// CHECK-NEXT: "value": {
// CHECK-NEXT: "type": "ExtractUnderlyingToOpaque.Warbler",
// CHECK-NEXT: "arguments": [
// CHECK-NEXT: {
// CHECK-NEXT: "label": "",
// CHECK-NEXT: "type": "Swift.String",
// CHECK-NEXT: "valueKind": "RawLiteral",
// CHECK-NEXT: "value": "blue"
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }