From ee9b4766ad00f60dcf8611c7966ebd2d8d6afdeb Mon Sep 17 00:00:00 2001 From: Artem Chikin Date: Wed, 1 Mar 2023 09:22:45 -0800 Subject: [PATCH] [Compile Time Constant Extraction] Extract InjectIntoOptional arguments Resolves rdar://106059663 --- lib/ConstExtract/ConstExtract.cpp | 2 ++ .../ExtractInjectOptional.swift | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/ConstExtraction/ExtractInjectOptional.swift diff --git a/lib/ConstExtract/ConstExtract.cpp b/lib/ConstExtract/ConstExtract.cpp index c597f3e15e1..8c0f43edef2 100644 --- a/lib/ConstExtract/ConstExtract.cpp +++ b/lib/ConstExtract/ConstExtract.cpp @@ -138,6 +138,8 @@ extractFunctionArguments(const ArgumentList *args) { if (decl->hasDefaultExpr()) { argExpr = decl->getTypeCheckedDefaultExpr(); } + } else if (auto optionalInject = dyn_cast(argExpr)) { + argExpr = optionalInject->getSubExpr(); } parameters.push_back({label, type, extractCompileTimeValue(argExpr)}); } diff --git a/test/ConstExtraction/ExtractInjectOptional.swift b/test/ConstExtraction/ExtractInjectOptional.swift new file mode 100644 index 00000000000..a5fe88a74f8 --- /dev/null +++ b/test/ConstExtraction/ExtractInjectOptional.swift @@ -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", +// 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: ]