Skip to content

Commit b4ed338

Browse files
authored
Merge pull request #62177 from jPaolantonio/static-extract-computed-accessor
[Compile Time Constant Extraction] Add extraction of single statement returns for computed properties
2 parents 2f8998a + 9f69176 commit b4ed338

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ extractPropertyInitializationValue(VarDecl *propertyDecl) {
133133
}
134134
}
135135

136+
if (auto accessorDecl = propertyDecl->getAccessor(AccessorKind::Get)) {
137+
auto node = accessorDecl->getTypecheckedBody()->getFirstElement();
138+
if (node.is<Stmt *>()) {
139+
if (auto returnStmt = dyn_cast<ReturnStmt>(node.get<Stmt *>())) {
140+
auto expr = returnStmt->getResult();
141+
std::string LiteralOutput;
142+
llvm::raw_string_ostream OutputStream(LiteralOutput);
143+
expr->printConstExprValue(&OutputStream, nullptr);
144+
if (!LiteralOutput.empty())
145+
return std::make_shared<RawLiteralValue>(LiteralOutput);
146+
}
147+
}
148+
}
149+
136150
return std::make_shared<RuntimeValue>();
137151
}
138152

test/ConstExtraction/fields.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@
7171
// CHECK-NEXT: "type": "Swift.Int",
7272
// CHECK-NEXT: "isStatic": "false",
7373
// CHECK-NEXT: "isComputed": "true",
74-
// CHECK-NEXT: "value": "Unknown"
74+
// CHECK-NEXT: "value": "3"
7575
// CHECK-NEXT: },
7676
// CHECK-NEXT: {
7777
// CHECK-NEXT: "label": "p4",
7878
// CHECK-NEXT: "type": "Swift.Int",
7979
// CHECK-NEXT: "isStatic": "true",
8080
// CHECK-NEXT: "isComputed": "true",
81-
// CHECK-NEXT: "value": "Unknown"
81+
// CHECK-NEXT: "value": "3"
8282
// CHECK-NEXT: }
8383
// CHECK-NEXT: ]
8484
// CHECK-NEXT: }
@@ -91,7 +91,7 @@ public struct Foo {
9191
let p1: String = "Hello, World"
9292
static let p2: Float = 42.2
9393
var p3: Int {3}
94-
static var p4: Int {3}
94+
static var p4: Int { return 3 }
9595
let p5: [Int] = [1,2,3,4,5,6,7,8,9]
9696
let p6: Bool = false
9797
let p7: Bool? = nil

0 commit comments

Comments
 (0)