Skip to content

Support opaque result types when applying a function builder to a func #25926

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
8 changes: 7 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2873,7 +2873,13 @@ class OpaqueTypeDecl : public GenericTypeDecl {
assert(!NamingDecl && "already have naming decl");
NamingDecl = D;
}


/// Is this opaque type the opaque return type of the given function?
///
/// This is more complex than just checking `getNamingDecl` because the
/// function could also be the getter of a storage declaration.
bool isOpaqueReturnTypeOfFunction(const AbstractFunctionDecl *func) const;

GenericSignature *getOpaqueInterfaceGenericSignature() const {
return OpaqueInterfaceGenericSignature;
}
Expand Down
15 changes: 15 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6575,6 +6575,21 @@ OpaqueTypeDecl::OpaqueTypeDecl(ValueDecl *NamingDecl,
setImplicit();
}

bool OpaqueTypeDecl::isOpaqueReturnTypeOfFunction(
const AbstractFunctionDecl *func) const {
// Either the function is declared with its own opaque return type...
if (getNamingDecl() == func)
return true;

// ...or the function is a getter for a property or subscript with an
// opaque return type.
if (auto accessor = dyn_cast<AccessorDecl>(func)) {
return accessor->isGetter() && getNamingDecl() == accessor->getStorage();
}

return false;
}

Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
assert(getNamingDecl() && "not an opaque return type");
if (!OpaqueReturnTypeIdentifier.empty())
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,16 @@ bool TypeChecker::typeCheckFunctionBuilderFuncBody(FuncDecl *FD,
if (!returnType || returnType->hasError())
return true;

TypeCheckExprOptions options = {};
if (auto opaque = returnType->getAs<OpaqueTypeArchetypeType>()) {
if (opaque->getDecl()->isOpaqueReturnTypeOfFunction(FD))
options |= TypeCheckExprFlags::ConvertTypeIsOpaqueReturnType;
}

// Type-check the single result expression.
Type returnExprType = typeCheckExpression(returnExpr, FD,
TypeLoc::withoutLoc(returnType),
CTP_ReturnStmt);
CTP_ReturnStmt, options);
if (!returnExprType)
return true;
assert(returnExprType->isEqual(returnType));
Expand Down
12 changes: 2 additions & 10 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,16 +484,8 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {
auto funcDecl = TheFunc->getAbstractFunctionDecl();
if (!funcDecl)
return false;
// Either the function is declared with its own opaque return type...
if (opaque->getNamingDecl() == funcDecl)
return true;
// ...or the function is a getter for a property or subscript with an
// opaque return type.
if (auto accessor = dyn_cast<AccessorDecl>(funcDecl)) {
return accessor->isGetter()
&& opaque->getNamingDecl() == accessor->getStorage();
}
return false;

return opaque->isOpaqueReturnTypeOfFunction(funcDecl);
};

if (auto opaque = ResultTy->getAs<OpaqueTypeArchetypeType>()) {
Expand Down
21 changes: 21 additions & 0 deletions test/Constraints/function_builder_opaque_result.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-swift-frontend -disable-availability-checking -typecheck -verify %s

protocol Taggable {}
extension String: Taggable {}

@_functionBuilder
struct TaggableBuilder {
static func buildBlock(_ params: Taggable...) -> String {
return "Your tags weren't worth keeping anyway"
}
}

@TaggableBuilder
func testFuncWithOpaqueResult() -> some Taggable {
"This is an amazing tag"
}

@TaggableBuilder
var testGetterWithOpaqueResult: some Taggable {
"This is also an amazing tag"
}