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: ]