Skip to content

Allow function builder attrs on vars without bodies in interfaces #29165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2483,8 +2483,24 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
decl = func;
} else if (auto storage = dyn_cast<AbstractStorageDecl>(D)) {
decl = storage;
auto getter = storage->getParsedAccessor(AccessorKind::Get);
if (!getter || !getter->hasBody()) {

// Check whether this is a property without an explicit getter.
auto shouldDiagnose = [&]() -> bool {
auto getter = storage->getParsedAccessor(AccessorKind::Get);
if (!getter)
return true;

// Module interfaces don't print bodies for all getters, so allow getters
// that don't have a body if we're compiling a module interface.
SourceFile *parent = storage->getDeclContext()->getParentSourceFile();
bool isInInterface = parent && parent->Kind == SourceFileKind::Interface;
if (!isInInterface && !getter->hasBody())
return true;

return false;
};

if (shouldDiagnose()) {
diagnose(attr->getLocation(),
diag::function_builder_attribute_on_storage_without_getter,
nominal->getFullName(),
Expand Down
11 changes: 11 additions & 0 deletions test/ModuleInterface/function_builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// RUN: %target-swift-frontend -typecheck -module-name FunctionBuilders -emit-module-interface-path %t/FunctionBuilders.swiftinterface %s
// RUN: %FileCheck %s < %t/FunctionBuilders.swiftinterface
// RUN: %target-swift-frontend -I %t -typecheck -verify %S/Inputs/function_builders_client.swift
// RUN: %target-swift-frontend -compile-module-from-interface %t/FunctionBuilders.swiftinterface -o %t/FunctionBuilders.swiftmodule

@_functionBuilder
public struct TupleBuilder {
Expand Down Expand Up @@ -33,3 +34,13 @@ public struct TupleBuilder {
public func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
print(body(cond))
}

public struct UsesBuilderProperty {
// CHECK: @FunctionBuilders.TupleBuilder public var myVar: (Swift.String, Swift.String) {
// CHECK-NEXT: get
// CHECK-NEXT: }
@TupleBuilder public var myVar: (String, String) {
"hello"
"goodbye"
}
}