Skip to content

Commit e23e77b

Browse files
author
Harlan Haskins
authored
Allow function builder attrs on vars without bodies in interfaces (#29165)
The check for whether or not we can use a function builder on a property checked whether the attached getter had a body, which is not always true for module interfaces. Just disable that part of the check when compiling a module interface. rdar://58535753
1 parent 3b9014d commit e23e77b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,8 +2483,24 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
24832483
decl = func;
24842484
} else if (auto storage = dyn_cast<AbstractStorageDecl>(D)) {
24852485
decl = storage;
2486-
auto getter = storage->getParsedAccessor(AccessorKind::Get);
2487-
if (!getter || !getter->hasBody()) {
2486+
2487+
// Check whether this is a property without an explicit getter.
2488+
auto shouldDiagnose = [&]() -> bool {
2489+
auto getter = storage->getParsedAccessor(AccessorKind::Get);
2490+
if (!getter)
2491+
return true;
2492+
2493+
// Module interfaces don't print bodies for all getters, so allow getters
2494+
// that don't have a body if we're compiling a module interface.
2495+
SourceFile *parent = storage->getDeclContext()->getParentSourceFile();
2496+
bool isInInterface = parent && parent->Kind == SourceFileKind::Interface;
2497+
if (!isInInterface && !getter->hasBody())
2498+
return true;
2499+
2500+
return false;
2501+
};
2502+
2503+
if (shouldDiagnose()) {
24882504
diagnose(attr->getLocation(),
24892505
diag::function_builder_attribute_on_storage_without_getter,
24902506
nominal->getFullName(),

test/ModuleInterface/function_builders.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %target-swift-frontend -typecheck -module-name FunctionBuilders -emit-module-interface-path %t/FunctionBuilders.swiftinterface %s
33
// RUN: %FileCheck %s < %t/FunctionBuilders.swiftinterface
44
// RUN: %target-swift-frontend -I %t -typecheck -verify %S/Inputs/function_builders_client.swift
5+
// RUN: %target-swift-frontend -compile-module-from-interface %t/FunctionBuilders.swiftinterface -o %t/FunctionBuilders.swiftmodule
56

67
@_functionBuilder
78
public struct TupleBuilder {
@@ -33,3 +34,13 @@ public struct TupleBuilder {
3334
public func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
3435
print(body(cond))
3536
}
37+
38+
public struct UsesBuilderProperty {
39+
// CHECK: @FunctionBuilders.TupleBuilder public var myVar: (Swift.String, Swift.String) {
40+
// CHECK-NEXT: get
41+
// CHECK-NEXT: }
42+
@TupleBuilder public var myVar: (String, String) {
43+
"hello"
44+
"goodbye"
45+
}
46+
}

0 commit comments

Comments
 (0)