Skip to content

Commit 8e0ec60

Browse files
authored
Merge pull request swiftlang#32005 from DougGregor/function-builder-infer
[Function builders] Infer function builder from a protocol requirement.
2 parents b90e579 + 8cec6b5 commit 8e0ec60

File tree

18 files changed

+395
-23
lines changed

18 files changed

+395
-23
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ SWIFT_TYPEID_NAMED(PatternBindingEntry *, PatternBindingEntry)
5757
SWIFT_TYPEID_NAMED(PostfixOperatorDecl *, PostfixOperatorDecl)
5858
SWIFT_TYPEID_NAMED(PrecedenceGroupDecl *, PrecedenceGroupDecl)
5959
SWIFT_TYPEID_NAMED(PrefixOperatorDecl *, PrefixOperatorDecl)
60+
SWIFT_TYPEID_NAMED(ProtocolConformance *, ProtocolConformance)
6061
SWIFT_TYPEID_NAMED(ProtocolDecl *, ProtocolDecl)
6162
SWIFT_TYPEID_NAMED(SourceFile *, SourceFile)
6263
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct PropertyWrapperTypeInfo;
5252
enum class CtorInitializerKind;
5353
struct PropertyWrapperLValueness;
5454
struct PropertyWrapperMutability;
55+
class ProtocolConformance;
5556
class ProtocolDecl;
5657
class Requirement;
5758
enum class ResilienceExpansion : unsigned;

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,14 @@ NOTE(function_builder_remove_attr, none,
50695069
"remove the attribute to explicitly disable the function builder", ())
50705070
NOTE(function_builder_remove_returns, none,
50715071
"remove 'return' statements to apply the function builder", ())
5072+
ERROR(function_builder_infer_ambig, none,
5073+
"ambiguous function builder inferred for %0: %1 or %2",
5074+
(DeclName, Type, Type))
5075+
NOTE(function_builder_infer_add_return, none,
5076+
"add an explicit 'return' statement to not use a function builder", ())
5077+
NOTE(function_builder_infer_pick_specific, none,
5078+
"apply function builder %0 (inferred from protocol %1)",
5079+
(Type, DeclName))
50725080

50735081
//------------------------------------------------------------------------------
50745082
// MARK: Tuple Shuffle Diagnostics

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,7 @@ class ClosureHasExplicitResultRequest
22402240
bool isCached() const { return true; }
22412241
};
22422242

2243-
using ProtocolConformanceLookupResult = SmallVector<ProtocolConformance *, 2>;
2243+
using ProtocolConformanceLookupResult = std::vector<ProtocolConformance *>;
22442244
void simple_display(llvm::raw_ostream &out, ConformanceLookupKind kind);
22452245

22462246
/// Lookup and expand all conformances in the given context.
@@ -2262,7 +2262,7 @@ class LookupAllConformancesInContextRequest
22622262
: public SimpleRequest<LookupAllConformancesInContextRequest,
22632263
ProtocolConformanceLookupResult(
22642264
const IterableDeclContext *),
2265-
RequestFlags::Uncached |
2265+
RequestFlags::Cached |
22662266
RequestFlags::DependencySink |
22672267
RequestFlags::DependencySource> {
22682268
public:
@@ -2276,6 +2276,8 @@ class LookupAllConformancesInContextRequest
22762276
evaluate(Evaluator &evaluator, const IterableDeclContext *IDC) const;
22772277

22782278
public:
2279+
bool isCached() const { return true; }
2280+
22792281
// Incremental dependencies
22802282
evaluator::DependencySource
22812283
readDependencySource(const evaluator::DependencyRecorder &eval) const;

include/swift/Basic/SimpleDisplay.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ namespace swift {
136136
out << "}";
137137
}
138138

139+
template<typename T>
140+
void simple_display(llvm::raw_ostream &out,
141+
const std::vector<T> &vec) {
142+
out << "{";
143+
bool first = true;
144+
for (const T &value : vec) {
145+
if (first) first = false;
146+
else out << ", ";
147+
148+
simple_display(out, value);
149+
}
150+
out << "}";
151+
}
152+
139153
template<typename T, typename U>
140154
void simple_display(llvm::raw_ostream &out,
141155
const llvm::PointerUnion<T, U> &ptrUnion) {

lib/AST/Attr.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,13 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
758758
case DAK_Custom: {
759759
if (!Options.IsForSwiftInterface)
760760
break;
761-
// For Swift interface, we should only print function builder attribute
762-
// on parameter decls. Printing the attribute elsewhere isn't ABI relevant.
761+
// For Swift interface, we should print function builder attributes
762+
// on parameter decls and on protocol requirements.
763+
// Printing the attribute elsewhere isn't ABI relevant.
763764
if (auto *VD = dyn_cast<ValueDecl>(D)) {
764765
if (VD->getAttachedFunctionBuilder() == this) {
765-
if (!isa<ParamDecl>(D))
766+
if (!isa<ParamDecl>(D) &&
767+
!(isa<VarDecl>(D) && isa<ProtocolDecl>(D->getDeclContext())))
766768
return false;
767769
}
768770
}

lib/AST/Decl.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6324,10 +6324,6 @@ void ParamDecl::setStoredProperty(VarDecl *var) {
63246324
}
63256325

63266326
Type ValueDecl::getFunctionBuilderType() const {
6327-
// Fast path: most declarations (especially parameters, which is where
6328-
// this is hottest) do not have any custom attributes at all.
6329-
if (!getAttrs().hasAttribute<CustomAttr>()) return Type();
6330-
63316327
auto &ctx = getASTContext();
63326328
auto mutableThis = const_cast<ValueDecl *>(this);
63336329
return evaluateOrDefault(ctx.evaluator,
@@ -6336,10 +6332,6 @@ Type ValueDecl::getFunctionBuilderType() const {
63366332
}
63376333

63386334
CustomAttr *ValueDecl::getAttachedFunctionBuilder() const {
6339-
// Fast path: most declarations (especially parameters, which is where
6340-
// this is hottest) do not have any custom attributes at all.
6341-
if (!getAttrs().hasAttribute<CustomAttr>()) return nullptr;
6342-
63436335
auto &ctx = getASTContext();
63446336
auto mutableThis = const_cast<ValueDecl *>(this);
63456337
return evaluateOrDefault(ctx.evaluator,

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,10 +1401,6 @@ BraceStmt *swift::applyFunctionBuilderTransform(
14011401
captured.first, captured.second)));
14021402
}
14031403

1404-
/// Find the return statements in the given body, which block the application
1405-
/// of a function builder.
1406-
static std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn);
1407-
14081404
Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
14091405
FuncDecl *func, Type builderType) {
14101406
// Pre-check the body: pre-check any expressions in it and look
@@ -1708,7 +1704,7 @@ PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval,
17081704
return PreCheckFunctionBuilderApplication(fn, false).run();
17091705
}
17101706

1711-
std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn) {
1707+
std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
17121708
PreCheckFunctionBuilderApplication precheck(fn, true);
17131709
(void)precheck.run();
17141710
return precheck.getReturnStmts();

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2971,9 +2971,11 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
29712971

29722972
// Module interfaces don't print bodies for all getters, so allow getters
29732973
// that don't have a body if we're compiling a module interface.
2974+
// Within a protocol definition, there will never be a body.
29742975
SourceFile *parent = storage->getDeclContext()->getParentSourceFile();
29752976
bool isInInterface = parent && parent->Kind == SourceFileKind::Interface;
2976-
if (!isInInterface && !getter->hasBody())
2977+
if (!isInInterface && !getter->hasBody() &&
2978+
!isa<ProtocolDecl>(storage->getDeclContext()))
29772979
return true;
29782980

29792981
return false;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5037,10 +5037,11 @@ diagnoseMissingAppendInterpolationMethod(NominalTypeDecl *typeDecl) {
50375037
}
50385038
}
50395039

5040-
SmallVector<ProtocolConformance *, 2>
5040+
std::vector<ProtocolConformance *>
50415041
LookupAllConformancesInContextRequest::evaluate(
50425042
Evaluator &eval, const IterableDeclContext *IDC) const {
5043-
return IDC->getLocalConformances(ConformanceLookupKind::All);
5043+
auto result = IDC->getLocalConformances(ConformanceLookupKind::All);
5044+
return std::vector<ProtocolConformance *>(result.begin(), result.end());
50445045
}
50455046

50465047
void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {

0 commit comments

Comments
 (0)