Skip to content

Commit 064576b

Browse files
Merge pull request swiftlang#76731 from venkatesh5789/resultbuilder-availability-notation
[Compile Time Constant Extraction] Extract listed Availability Attributes for buildLimitedAvailability in Result Builders
2 parents e646188 + f04486d commit 064576b

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

include/swift/AST/ConstTypeInfo.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/AST/Attr.h"
1717
#include "swift/AST/Type.h"
18+
#include "swift/AST/TypeCheckRequests.h"
1819
#include <memory>
1920
#include <string>
2021
#include <vector>
@@ -197,6 +198,15 @@ class BuilderValue : public CompileTimeValue {
197198
///
198199
class ConditionalMember : public BuilderMember {
199200
public:
201+
ConditionalMember(MemberKind MemberKind,
202+
std::vector<PlatformVersionConstraintAvailabilitySpec>
203+
AvailabilityAttributes,
204+
std::vector<std::shared_ptr<BuilderMember>> IfElements,
205+
std::vector<std::shared_ptr<BuilderMember>> ElseElements)
206+
: BuilderMember(MemberKind),
207+
AvailabilityAttributes(AvailabilityAttributes),
208+
IfElements(IfElements), ElseElements(ElseElements) {}
209+
200210
ConditionalMember(MemberKind MemberKind,
201211
std::vector<std::shared_ptr<BuilderMember>> IfElements,
202212
std::vector<std::shared_ptr<BuilderMember>> ElseElements)
@@ -210,6 +220,10 @@ class BuilderValue : public CompileTimeValue {
210220
(Kind == MemberKind::Optional);
211221
}
212222

223+
std::optional<std::vector<PlatformVersionConstraintAvailabilitySpec>>
224+
getAvailabilityAttributes() const {
225+
return AvailabilityAttributes;
226+
}
213227
std::vector<std::shared_ptr<BuilderMember>> getIfElements() const {
214228
return IfElements;
215229
}
@@ -218,6 +232,8 @@ class BuilderValue : public CompileTimeValue {
218232
}
219233

220234
private:
235+
std::optional<std::vector<PlatformVersionConstraintAvailabilitySpec>>
236+
AvailabilityAttributes;
221237
std::vector<std::shared_ptr<BuilderMember>> IfElements;
222238
std::vector<std::shared_ptr<BuilderMember>> ElseElements;
223239
};

lib/ConstExtract/ConstExtract.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ getResultBuilderElementFromASTNode(const ASTNode node) {
945945

946946
BuilderValue::ConditionalMember
947947
getConditionalMemberFromIfStmt(const IfStmt *ifStmt) {
948+
std::vector<PlatformVersionConstraintAvailabilitySpec> AvailabilityAttributes;
948949
std::vector<std::shared_ptr<BuilderValue::BuilderMember>> IfElements;
949950
std::vector<std::shared_ptr<BuilderValue::BuilderMember>> ElseElements;
950951
if (auto thenBraceStmt = ifStmt->getThenStmt()) {
@@ -976,12 +977,24 @@ getConditionalMemberFromIfStmt(const IfStmt *ifStmt) {
976977
}
977978
for (auto elt : ifStmt->getCond()) {
978979
if (elt.getKind() == StmtConditionElement::CK_Availability) {
980+
for (auto *Q : elt.getAvailability()->getQueries()) {
981+
if (auto *availability =
982+
dyn_cast<PlatformVersionConstraintAvailabilitySpec>(Q)) {
983+
AvailabilityAttributes.push_back(*availability);
984+
}
985+
}
979986
memberKind = BuilderValue::LimitedAvailability;
980987
break;
981988
}
982989
}
983990

984-
return BuilderValue::ConditionalMember(memberKind, IfElements, ElseElements);
991+
if (AvailabilityAttributes.empty()) {
992+
return BuilderValue::ConditionalMember(memberKind, IfElements,
993+
ElseElements);
994+
}
995+
996+
return BuilderValue::ConditionalMember(memberKind, AvailabilityAttributes,
997+
IfElements, ElseElements);
985998
}
986999

9871000
BuilderValue::ArrayMember
@@ -1094,6 +1107,17 @@ void writeBuilderMember(
10941107

10951108
default: {
10961109
auto member = cast<BuilderValue::ConditionalMember>(Member);
1110+
if (auto availabilityAttributes = member->getAvailabilityAttributes()) {
1111+
JSON.attributeArray("availabilityAttributes", [&] {
1112+
for (auto elem : *availabilityAttributes) {
1113+
JSON.object([&] {
1114+
JSON.attribute("platform",
1115+
platformString(elem.getPlatform()).str());
1116+
JSON.attribute("minVersion", elem.getVersion().getAsString());
1117+
});
1118+
}
1119+
});
1120+
}
10971121
JSON.attributeArray("ifElements", [&] {
10981122
for (auto elem : member->getIfElements()) {
10991123
JSON.object([&] { writeBuilderMember(JSON, elem); });

test/ConstExtraction/ExtractResultBuilders.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public struct MyFooProviderInferred: FooProvider {
108108
Foo(name: "MyFooProviderInferred.foos.Optional")
109109
}
110110

111-
if #available(macOS 99, *) {
111+
if #available(iOS 18.0, macOS 15.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
112112
Foo(name: "MyFooProviderInferred.foos.limitedAvailability.1")
113113
Foo(name: "MyFooProviderInferred.foos.limitedAvailability.2")
114114
} else {
@@ -430,6 +430,28 @@ public struct MyFooProviderInferred: FooProvider {
430430
// CHECK-NEXT: },
431431
// CHECK-NEXT: {
432432
// CHECK-NEXT: "kind": "buildLimitedAvailability",
433+
// CHECK-NEXT: "availabilityAttributes": [
434+
// CHECK-NEXT: {
435+
// CHECK-NEXT: "platform": "iOS",
436+
// CHECK-NEXT: "minVersion": "18.0"
437+
// CHECK-NEXT: },
438+
// CHECK-NEXT: {
439+
// CHECK-NEXT: "platform": "macOS",
440+
// CHECK-NEXT: "minVersion": "15.0"
441+
// CHECK-NEXT: },
442+
// CHECK-NEXT: {
443+
// CHECK-NEXT: "platform": "watchOS",
444+
// CHECK-NEXT: "minVersion": "11.0"
445+
// CHECK-NEXT: },
446+
// CHECK-NEXT: {
447+
// CHECK-NEXT: "platform": "tvOS",
448+
// CHECK-NEXT: "minVersion": "18.0"
449+
// CHECK-NEXT: },
450+
// CHECK-NEXT: {
451+
// CHECK-NEXT: "platform": "visionOS",
452+
// CHECK-NEXT: "minVersion": "2.0"
453+
// CHECK-NEXT: }
454+
// CHECK-NEXT: ],
433455
// CHECK-NEXT: "ifElements": [
434456
// CHECK-NEXT: {
435457
// CHECK-NEXT: "element": {

0 commit comments

Comments
 (0)