Skip to content

Commit 9325d1a

Browse files
committed
---
yaml --- r: 340383 b: refs/heads/rxwei-patch-1 c: 0078422 h: refs/heads/master i: 340381: ed8a6d7 340379: 1fdff37 340375: 020952d 340367: cba1b78 340351: 4dc7c2c
1 parent 5229be7 commit 9325d1a

13 files changed

+158
-111
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: 2268939f3b23a040e039bcc75c01c33d5e4aea8d
1018+
refs/heads/rxwei-patch-1: 007842261f06a62c2b6a75bf3c6ea3219aff4971
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,6 +4475,10 @@ ERROR(function_builder_parameter_not_of_function_type, none,
44754475
"function builder attribute %0 can only be applied to a parameter of "
44764476
"function type",
44774477
(DeclName))
4478+
ERROR(function_builder_parameter_autoclosure, none,
4479+
"function builder attribute %0 cannot be applied to an autoclosure "
4480+
"parameter",
4481+
(DeclName))
44784482
ERROR(function_builder_multiple, none,
44794483
"only one function builder attribute can be attached to a parameter", ())
44804484
NOTE(previous_function_builder_here, none,

branches/rxwei-patch-1/include/swift/AST/TypeCheckRequests.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -649,38 +649,22 @@ class AttachedFunctionBuilderRequest :
649649
void noteCycleStep(DiagnosticEngine &diags) const;
650650
};
651651

652-
/// Kinds of types for CustomAttr.
653-
enum class CustomAttrTypeKind {
654-
/// The type is required to not be expressed in terms of
655-
/// any contextual type parameters.
656-
NonGeneric,
657-
658-
/// Property delegates have some funky rules, like allowing
659-
/// unbound generic types.
660-
PropertyDelegate,
661-
};
662-
663-
void simple_display(llvm::raw_ostream &out, CustomAttrTypeKind value);
664-
665652
/// Request the type spelled out in a custom attribute.
666653
///
667654
/// Different parameters cannot be used for the same attribute.
668-
class CustomAttrTypeRequest :
669-
public SimpleRequest<CustomAttrTypeRequest,
655+
class FunctionBuilderTypeRequest :
656+
public SimpleRequest<FunctionBuilderTypeRequest,
670657
CacheKind::Cached,
671658
Type,
672-
CustomAttr *,
673-
DeclContext *,
674-
CustomAttrTypeKind> {
659+
ParamDecl *> {
675660
public:
676661
using SimpleRequest::SimpleRequest;
677662

678663
private:
679664
friend SimpleRequest;
680665

681666
llvm::Expected<Type>
682-
evaluate(Evaluator &evaluator, CustomAttr *attr, DeclContext *dc,
683-
CustomAttrTypeKind typeKind) const;
667+
evaluate(Evaluator &evaluator, ParamDecl *param) const;
684668

685669
public:
686670
// Caching

branches/rxwei-patch-1/include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ SWIFT_TYPEID(AttachedPropertyWrapperTypeRequest)
3434
SWIFT_TYPEID(PropertyWrapperBackingPropertyTypeRequest)
3535
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfoRequest)
3636
SWIFT_TYPEID(AttachedFunctionBuilderRequest)
37-
SWIFT_TYPEID(CustomAttrTypeRequest)
37+
SWIFT_TYPEID(FunctionBuilderTypeRequest)

branches/rxwei-patch-1/lib/AST/Decl.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,15 +5741,14 @@ void ParamDecl::setStoredProperty(VarDecl *var) {
57415741
}
57425742

57435743
Type ParamDecl::getFunctionBuilderType() const {
5744-
auto attr = getAttachedFunctionBuilder();
5745-
if (!attr) return Type();
5744+
// Fast path: most parameters do not have any attributes at all,
5745+
// much less custom ones.
5746+
if (!getAttrs().hasAttribute<CustomAttr>()) return Type();
57465747

5747-
auto mutableAttr = const_cast<CustomAttr*>(attr);
57485748
auto &ctx = getASTContext();
5749-
auto dc = getDeclContext();
5749+
auto mutableThis = const_cast<ParamDecl *>(this);
57505750
return evaluateOrDefault(ctx.evaluator,
5751-
CustomAttrTypeRequest{mutableAttr, dc,
5752-
CustomAttrTypeKind::NonGeneric},
5751+
FunctionBuilderTypeRequest{mutableThis},
57535752
Type());
57545753
}
57555754

branches/rxwei-patch-1/lib/AST/TypeCheckRequests.cpp

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ void swift::simple_display(llvm::raw_ostream &out,
5959
}
6060
}
6161

62+
template<>
63+
bool swift::AnyValue::Holder<Type>::equals(const HolderBase &other) const {
64+
assert(typeID == other.typeID && "Caller should match type IDs");
65+
return value.getPointer() ==
66+
static_cast<const Holder<Type> &>(other).value.getPointer();
67+
}
68+
69+
void swift::simple_display(llvm::raw_ostream &out, Type type) {
70+
if (type)
71+
type.print(out);
72+
else
73+
out << "null";
74+
}
75+
6276
//----------------------------------------------------------------------------//
6377
// Inherited type computation.
6478
//----------------------------------------------------------------------------//
@@ -688,20 +702,6 @@ void swift::simple_display(
688702
out << " }";
689703
}
690704

691-
template<>
692-
bool swift::AnyValue::Holder<Type>::equals(const HolderBase &other) const {
693-
assert(typeID == other.typeID && "Caller should match type IDs");
694-
return value.getPointer() ==
695-
static_cast<const Holder<Type> &>(other).value.getPointer();
696-
}
697-
698-
void swift::simple_display(llvm::raw_ostream &out, Type type) {
699-
if (type)
700-
type.print(out);
701-
else
702-
out << "null";
703-
}
704-
705705
//----------------------------------------------------------------------------//
706706
// StructuralTypeRequest.
707707
//----------------------------------------------------------------------------//
@@ -715,34 +715,7 @@ void StructuralTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
715715
}
716716

717717
//----------------------------------------------------------------------------//
718-
// CustomAttrTypeRequest.
719-
//----------------------------------------------------------------------------//
720-
721-
void CustomAttrTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
722-
auto attr = std::get<0>(getStorage());
723-
diags.diagnose(attr->getLocation(), diag::circular_reference);
724-
}
725-
726-
void CustomAttrTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
727-
auto attr = std::get<0>(getStorage());
728-
diags.diagnose(attr->getLocation(), diag::circular_reference_through);
729-
}
730-
731-
void swift::simple_display(llvm::raw_ostream &out, CustomAttrTypeKind value) {
732-
switch (value) {
733-
case CustomAttrTypeKind::NonGeneric:
734-
out << "non-generic";
735-
return;
736-
737-
case CustomAttrTypeKind::PropertyDelegate:
738-
out << "property-delegate";
739-
return;
740-
}
741-
llvm_unreachable("bad kind");
742-
}
743-
744-
//----------------------------------------------------------------------------//
745-
// Finding the attached @functionBuilder for a parameter.
718+
// FunctionBuilder-related requests.
746719
//----------------------------------------------------------------------------//
747720

748721
bool AttachedFunctionBuilderRequest::isCached() const {
@@ -760,3 +733,11 @@ void AttachedFunctionBuilderRequest::noteCycleStep(
760733
DiagnosticEngine &diags) const {
761734
std::get<0>(getStorage())->diagnose(diag::circular_reference_through);
762735
}
736+
737+
void FunctionBuilderTypeRequest::diagnoseCycle(DiagnosticEngine &diags) const {
738+
std::get<0>(getStorage())->diagnose(diag::circular_reference);
739+
}
740+
741+
void FunctionBuilderTypeRequest::noteCycleStep(DiagnosticEngine &diags) const {
742+
std::get<0>(getStorage())->diagnose(diag::circular_reference_through);
743+
}

branches/rxwei-patch-1/lib/Sema/TypeCheckAttr.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,17 +2577,6 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
25772577
return;
25782578
}
25792579

2580-
// If we can't resolve an interface type here, we should be in invalid
2581-
// code.
2582-
Type type = param->getInterfaceType();
2583-
if (type && !type->is<FunctionType>()) {
2584-
TC.diagnose(attr->getLocation(),
2585-
diag::function_builder_parameter_not_of_function_type,
2586-
nominal->getFullName());
2587-
attr->setInvalid();
2588-
return;
2589-
}
2590-
25912580
// Diagnose and ignore arguments.
25922581
if (attr->getArg()) {
25932582
TC.diagnose(attr->getLocation(), diag::function_builder_arguments)

branches/rxwei-patch-1/lib/Sema/TypeCheckRequestFunctions.cpp

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -173,40 +173,53 @@ AttachedFunctionBuilderRequest::evaluate(Evaluator &evaluator,
173173
}
174174

175175
llvm::Expected<Type>
176-
CustomAttrTypeRequest::evaluate(Evaluator &evaluator,
177-
CustomAttr *attr, DeclContext *dc,
178-
CustomAttrTypeKind typeKind) const {
179-
auto resolution = TypeResolution::forContextual(dc);
180-
TypeResolutionOptions options(TypeResolverContext::PatternBindingDecl);
181-
182-
// Property delegates allow their type to be an unbound generic.
183-
if (typeKind == CustomAttrTypeKind::PropertyDelegate)
184-
options |= TypeResolutionFlags::AllowUnboundGenerics;
185-
186-
ASTContext &ctx = dc->getASTContext();
187-
auto &tc = *static_cast<TypeChecker *>(ctx.getLazyResolver());
188-
if (tc.validateType(attr->getTypeLoc(), resolution, options))
189-
return ErrorType::get(ctx);
190-
191-
// We always require the type to resolve to a nominal type.
192-
Type type = attr->getTypeLoc().getType();
193-
if (!type->getAnyNominal()) {
176+
FunctionBuilderTypeRequest::evaluate(Evaluator &evaluator,
177+
ParamDecl *param) const {
178+
// Look for a function-builder custom attribute.
179+
auto attr = param->getAttachedFunctionBuilder();
180+
if (!attr) return Type();
181+
182+
// Resolve a type for the attribute.
183+
auto mutableAttr = const_cast<CustomAttr*>(attr);
184+
auto dc = param->getDeclContext();
185+
auto &ctx = dc->getASTContext();
186+
Type type = resolveCustomAttrType(mutableAttr, dc,
187+
CustomAttrTypeKind::NonGeneric);
188+
if (!type) return Type();
189+
190+
// The type must not be contextually-dependent.
191+
if (type->hasArchetype()) {
192+
ctx.Diags.diagnose(attr->getLocation(),
193+
diag::function_builder_type_contextual, type);
194+
return Type();
195+
}
196+
197+
auto nominal = type->getAnyNominal();
198+
if (!nominal) {
194199
assert(ctx.Diags.hadAnyError());
195-
return ErrorType::get(ctx);
200+
return Type();
196201
}
197202

198-
switch (typeKind) {
199-
case CustomAttrTypeKind::NonGeneric:
200-
if (type->hasArchetype()) {
201-
ctx.Diags.diagnose(attr->getLocation(),
202-
diag::function_builder_type_contextual, type);
203-
return ErrorType::get(ctx);
204-
}
205-
break;
203+
// The parameter had better already have an interface type.
204+
Type paramType = param->getInterfaceType();
205+
assert(paramType);
206+
auto paramFnType = paramType->getAs<FunctionType>();
207+
208+
// Require the parameter to be an interface type.
209+
if (!paramFnType) {
210+
ctx.Diags.diagnose(attr->getLocation(),
211+
diag::function_builder_parameter_not_of_function_type,
212+
nominal->getFullName());
213+
mutableAttr->setInvalid();
214+
return Type();
215+
}
206216

207-
case CustomAttrTypeKind::PropertyDelegate:
208-
// No further logic required here.
209-
break;
217+
if (param->isAutoClosure()) {
218+
ctx.Diags.diagnose(attr->getLocation(),
219+
diag::function_builder_parameter_autoclosure,
220+
nominal->getFullName());
221+
mutableAttr->setInvalid();
222+
return Type();
210223
}
211224

212225
return type;

branches/rxwei-patch-1/lib/Sema/TypeCheckType.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,3 +3580,27 @@ void TypeChecker::checkUnsupportedProtocolType(GenericParamList *genericParams)
35803580
UnsupportedProtocolVisitor visitor(*this, /*checkStatements=*/false);
35813581
visitor.visitRequirements(genericParams->getRequirements());
35823582
}
3583+
3584+
Type swift::resolveCustomAttrType(CustomAttr *attr, DeclContext *dc,
3585+
CustomAttrTypeKind typeKind) {
3586+
auto resolution = TypeResolution::forContextual(dc);
3587+
TypeResolutionOptions options(TypeResolverContext::PatternBindingDecl);
3588+
3589+
// Property delegates allow their type to be an unbound generic.
3590+
if (typeKind == CustomAttrTypeKind::PropertyDelegate)
3591+
options |= TypeResolutionFlags::AllowUnboundGenerics;
3592+
3593+
ASTContext &ctx = dc->getASTContext();
3594+
auto &tc = *static_cast<TypeChecker *>(ctx.getLazyResolver());
3595+
if (tc.validateType(attr->getTypeLoc(), resolution, options))
3596+
return Type();
3597+
3598+
// We always require the type to resolve to a nominal type.
3599+
Type type = attr->getTypeLoc().getType();
3600+
if (!type->getAnyNominal()) {
3601+
assert(ctx.Diags.hadAnyError());
3602+
return Type();
3603+
}
3604+
3605+
return type;
3606+
}

branches/rxwei-patch-1/lib/Sema/TypeCheckType.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ class TypeResolution {
377377
bool areSameType(Type type1, Type type2) const;
378378
};
379379

380+
/// Kinds of types for CustomAttr.
381+
enum class CustomAttrTypeKind {
382+
/// The type is required to not be expressed in terms of
383+
/// any contextual type parameters.
384+
NonGeneric,
385+
386+
/// Property delegates have some funky rules, like allowing
387+
/// unbound generic types.
388+
PropertyDelegate,
389+
};
390+
391+
/// Attempt to resolve a concrete type for a custom attribute.
392+
Type resolveCustomAttrType(CustomAttr *attr, DeclContext *dc,
393+
CustomAttrTypeKind typeKind);
394+
380395
} // end namespace swift
381396

382397
#endif /* SWIFT_SEMA_TYPE_CHECK_TYPE_H */

branches/rxwei-patch-1/test/decl/var/function_builders.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ func makerParamMissing2(@Maker
4444
func makerParamExtra(@Maker(5) // expected-error {{function builder attributes cannot have arguments}}
4545
fn: () -> ()) {}
4646

47+
func makerParamAutoclosure(@Maker // expected-error {{function builder attribute 'Maker' cannot be applied to an autoclosure parameter}}
48+
fn: @autoclosure () -> ()) {}
49+
4750
@_functionBuilder
4851
struct GenericMaker<T> {} // expected-note {{generic type 'GenericMaker' declared here}}
4952

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@_functionBuilder
2+
struct TupleBuilder {
3+
static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
4+
return (t1, t2)
5+
}
6+
7+
static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
8+
-> (T1, T2, T3) {
9+
return (t1, t2, t3)
10+
}
11+
12+
static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
13+
-> (T1, T2, T3, T4) {
14+
return (t1, t2, t3, t4)
15+
}
16+
17+
static func buildBlock<T1, T2, T3, T4, T5>(
18+
_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
19+
) -> (T1, T2, T3, T4, T5) {
20+
return (t1, t2, t3, t4, t5)
21+
}
22+
}
23+
24+
func tuplify<T>(@TupleBuilder body: () -> T) -> T {
25+
return body()
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -typecheck %S/Inputs/function_builder_definition.swift -primary-file %s
2+
3+
func test0() -> (Int, Float, String) {
4+
return tuplify {
5+
17
6+
3.14159
7+
"Hello"
8+
}
9+
}

0 commit comments

Comments
 (0)