Skip to content

Commit ae5bca9

Browse files
committed
Sema: Check result builder availability
1 parent fd38c4d commit ae5bca9

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,7 @@ NOTE(construct_raw_representable_from_unwrapped_value,none,
27342734

27352735
ERROR(decl_from_hidden_module,none,
27362736
"cannot use %0 %1 %select{here|as property wrapper here|"
2737+
"as result builder here|"
27372738
"in an extension with public or '@usableFromInline' members|"
27382739
"in an extension with conditional conformances}2; "
27392740
"%select{%3 has been imported as implementation-only|"
@@ -2742,12 +2743,14 @@ ERROR(decl_from_hidden_module,none,
27422743
(DescriptiveDeclKind, DeclName, unsigned, Identifier, unsigned))
27432744
WARNING(decl_from_hidden_module_warn,none,
27442745
"cannot use %0 %1 %select{in SPI|as property wrapper in SPI|"
2746+
"as result builder in SPI|"
27452747
"in an extension with public or '@usableFromInline' members|"
27462748
"in an extension with conditional conformances}2; "
27472749
"%select{%3 has been imported as implementation-only}4",
27482750
(DescriptiveDeclKind, DeclName, unsigned, Identifier, unsigned))
27492751
ERROR(conformance_from_implementation_only_module,none,
27502752
"cannot use conformance of %0 to %1 %select{here|as property wrapper here|"
2753+
"as result builder here|"
27512754
"in an extension with public or '@usableFromInline' members|"
27522755
"in an extension with conditional conformances}2; "
27532756
"%select{%3 has been imported as implementation-only|"

lib/Sema/TypeCheckAccess.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,10 +1591,18 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
15911591
TP->getTypeRepr(), anyVar ? (Decl *)anyVar : (Decl *)PBD);
15921592

15931593
// Check the property wrapper types.
1594-
if (anyVar)
1595-
for (auto attr : anyVar->getAttachedPropertyWrappers())
1594+
if (anyVar) {
1595+
for (auto attr : anyVar->getAttachedPropertyWrappers()) {
15961596
checkType(attr->getType(), attr->getTypeRepr(), anyVar,
15971597
ExportabilityReason::PropertyWrapper);
1598+
}
1599+
1600+
if (auto attr = anyVar->getAttachedResultBuilder()) {
1601+
checkType(anyVar->getResultBuilderType(),
1602+
attr->getTypeRepr(), anyVar,
1603+
ExportabilityReason::ResultBuilder);
1604+
}
1605+
}
15981606
}
15991607

16001608
void visitPatternBindingDecl(PatternBindingDecl *PBD) {
@@ -1683,6 +1691,12 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
16831691
void visitFuncDecl(FuncDecl *FD) {
16841692
visitAbstractFunctionDecl(FD);
16851693
checkType(FD->getResultInterfaceType(), FD->getResultTypeRepr(), FD);
1694+
1695+
if (auto attr = FD->getAttachedResultBuilder()) {
1696+
checkType(FD->getResultBuilderType(),
1697+
attr->getTypeRepr(), FD,
1698+
ExportabilityReason::ResultBuilder);
1699+
}
16861700
}
16871701

16881702
void visitEnumElementDecl(EnumElementDecl *EED) {

lib/Sema/TypeCheckAvailability.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ using DeclAvailabilityFlags = OptionSet<DeclAvailabilityFlag>;
6262
enum class ExportabilityReason : unsigned {
6363
General,
6464
PropertyWrapper,
65+
ResultBuilder,
6566
ExtensionWithPublicMembers,
6667
ExtensionWithConditionalConformances
6768
};
@@ -101,7 +102,7 @@ class ExportContext {
101102
unsigned Implicit : 1;
102103
unsigned Unavailable : 1;
103104
unsigned Platform : 8;
104-
unsigned Reason : 2;
105+
unsigned Reason : 3;
105106

106107
ExportContext(DeclContext *DC,
107108
AvailabilityContext runningOSVersion,

test/Constraints/result_builder_availability.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
// REQUIRES: OS=macosx
44

5+
@available(*, unavailable)
6+
@resultBuilder
7+
struct UnavailableBuilder {
8+
// expected-note@-1 {{'UnavailableBuilder' has been explicitly marked unavailable here}}
9+
static func buildBlock() {}
10+
}
11+
12+
@UnavailableBuilder public func usesUnavailableBuilder() {}
13+
// expected-error@-1 {{'UnavailableBuilder' is unavailable}}
14+
515
enum Either<T,U> {
616
case first(T)
717
case second(U)

0 commit comments

Comments
 (0)