Skip to content

Commit 2456a6e

Browse files
committed
Finish main callable functionality.
- Use uniform naming: either "`call` member" or "`call` declaration". - Fix `call` member resolution during type-checking. - Enable more `func` attributes on `call` declarations. - Add tests.
1 parent 61ee914 commit 2456a6e

14 files changed

+212
-219
lines changed

include/swift/AST/Attr.def

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ DECL_ATTR(available, Available,
117117
AllowMultipleAttributes | LongAttribute,
118118
1)
119119
CONTEXTUAL_SIMPLE_DECL_ATTR(final, Final,
120-
OnClass | OnFunc | OnAccessor | OnVar | OnSubscript |
120+
OnClass | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
121121
DeclModifier,
122122
2)
123123
DECL_ATTR(objc, ObjC,
@@ -129,7 +129,7 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(required, Required,
129129
DeclModifier,
130130
4)
131131
CONTEXTUAL_SIMPLE_DECL_ATTR(optional, Optional,
132-
OnConstructor | OnFunc | OnAccessor | OnVar | OnSubscript |
132+
OnConstructor | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
133133
DeclModifier,
134134
5)
135135
SIMPLE_DECL_ATTR(dynamicCallable, DynamicCallable,
@@ -183,7 +183,7 @@ DECL_ATTR(_semantics, Semantics,
183183
AllowMultipleAttributes | UserInaccessible,
184184
21)
185185
CONTEXTUAL_SIMPLE_DECL_ATTR(dynamic, Dynamic,
186-
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor |
186+
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
187187
DeclModifier,
188188
22)
189189
CONTEXTUAL_SIMPLE_DECL_ATTR(infix, Infix,
@@ -199,13 +199,14 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(postfix, Postfix,
199199
DeclModifier,
200200
25)
201201
SIMPLE_DECL_ATTR(_transparent, Transparent,
202-
OnFunc | OnAccessor | OnConstructor | OnVar | UserInaccessible,
202+
OnFunc | OnAccessor | OnCall | OnConstructor | OnVar | UserInaccessible,
203203
26)
204204
SIMPLE_DECL_ATTR(requires_stored_property_inits, RequiresStoredPropertyInits,
205205
OnClass,
206206
27)
207207
SIMPLE_DECL_ATTR(nonobjc, NonObjC,
208-
OnExtension | OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor,
208+
OnExtension | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
209+
OnConstructor,
209210
30)
210211
SIMPLE_DECL_ATTR(_fixed_layout, FixedLayout,
211212
OnVar | OnClass | OnStruct |
@@ -215,40 +216,41 @@ SIMPLE_DECL_ATTR(inlinable, Inlinable,
215216
OnVar | OnSubscript | OnAbstractFunction,
216217
32)
217218
DECL_ATTR(_specialize, Specialize,
218-
OnConstructor | OnFunc | OnAccessor |
219+
OnConstructor | OnFunc | OnAccessor | OnCall |
219220
AllowMultipleAttributes | LongAttribute | UserInaccessible,
220221
33)
221222
SIMPLE_DECL_ATTR(objcMembers, ObjCMembers,
222223
OnClass,
223224
34)
224225
CONTEXTUAL_SIMPLE_DECL_ATTR(__consuming, Consuming,
225-
OnFunc | OnAccessor |
226+
OnFunc | OnAccessor | OnCall |
226227
DeclModifier |
227228
UserInaccessible |
228229
NotSerialized, 40)
229230
CONTEXTUAL_SIMPLE_DECL_ATTR(mutating, Mutating,
230-
OnFunc | OnAccessor |
231+
OnFunc | OnAccessor | OnCall |
231232
DeclModifier |
232233
NotSerialized, 41)
233234
CONTEXTUAL_SIMPLE_DECL_ATTR(nonmutating, NonMutating,
234-
OnFunc | OnAccessor |
235+
OnFunc | OnAccessor | OnCall |
235236
DeclModifier |
236237
NotSerialized, 42)
237238
CONTEXTUAL_SIMPLE_DECL_ATTR(convenience, Convenience,
238239
OnConstructor |
239240
DeclModifier |
240241
NotSerialized, 43)
241242
CONTEXTUAL_SIMPLE_DECL_ATTR(override, Override,
242-
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor | OnAssociatedType |
243+
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
244+
OnAssociatedType |
243245
DeclModifier |
244246
NotSerialized, 44)
245247
SIMPLE_DECL_ATTR(_hasStorage, HasStorage,
246248
OnVar |
247249
UserInaccessible |
248250
NotSerialized, 45)
249251
DECL_ATTR(private, AccessControl,
250-
OnFunc | OnAccessor | OnExtension | OnGenericType | OnVar | OnSubscript |
251-
OnConstructor |
252+
OnFunc | OnAccessor | OnCall | OnExtension | OnGenericType | OnVar |
253+
OnSubscript | OnConstructor |
252254
DeclModifier |
253255
NotSerialized, 46)
254256
DECL_ATTR_ALIAS(fileprivate, AccessControl)
@@ -296,7 +298,7 @@ DECL_ATTR(_alignment, Alignment,
296298
UserInaccessible,
297299
56)
298300
SIMPLE_DECL_ATTR(rethrows, Rethrows,
299-
OnFunc | OnAccessor | OnConstructor |
301+
OnFunc | OnAccessor | OnCall | OnConstructor |
300302
RejectByParser,
301303
57)
302304
DECL_ATTR(_swift_native_objc_runtime_base, SwiftNativeObjCRuntimeBase,
@@ -307,30 +309,30 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(indirect, Indirect, DeclModifier |
307309
OnEnum | OnEnumElement,
308310
60)
309311
SIMPLE_DECL_ATTR(warn_unqualified_access, WarnUnqualifiedAccess,
310-
OnFunc | OnAccessor /*| OnVar*/ |
312+
OnFunc | OnAccessor | OnCall | /*OnVar |*/
311313
LongAttribute,
312314
61)
313315
SIMPLE_DECL_ATTR(_show_in_interface, ShowInInterface,
314316
OnProtocol |
315317
UserInaccessible,
316318
62)
317319
DECL_ATTR(_cdecl, CDecl,
318-
OnFunc | OnAccessor |
320+
OnFunc | OnAccessor | OnCall |
319321
LongAttribute | UserInaccessible,
320322
63)
321323
SIMPLE_DECL_ATTR(usableFromInline, UsableFromInline,
322324
OnAbstractFunction | OnVar | OnSubscript | OnNominalType | OnTypeAlias |
323325
LongAttribute,
324326
64)
325327
SIMPLE_DECL_ATTR(discardableResult, DiscardableResult,
326-
OnFunc | OnAccessor | OnConstructor |
328+
OnFunc | OnAccessor | OnCall | OnConstructor |
327329
LongAttribute,
328330
65)
329331
SIMPLE_DECL_ATTR(GKInspectable, GKInspectable,
330332
OnVar,
331333
66)
332334
DECL_ATTR(_implements, Implements,
333-
OnFunc | OnAccessor | OnVar | OnSubscript | OnTypeAlias |
335+
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnTypeAlias |
334336
UserInaccessible |
335337
NotSerialized, 67)
336338
DECL_ATTR(_objcRuntimeName, ObjCRuntimeName,
@@ -346,7 +348,7 @@ DECL_ATTR(_restatedObjCConformance, RestatedObjCConformance,
346348
NotSerialized, 70)
347349
// NOTE: 71 is unused
348350
SIMPLE_DECL_ATTR(_implicitly_unwrapped_optional, ImplicitlyUnwrappedOptional,
349-
OnFunc | OnAccessor | OnVar | OnParam | OnSubscript | OnConstructor |
351+
OnFunc | OnAccessor | OnCall | OnVar | OnParam | OnSubscript | OnConstructor |
350352
RejectByParser,
351353
72)
352354
DECL_ATTR(_optimize, Optimize,
@@ -358,7 +360,7 @@ DECL_ATTR(_clangImporterSynthesizedType, ClangImporterSynthesizedType,
358360
LongAttribute | RejectByParser | UserInaccessible |
359361
NotSerialized, 74)
360362
SIMPLE_DECL_ATTR(_weakLinked, WeakLinked,
361-
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnVar |
363+
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnCall | OnVar |
362364
OnSubscript | OnConstructor | OnEnumElement | OnExtension | UserInaccessible,
363365
75)
364366
SIMPLE_DECL_ATTR(_frozen, Frozen,
@@ -374,7 +376,8 @@ SIMPLE_DECL_ATTR(_hasInitialValue, HasInitialValue,
374376
UserInaccessible,
375377
78)
376378
SIMPLE_DECL_ATTR(_nonoverride, NonOverride,
377-
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor | OnAssociatedType |
379+
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
380+
OnAssociatedType |
378381
UserInaccessible | NotSerialized,
379382
79)
380383
DECL_ATTR(_dynamicReplacement, DynamicReplacement,

include/swift/AST/Decl.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,21 +3298,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
32983298
ToStoredPropertyOrMissingMemberPlaceholder());
32993299
}
33003300

3301-
private:
3302-
/// Predicate used to filter CallDeclRange.
3303-
struct ToCallDecl {
3304-
ToCallDecl() {}
3305-
Optional<CallDecl *> operator()(Decl *decl) const;
3306-
};
3307-
3308-
public:
3309-
/// A range for iterating the call declarations of a nominal type.
3310-
using CallDeclRange = OptionalTransformRange<DeclRange,
3311-
ToCallDecl>;
3312-
3313-
/// Return a collection of the call declarations of this nominal type.
3314-
CallDeclRange getCallDeclarations() const;
3315-
33163301
// Implement isa/cast/dyncast/etc.
33173302
static bool classof(const Decl *D) {
33183303
return D->getKind() >= DeclKind::First_NominalTypeDecl &&
@@ -6839,13 +6824,6 @@ ::operator()(Decl *decl) const {
68396824
return None;
68406825
}
68416826

6842-
inline Optional<CallDecl *>
6843-
NominalTypeDecl::ToCallDecl::operator()(Decl *decl) const {
6844-
if (auto callDecl = dyn_cast<CallDecl>(decl))
6845-
return callDecl;
6846-
return None;
6847-
}
6848-
68496827
inline void
68506828
AbstractStorageDecl::overwriteSetterAccess(AccessLevel accessLevel) {
68516829
Accessors.setInt(accessLevel);

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,15 +1692,15 @@ ERROR(witness_requires_class_implementation,none,
16921692
(DeclName, Type))
16931693
ERROR(witness_not_accessible_proto,none,
16941694
"%select{initializer %1|method %1|%select{|setter for }2property %1"
1695-
"|subscript%select{| setter}2}0 must be declared "
1695+
"|subscript%select{| setter}2|'call' member}0 must be declared "
16961696
"%select{%error|fileprivate|internal|public|%error}3 because it matches a "
16971697
"requirement in %select{private|fileprivate|internal|public|%error}4 protocol "
16981698
"%5",
16991699
(RequirementKind, DeclName, bool, AccessLevel, AccessLevel, DeclName))
17001700
ERROR(witness_not_accessible_type,none,
17011701
"%select{initializer %1|method %1|%select{|setter for }2property %1"
1702-
"|subscript%select{| setter}2}0 must be as accessible as its enclosing "
1703-
"type because it matches a requirement in protocol %5",
1702+
"|subscript%select{| setter}2|'call' member}0 must be as accessible as "
1703+
"its enclosing type because it matches a requirement in protocol %5",
17041704
(RequirementKind, DeclName, bool, AccessLevel, AccessLevel, DeclName))
17051705
ERROR(type_witness_not_accessible_proto,none,
17061706
"%0 %1 must be declared %select{%error|fileprivate|internal|public|%error}2 "
@@ -1776,11 +1776,13 @@ NOTE(missing_witnesses_general,none, "do you want to add protocol stubs?",
17761776
NOTE(ambiguous_witnesses,none,
17771777
"multiple matching "
17781778
"%select{initializers named %1|functions named %1|properties named %1|"
1779-
"subscript operators}0 with type %2", (RequirementKind, DeclName, Type))
1779+
"subscript operators|'call' member}0 with type %2",
1780+
(RequirementKind, DeclName, Type))
17801781
NOTE(ambiguous_witnesses_wrong_name,none,
17811782
"multiple matching "
17821783
"%select{initializers named %1|functions named %1|properties named %1|"
1783-
"subscript operators}0 with type %2", (RequirementKind, DeclName, Type))
1784+
"subscript operators|'call' member}0 with type %2",
1785+
(RequirementKind, DeclName, Type))
17841786
NOTE(no_witnesses_type,none,
17851787
"protocol requires nested type %0; do you want to add it?", (Identifier))
17861788
NOTE(default_associated_type_req_fail,none,
@@ -1844,7 +1846,7 @@ NOTE(protocol_witness_renamed,none,
18441846
"rename to %0 to satisfy this requirement%1", (DeclName, StringRef))
18451847
NOTE(protocol_witness_kind_conflict,none,
18461848
"candidate is not %select{an initializer|a function|a variable|"
1847-
"a subscript}0", (RequirementKind))
1849+
"a subscript|a 'call' member}0", (RequirementKind))
18481850
NOTE(protocol_witness_type_conflict,none,
18491851
"candidate has non-matching type %0%1", (Type, StringRef))
18501852
NOTE(protocol_witness_missing_requirement,none,

lib/AST/Decl.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,10 +3216,6 @@ auto NominalTypeDecl::getStoredProperties(bool skipInaccessible) const
32163216
ToStoredProperty(skipInaccessible));
32173217
}
32183218

3219-
auto NominalTypeDecl::getCallDeclarations() const -> CallDeclRange {
3220-
return CallDeclRange(getMembers(), ToCallDecl());
3221-
}
3222-
32233219
bool NominalTypeDecl::isOptionalDecl() const {
32243220
return this == getASTContext().getOptionalDecl();
32253221
}

lib/Sema/CSApply.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7134,6 +7134,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
71347134
};
71357135

71367136
// The function is always an rvalue.
7137+
// Save the original potentially lvalue function for rewriting `call` member
7138+
// applications.
7139+
auto *originalFn = fn;
71377140
fn = cs.coerceToRValue(fn);
71387141

71397142
// Resolve applications of decls with special semantics.
@@ -7320,24 +7323,36 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
73207323
return finishApply(apply, openedType, locator);
73217324
}
73227325

7323-
// Handle `call` method applications.
7326+
// Handle `call` member applications.
73247327
auto &ctx = cs.getASTContext();
73257328

73267329
TupleExpr *arg = dyn_cast<TupleExpr>(apply->getArg());
73277330
if (auto parenExpr = dyn_cast<ParenExpr>(apply->getArg()))
73287331
arg = TupleExpr::createImplicit(ctx, parenExpr->getSubExpr(), {});
73297332

7330-
// Get resolved `call` method and verify it.
7333+
// Get resolved `call` member and verify it.
73317334
auto loc = locator.withPathElement(ConstraintLocator::ApplyFunction);
73327335
auto selected = solution.getOverloadChoice(cs.getConstraintLocator(loc));
73337336
auto choice = selected.choice;
73347337
if (auto *method = dyn_cast<CallDecl>(selected.choice.getDecl())) {
7338+
auto methodType = selected.openedFullType->castTo<AnyFunctionType>();
73357339
bool isDynamic = choice.getKind() == OverloadChoiceKind::DeclViaDynamic;
73367340
auto callDeclLocator = cs.getConstraintLocator(
73377341
locator.withPathElement(ConstraintLocator::ApplyFunction)
73387342
.withPathElement(ConstraintLocator::CallMember));
7339-
// Create direct reference to `call` method.
7340-
Expr *declRef = buildMemberRef(fn, selected.openedFullType,
7343+
assert(methodType->getNumParams() == 1);
7344+
auto selfParam = methodType->getParams().front();
7345+
// Diagnose `mutating` method call on immutable value.
7346+
if (!cs.getType(originalFn)->hasLValueType() && selfParam.isInOut()) {
7347+
AssignmentFailure failure(
7348+
originalFn, cs, originalFn->getLoc(),
7349+
diag::cannot_pass_rvalue_mutating_subelement,
7350+
diag::cannot_pass_rvalue_mutating);
7351+
failure.diagnose();
7352+
return nullptr;
7353+
}
7354+
// Create direct reference to `call` member.
7355+
Expr *declRef = buildMemberRef(originalFn, selected.openedFullType,
73417356
/*dotLoc=*/SourceLoc(), choice,
73427357
DeclNameLoc(fn->getEndLoc()),
73437358
selected.openedType, locator,
@@ -7347,7 +7362,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
73477362
if (!declRef)
73487363
return nullptr;
73497364
declRef->setImplicit(apply->isImplicit());
7350-
apply->setFn(declRef) ;
7365+
apply->setFn(declRef);
73517366
Expr *result = apply;
73527367
cs.TC.typeCheckExpression(result, cs.DC);
73537368
cs.cacheExprTypes(result);

0 commit comments

Comments
 (0)