Skip to content

Commit a0fa03f

Browse files
authored
Merge pull request swiftlang#32013 from DougGregor/function-builder-infer-5.3
[5.3] [Function builders] Infer function builder from a protocol requirement.
2 parents 8533ef0 + 2442b5a commit a0fa03f

17 files changed

+386
-23
lines changed

include/swift/AST/ASTTypeIDZone.def

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

include/swift/AST/ASTTypeIDs.h

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

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5048,6 +5048,14 @@ NOTE(function_builder_remove_attr, none,
50485048
"remove the attribute to explicitly disable the function builder", ())
50495049
NOTE(function_builder_remove_returns, none,
50505050
"remove 'return' statements to apply the function builder", ())
5051+
ERROR(function_builder_infer_ambig, none,
5052+
"ambiguous function builder inferred for %0: %1 or %2",
5053+
(DeclName, Type, Type))
5054+
NOTE(function_builder_infer_add_return, none,
5055+
"add an explicit 'return' statement to not use a function builder", ())
5056+
NOTE(function_builder_infer_pick_specific, none,
5057+
"apply function builder %0 (inferred from protocol %1)",
5058+
(Type, DeclName))
50515059

50525060
//------------------------------------------------------------------------------
50535061
// MARK: Tuple Shuffle Diagnostics

include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ class ClosureHasExplicitResultRequest
22342234
bool isCached() const { return true; }
22352235
};
22362236

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

22402240
/// Lookup and expand all conformances in the given context.
@@ -2255,7 +2255,7 @@ void simple_display(llvm::raw_ostream &out, ConformanceLookupKind kind);
22552255
class LookupAllConformancesInContextRequest
22562256
: public SimpleRequest<LookupAllConformancesInContextRequest,
22572257
ProtocolConformanceLookupResult(const DeclContext *),
2258-
RequestFlags::Uncached |
2258+
RequestFlags::Cached |
22592259
RequestFlags::DependencySink |
22602260
RequestFlags::DependencySource> {
22612261
public:
@@ -2269,6 +2269,8 @@ class LookupAllConformancesInContextRequest
22692269
evaluate(Evaluator &evaluator, const DeclContext *DC) const;
22702270

22712271
public:
2272+
bool isCached() const { return true; }
2273+
22722274
// Incremental dependencies
22732275
evaluator::DependencySource readDependencySource(Evaluator &eval) const;
22742276
void writeDependencySink(Evaluator &eval, ReferencedNameTracker &tracker,

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
@@ -6345,10 +6345,6 @@ void ParamDecl::setStoredProperty(VarDecl *var) {
63456345
}
63466346

63476347
Type ValueDecl::getFunctionBuilderType() const {
6348-
// Fast path: most declarations (especially parameters, which is where
6349-
// this is hottest) do not have any custom attributes at all.
6350-
if (!getAttrs().hasAttribute<CustomAttr>()) return Type();
6351-
63526348
auto &ctx = getASTContext();
63536349
auto mutableThis = const_cast<ValueDecl *>(this);
63546350
return evaluateOrDefault(ctx.evaluator,
@@ -6357,10 +6353,6 @@ Type ValueDecl::getFunctionBuilderType() const {
63576353
}
63586354

63596355
CustomAttr *ValueDecl::getAttachedFunctionBuilder() const {
6360-
// Fast path: most declarations (especially parameters, which is where
6361-
// this is hottest) do not have any custom attributes at all.
6362-
if (!getAttrs().hasAttribute<CustomAttr>()) return nullptr;
6363-
63646356
auto &ctx = getASTContext();
63656357
auto mutableThis = const_cast<ValueDecl *>(this);
63666358
return evaluateOrDefault(ctx.evaluator,

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,10 +1223,6 @@ BraceStmt *swift::applyFunctionBuilderTransform(
12231223
captured.first, captured.second)));
12241224
}
12251225

1226-
/// Find the return statements in the given body, which block the application
1227-
/// of a function builder.
1228-
static std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn);
1229-
12301226
Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
12311227
FuncDecl *func, Type builderType) {
12321228
// Pre-check the body: pre-check any expressions in it and look
@@ -1552,7 +1548,7 @@ PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval,
15521548
return PreCheckFunctionBuilderApplication(fn, false).run();
15531549
}
15541550

1555-
std::vector<ReturnStmt *> findReturnStatements(AnyFunctionRef fn) {
1551+
std::vector<ReturnStmt *> TypeChecker::findReturnStatements(AnyFunctionRef fn) {
15561552
PreCheckFunctionBuilderApplication precheck(fn, true);
15571553
(void)precheck.run();
15581554
return precheck.getReturnStmts();

lib/Sema/TypeCheckAttr.cpp

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

29422942
// Module interfaces don't print bodies for all getters, so allow getters
29432943
// that don't have a body if we're compiling a module interface.
2944+
// Within a protocol definition, there will never be a body.
29442945
SourceFile *parent = storage->getDeclContext()->getParentSourceFile();
29452946
bool isInInterface = parent && parent->Kind == SourceFileKind::Interface;
2946-
if (!isInInterface && !getter->hasBody())
2947+
if (!isInInterface && !getter->hasBody() &&
2948+
!isa<ProtocolDecl>(storage->getDeclContext()))
29472949
return true;
29482950

29492951
return false;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5100,10 +5100,11 @@ diagnoseMissingAppendInterpolationMethod(NominalTypeDecl *typeDecl) {
51005100
}
51015101
}
51025102

5103-
SmallVector<ProtocolConformance *, 2>
5103+
std::vector<ProtocolConformance *>
51045104
LookupAllConformancesInContextRequest::evaluate(
51055105
Evaluator &eval, const DeclContext *DC) const {
5106-
return DC->getLocalConformances(ConformanceLookupKind::All);
5106+
auto result = DC->getLocalConformances(ConformanceLookupKind::All);
5107+
return std::vector<ProtocolConformance *>(result.begin(), result.end());
51075108
}
51085109

51095110
void TypeChecker::checkConformancesInContext(DeclContext *dc,

0 commit comments

Comments
 (0)