-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathTypeCheckDistributed.cpp
928 lines (779 loc) · 32.2 KB
/
TypeCheckDistributed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
//===--- TypeCheckDistributed.cpp - Distributed ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements type checking support for Swift's concurrency model.
//
//===----------------------------------------------------------------------===//
#include "TypeCheckConcurrency.h"
#include "TypeCheckDistributed.h"
#include "TypeChecker.h"
#include "swift/Strings.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/DistributedDecl.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/Basic/Defer.h"
#include "swift/AST/ASTPrinter.h"
using namespace swift;
// ==== ------------------------------------------------------------------------
bool swift::ensureDistributedModuleLoaded(Decl *decl) {
auto &C = decl->getASTContext();
auto moduleAvailable = evaluateOrDefault(
C.evaluator, DistributedModuleIsAvailableRequest{decl}, false);
return moduleAvailable;
}
bool
DistributedModuleIsAvailableRequest::evaluate(Evaluator &evaluator,
Decl *decl) const {
auto &C = decl->getASTContext();
if (C.getLoadedModule(C.Id_Distributed))
return true;
// seems we're missing the Distributed module, ask to import it explicitly
decl->diagnose(diag::distributed_actor_needs_explicit_distributed_import);
return false;
}
/******************************************************************************/
/************ LOCATING AD-HOC PROTOCOL REQUIREMENT IMPLS **********************/
/******************************************************************************/
static AbstractFunctionDecl *findDistributedAdHocRequirement(
NominalTypeDecl *decl, Identifier identifier,
std::function<bool(AbstractFunctionDecl *)> matchFn) {
auto &C = decl->getASTContext();
// It would be nice to check if this is a DistributedActorSystem
// "conforming" type, but we can't do this as we invoke this function WHILE
// deciding if the type conforms or not;
// Not via `ensureDistributedModuleLoaded` to avoid generating a warning,
// we won't be emitting the offending decl after all.
if (!C.getLoadedModule(C.Id_Distributed)) {
return nullptr;
}
llvm::SmallVector<ValueDecl *, 2> results;
decl->lookupQualified(decl, DeclNameRef(identifier),
SourceLoc(), NL_QualifiedDefault, results);
for (auto value : results) {
auto func = dyn_cast<AbstractFunctionDecl>(value);
if (func && matchFn(func))
return func;
}
return nullptr;
}
AbstractFunctionDecl *
GetDistributedActorSystemRemoteCallFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl, bool isVoidReturn) const {
auto &C = decl->getASTContext();
auto callId = isVoidReturn ? C.Id_remoteCallVoid : C.Id_remoteCall;
return findDistributedAdHocRequirement(
decl, callId, [isVoidReturn](AbstractFunctionDecl *func) {
return func->isDistributedActorSystemRemoteCall(isVoidReturn);
});
}
AbstractFunctionDecl *
GetDistributedTargetInvocationEncoderRecordArgumentFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &C = decl->getASTContext();
return findDistributedAdHocRequirement(
decl, C.Id_recordArgument, [](AbstractFunctionDecl *func) {
return func->isDistributedTargetInvocationEncoderRecordArgument();
});
}
AbstractFunctionDecl *
GetDistributedTargetInvocationEncoderRecordReturnTypeFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &C = decl->getASTContext();
return findDistributedAdHocRequirement(
decl, C.Id_recordReturnType, [](AbstractFunctionDecl *func) {
return func->isDistributedTargetInvocationEncoderRecordReturnType();
});
}
AbstractFunctionDecl *
GetDistributedTargetInvocationEncoderRecordErrorTypeFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &C = decl->getASTContext();
return findDistributedAdHocRequirement(
decl, C.Id_recordErrorType, [](AbstractFunctionDecl *func) {
return func->isDistributedTargetInvocationEncoderRecordErrorType();
});
}
AbstractFunctionDecl *
GetDistributedTargetInvocationDecoderDecodeNextArgumentFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &C = decl->getASTContext();
return findDistributedAdHocRequirement(
decl, C.Id_decodeNextArgument, [](AbstractFunctionDecl *func) {
return func->isDistributedTargetInvocationDecoderDecodeNextArgument();
});
}
AbstractFunctionDecl *
GetDistributedTargetInvocationResultHandlerOnReturnFunctionRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &C = decl->getASTContext();
return findDistributedAdHocRequirement(
decl, C.Id_onReturn, [](AbstractFunctionDecl *func) {
return func->isDistributedTargetInvocationResultHandlerOnReturn();
});
}
// ==== ------------------------------------------------------------------------
/// Add Fix-It text for the given protocol type to inherit DistributedActor.
void swift::diagnoseDistributedFunctionInNonDistributedActorProtocol(
const ProtocolDecl *proto, InFlightDiagnostic &diag) {
if (proto->getInherited().empty()) {
SourceLoc fixItLoc = proto->getBraces().Start;
diag.fixItInsert(fixItLoc, ": DistributedActor");
} else {
// Similar to how Sendable FitIts do this, we insert at the end of
// the inherited types.
SourceLoc fixItLoc = proto->getInherited().getEndLoc();
diag.fixItInsertAfter(fixItLoc, ", DistributedActor");
}
}
/// Add Fix-It text for the given nominal type to adopt Codable.
///
/// Useful when 'Codable' is the 'SerializationRequirement' and a non-Codable
/// function parameter or return value type is detected.
void swift::addCodableFixIt(
const NominalTypeDecl *nominal, InFlightDiagnostic &diag) {
if (nominal->getInherited().empty()) {
SourceLoc fixItLoc = nominal->getBraces().Start;
diag.fixItInsert(fixItLoc, ": Codable");
} else {
SourceLoc fixItLoc = nominal->getInherited().getEndLoc();
diag.fixItInsertAfter(fixItLoc, ", Codable");
}
}
// ==== ------------------------------------------------------------------------
bool IsDistributedActorRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *nominal) const {
// Protocols are actors if they inherit from `DistributedActor`.
if (auto protocol = dyn_cast<ProtocolDecl>(nominal)) {
auto &ctx = protocol->getASTContext();
auto *distributedActorProtocol = ctx.getDistributedActorDecl();
if (!distributedActorProtocol)
return false;
return (protocol == distributedActorProtocol ||
protocol->inheritsFrom(distributedActorProtocol));
}
// Class declarations are 'distributed actors' if they are declared with
// 'distributed actor'
auto classDecl = dyn_cast<ClassDecl>(nominal);
if(!classDecl)
return false;
return classDecl->isExplicitDistributedActor();
}
// ==== ------------------------------------------------------------------------
static bool checkAdHocRequirementAccessControl(
NominalTypeDecl *decl,
ProtocolDecl *proto,
AbstractFunctionDecl *func) {
if (!func)
return true;
// === check access control
if (func->getEffectiveAccess() == decl->getEffectiveAccess()) {
return false;
}
func->diagnose(diag::witness_not_accessible_type, diag::RequirementKind::Func,
func, /*isSetter=*/false,
/*requiredAccess=*/AccessLevel::Public, AccessLevel::Public,
proto);
return true;
}
static bool diagnoseMissingAdHocProtocolRequirement(ASTContext &C, Identifier identifier, NominalTypeDecl *decl) {
assert(decl);
auto FixitLocation = decl->getBraces().Start;
// Prepare the indent (same as `printRequirementStub`)
StringRef ExtraIndent;
StringRef CurrentIndent =
Lexer::getIndentationForLine(C.SourceMgr, decl->getStartLoc(), &ExtraIndent);
llvm::SmallString<128> Text;
llvm::raw_svector_ostream OS(Text);
ExtraIndentStreamPrinter Printer(OS, CurrentIndent);
Printer.printNewline();
Printer.printIndent();
Printer << (decl->getFormalAccess() == AccessLevel::Public ? "public " : "");
if (identifier == C.Id_remoteCall) {
Printer << "func remoteCall<Act, Err, Res>("
"on actor: Act, "
"target: RemoteCallTarget, "
"invocation: inout InvocationEncoder, "
"throwing: Err.Type, "
"returning: Res.Type) "
"async throws -> Res "
"where Act: DistributedActor, "
"Act.ID == ActorID, "
"Err: Error, "
"Res: SerializationRequirement";
} else if (identifier == C.Id_remoteCallVoid) {
Printer << "func remoteCallVoid<Act, Err>("
"on actor: Act, "
"target: RemoteCallTarget, "
"invocation: inout InvocationEncoder, "
"throwing: Err.Type"
") async throws "
"where Act: DistributedActor, "
"Act.ID == ActorID, "
"Err: Error";
} else if (identifier == C.Id_recordArgument) {
Printer << "mutating func recordArgument<Value: SerializationRequirement>(_ argument: RemoteCallArgument<Value>) throws";
} else if (identifier == C.Id_recordReturnType) {
Printer << "mutating func recordReturnType<Res: SerializationRequirement>(_ resultType: Res.Type) throws";
} else if (identifier == C.Id_decodeNextArgument) {
Printer << "mutating func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument";
} else if (identifier == C.Id_onReturn) {
Printer << "func onReturn<Success: SerializationRequirement>(value: Success) async throws";
} else {
llvm_unreachable("Unknown identifier for diagnosing ad-hoc missing requirement.");
}
/// Print the "{ <#code#> }" placeholder body
Printer << " {\n";
Printer << ExtraIndent << getCodePlaceholder();
Printer.printNewline();
Printer.printIndent();
Printer << "}\n";
decl->diagnose(
diag::distributed_actor_system_conformance_missing_adhoc_requirement,
decl, identifier);
decl->diagnose(diag::missing_witnesses_general)
.fixItInsertAfter(FixitLocation, Text.str());
return true;
}
bool swift::checkDistributedActorSystemAdHocProtocolRequirements(
ASTContext &C,
ProtocolDecl *Proto,
NormalProtocolConformance *Conformance,
Type Adoptee,
bool diagnose) {
auto decl = Adoptee->getAnyNominal();
auto anyMissingAdHocRequirements = false;
// ==== ----------------------------------------------------------------------
// Check the ad-hoc requirements of 'DistributedActorSystem":
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedActorSystem)) {
// - remoteCall
auto remoteCallDecl =
getRemoteCallOnDistributedActorSystem(decl, /*isVoidReturn=*/false);
if (!remoteCallDecl && diagnose) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_remoteCall, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, remoteCallDecl)) {
anyMissingAdHocRequirements = true;
}
// - remoteCallVoid
auto remoteCallVoidDecl =
getRemoteCallOnDistributedActorSystem(decl, /*isVoidReturn=*/true);
if (!remoteCallVoidDecl && diagnose) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_remoteCallVoid, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, remoteCallVoidDecl)) {
anyMissingAdHocRequirements = true;
}
return anyMissingAdHocRequirements;
}
// ==== ----------------------------------------------------------------------
// Check the ad-hoc requirements of 'DistributedTargetInvocationEncoder'
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedTargetInvocationEncoder)) {
// - recordArgument
auto recordArgumentDecl =
getRecordArgumentOnDistributedInvocationEncoder(decl);
if (!recordArgumentDecl) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_recordArgument, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, recordArgumentDecl)) {
anyMissingAdHocRequirements = true;
}
// - recordReturnType
auto recordReturnTypeDecl =
getRecordReturnTypeOnDistributedInvocationEncoder(decl);
if (!recordReturnTypeDecl) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_recordReturnType, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, recordReturnTypeDecl)) {
anyMissingAdHocRequirements = true;
}
return anyMissingAdHocRequirements;
}
// ==== ----------------------------------------------------------------------
// Check the ad-hoc requirements of 'DistributedTargetInvocationDecoder'
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedTargetInvocationDecoder)) {
// - decodeNextArgument
auto decodeNextArgumentDecl =
getDecodeNextArgumentOnDistributedInvocationDecoder(decl);
if (!decodeNextArgumentDecl) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_decodeNextArgument, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, decodeNextArgumentDecl)) {
anyMissingAdHocRequirements = true;
}
return anyMissingAdHocRequirements;
}
// === -----------------------------------------------------------------------
// Check the ad-hoc requirements of 'DistributedTargetInvocationResultHandler'
if (Proto->isSpecificProtocol(KnownProtocolKind::DistributedTargetInvocationResultHandler)) {
// - onReturn
auto onReturnDecl =
getOnReturnOnDistributedTargetInvocationResultHandler(decl);
if (!onReturnDecl) {
anyMissingAdHocRequirements = diagnoseMissingAdHocProtocolRequirement(C, C.Id_onReturn, decl);
}
if (checkAdHocRequirementAccessControl(decl, Proto, onReturnDecl)) {
anyMissingAdHocRequirements = true;
}
return anyMissingAdHocRequirements;
}
assert(!anyMissingAdHocRequirements &&
"Should have returned in appropriate type checking block earlier!");
return false;
}
static bool checkDistributedTargetResultType(
ModuleDecl *module, ValueDecl *valueDecl,
Type serializationRequirement,
bool diagnose) {
auto &C = valueDecl->getASTContext();
if (serializationRequirement && serializationRequirement->hasError()) {
return false;
}
if (!serializationRequirement || serializationRequirement->hasError()) {
return false; // error of the type would be diagnosed elsewhere
}
Type resultType;
if (auto func = dyn_cast<FuncDecl>(valueDecl)) {
resultType = func->mapTypeIntoContext(func->getResultInterfaceType());
} else if (auto var = dyn_cast<VarDecl>(valueDecl)) {
resultType = var->getInterfaceType();
} else {
llvm_unreachable("Unsupported distributed target");
}
if (resultType->isVoid())
return false;
SmallVector<ProtocolDecl *, 4> serializationRequirements;
// Collect extra "SerializationRequirement: SomeProtocol" requirements
if (serializationRequirement && !serializationRequirement->hasError()) {
auto srl = serializationRequirement->getExistentialLayout();
llvm::copy(srl.getProtocols(),
std::back_inserter(serializationRequirements));
}
auto isCodableRequirement =
checkDistributedSerializationRequirementIsExactlyCodable(
C, serializationRequirement);
for (auto serializationReq: serializationRequirements) {
auto conformance =
module->checkConformance(resultType, serializationReq);
if (conformance.isInvalid()) {
if (diagnose) {
llvm::StringRef conformanceToSuggest = isCodableRequirement ?
"Codable" : // Codable is a typealias, easier to diagnose like that
serializationReq->getNameStr();
auto diag = valueDecl->diagnose(
diag::distributed_actor_target_result_not_codable,
resultType,
valueDecl,
conformanceToSuggest
);
if (isCodableRequirement) {
if (auto resultNominalType = resultType->getAnyNominal()) {
addCodableFixIt(resultNominalType, diag);
}
}
} // end if: diagnose
return true;
}
}
return false;
}
bool swift::checkDistributedActorSystem(const NominalTypeDecl *system) {
auto nominal = const_cast<NominalTypeDecl *>(system);
// ==== Ensure the Distributed module is available,
// without it there's no reason to check the decl in more detail anyway.
if (!swift::ensureDistributedModuleLoaded(nominal))
return true;
// === AssociatedTypes
// --- SerializationRequirement MUST be a protocol TODO(distributed): rdar://91663941
// we may lift this in the future and allow classes but this requires more
// work to enable associatedtypes to be constrained to class or protocol,
// which then will unlock using them as generic constraints in protocols.
Type requirementTy = getDistributedActorSystemSerializationType(nominal);
if (auto existentialTy = requirementTy->getAs<ExistentialType>()) {
requirementTy = existentialTy->getConstraintType();
}
if (auto alias = dyn_cast<TypeAliasType>(requirementTy.getPointer())) {
auto concreteReqTy = alias->getDesugaredType();
if (auto comp = dyn_cast<ProtocolCompositionType>(concreteReqTy)) {
// ok, protocol composition is fine as requirement,
// since special case of just a single protocol
} else if (auto proto = dyn_cast<ProtocolType>(concreteReqTy)) {
// ok, protocols is exactly what we want to be used as constraints here
} else {
nominal->diagnose(diag::distributed_actor_system_serialization_req_must_be_protocol,
requirementTy);
return true;
}
}
// all good, didn't find any errors
return false;
}
/// Check whether the function is a proper distributed function
///
/// \returns \c true if there was a problem with adding the attribute, \c false
/// otherwise.
bool swift::checkDistributedFunction(AbstractFunctionDecl *func) {
auto &C = func->getASTContext();
return evaluateOrDefault(C.evaluator,
CheckDistributedFunctionRequest{func},
false); // no error if cycle
}
bool CheckDistributedFunctionRequest::evaluate(
Evaluator &evaluator, AbstractFunctionDecl *func) const {
if (auto *accessor = dyn_cast<AccessorDecl>(func)) {
auto *var = cast<VarDecl>(accessor->getStorage());
assert(var->isDistributed() && accessor->isGetter());
} else {
assert(func->isDistributed());
}
auto &C = func->getASTContext();
auto module = func->getParentModule();
/// If no distributed module is available, then no reason to even try checks.
if (!C.getLoadedModule(C.Id_Distributed))
return true;
Type serializationReqType =
getDistributedActorSerializationType(func->getDeclContext());
for (auto param: *func->getParameters()) {
// --- Check the parameter conforming to serialization requirements
if (serializationReqType && !serializationReqType->hasError()) {
// If the requirement is exactly `Codable` we diagnose it ia bit nicer.
auto serializationRequirementIsCodable =
checkDistributedSerializationRequirementIsExactlyCodable(
C, serializationReqType);
// --- Check parameters for 'SerializationRequirement' conformance
auto paramTy = func->mapTypeIntoContext(param->getInterfaceType());
auto srl = serializationReqType->getExistentialLayout();
for (auto req: srl.getProtocols()) {
if (module->checkConformance(paramTy, req).isInvalid()) {
auto diag = func->diagnose(
diag::distributed_actor_func_param_not_codable,
param->getArgumentName().str(), param->getInterfaceType(),
func->getDescriptiveKind(),
serializationRequirementIsCodable ? "Codable"
: req->getNameStr());
if (auto paramNominalTy = paramTy->getAnyNominal()) {
addCodableFixIt(paramNominalTy, diag);
} // else, no nominal type to suggest the fixit for, e.g. a closure
return true;
}
}
}
// --- Check parameters for various illegal modifiers
if (param->isInOut()) {
param->diagnose(
diag::distributed_actor_func_inout,
param->getName(),
func
).fixItRemove(SourceRange(param->getTypeSourceRangeForDiagnostics().Start,
param->getTypeSourceRangeForDiagnostics().Start.getAdvancedLoc(1)));
// FIXME(distributed): the fixIt should be on param->getSpecifierLoc(), but that Loc is invalid for some reason?
return true;
}
if (param->getSpecifier() == ParamSpecifier::LegacyShared ||
param->getSpecifier() == ParamSpecifier::LegacyOwned ||
param->getSpecifier() == ParamSpecifier::Consuming ||
param->getSpecifier() == ParamSpecifier::Borrowing) {
param->diagnose(
diag::distributed_actor_func_unsupported_specifier,
ParamDecl::getSpecifierSpelling(param->getSpecifier()),
param->getName(),
func);
return true;
}
if (param->isVariadic()) {
param->diagnose(
diag::distributed_actor_func_variadic,
param->getName(),
func
);
}
}
// --- Result type must be either void or a serialization requirement conforming type
if (checkDistributedTargetResultType(module, func, serializationReqType,
/*diagnose=*/true)) {
return true;
}
return false;
}
/// Check whether the function is a proper distributed computed property
///
/// \param diagnose Whether to emit a diagnostic when a problem is encountered.
///
/// \returns \c true if there was a problem with adding the attribute, \c false
/// otherwise.
bool swift::checkDistributedActorProperty(VarDecl *var, bool diagnose) {
// without the distributed module, we can't check any of these.
if (!ensureDistributedModuleLoaded(var))
return true;
/// === Check if the declaration is a valid combination of attributes
if (var->isStatic()) {
if (diagnose)
var->diagnose(diag::distributed_property_cannot_be_static,
var->getName());
// TODO(distributed): fixit, offer removing the static keyword
return true;
}
// it is not a computed property
if (var->isLet() || var->hasStorageOrWrapsStorage()) {
if (diagnose)
var->diagnose(diag::distributed_property_can_only_be_computed, var);
return true;
}
// distributed properties cannot have setters
if (var->getWriteImpl() != swift::WriteImplKind::Immutable) {
if (diagnose)
var->diagnose(diag::distributed_property_can_only_be_computed_get_only,
var->getName());
return true;
}
auto serializationRequirement =
getDistributedActorSerializationType(var->getDeclContext());
auto module = var->getModuleContext();
if (checkDistributedTargetResultType(module, var, serializationRequirement, diagnose)) {
return true;
}
return false;
}
void swift::checkDistributedActorProperties(const NominalTypeDecl *decl) {
auto &C = decl->getASTContext();
if (auto sourceFile = decl->getDeclContext()->getParentSourceFile()) {
if (sourceFile->Kind == SourceFileKind::Interface) {
// Don't diagnose properties in swiftinterfaces.
return;
}
} else {
// Don't diagnose when checking without source file (e.g. from module, importer etc).
return;
}
if (isa<ProtocolDecl>(decl)) {
// protocols don't matter for stored property checking
return;
}
for (auto member : decl->getMembers()) {
if (auto prop = dyn_cast<VarDecl>(member)) {
if (prop->isSynthesized())
continue;
auto id = prop->getName();
if (id == C.Id_actorSystem || id == C.Id_id) {
prop->diagnose(diag::distributed_actor_user_defined_special_property,
id);
prop->setInvalid();
}
}
}
}
// ==== ------------------------------------------------------------------------
void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal) {
if (!nominal)
return;
// ==== Ensure the Distributed module is available,
// without it there's no reason to check the decl in more detail anyway.
if (!swift::ensureDistributedModuleLoaded(nominal))
return;
// ==== Constructors
// --- Get the default initializer
// If applicable, this will create the default 'init(transport:)' initializer
(void)nominal->getDefaultInitializer();
for (auto member : nominal->getMembers()) {
// --- Ensure 'distributed func' all thunks
if (auto *var = dyn_cast<VarDecl>(member)) {
if (!var->isDistributed())
continue;
if (auto thunk = var->getDistributedThunk())
SF->addDelayedFunction(thunk);
continue;
}
// --- Ensure 'distributed func' all thunks
if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
if (!func->isDistributed())
continue;
if (!isa<ProtocolDecl>(nominal)) {
auto systemTy = getConcreteReplacementForProtocolActorSystemType(func);
if (!systemTy || systemTy->hasError()) {
nominal->diagnose(
diag::distributed_actor_conformance_missing_system_type,
nominal->getName());
return;
}
}
if (auto thunk = func->getDistributedThunk()) {
SF->addDelayedFunction(thunk);
}
}
}
// ==== Properties
checkDistributedActorProperties(nominal);
// --- Synthesize the 'id' property here rather than via derived conformance
// because the 'DerivedConformanceDistributedActor' won't trigger for 'id'
// because it has a default impl via 'Identifiable' (ObjectIdentifier)
// which we do not want.
// Also, the 'id' var must be added before the 'actorSystem'.
// See NOTE (id-before-actorSystem) for more details.
(void)nominal->getDistributedActorIDProperty();
}
bool TypeChecker::checkDistributedFunc(FuncDecl *func) {
if (!func->isDistributed())
return false;
return swift::checkDistributedFunction(func);
}
ConstructorDecl*
GetDistributedRemoteCallTargetInitFunctionRequest::evaluate(
Evaluator &evaluator,
NominalTypeDecl *nominal) const {
auto &C = nominal->getASTContext();
// not via `ensureDistributedModuleLoaded` to avoid generating a warning,
// we won't be emitting the offending decl after all.
if (!C.getLoadedModule(C.Id_Distributed))
return nullptr;
if (!nominal->getDeclaredInterfaceType()->isEqual(
C.getRemoteCallTargetType()))
return nullptr;
for (auto value : nominal->getMembers()) {
auto ctor = dyn_cast<ConstructorDecl>(value);
if (!ctor)
continue;
auto params = ctor->getParameters();
if (params->size() != 1)
return nullptr;
// _ identifier
if (params->get(0)->getArgumentName().empty())
return ctor;
return nullptr;
}
return nullptr;
}
ConstructorDecl*
GetDistributedRemoteCallArgumentInitFunctionRequest::evaluate(
Evaluator &evaluator,
NominalTypeDecl *nominal) const {
auto &C = nominal->getASTContext();
// not via `ensureDistributedModuleLoaded` to avoid generating a warning,
// we won't be emitting the offending decl after all.
if (!C.getLoadedModule(C.Id_Distributed))
return nullptr;
if (!nominal->getDeclaredInterfaceType()->isEqual(
C.getRemoteCallArgumentType()))
return nullptr;
for (auto value : nominal->getMembers()) {
auto ctor = dyn_cast<ConstructorDecl>(value);
if (!ctor)
continue;
auto params = ctor->getParameters();
if (params->size() != 3)
return nullptr;
// --- param: label
if (!params->get(0)->getArgumentName().is("label"))
return nullptr;
// --- param: name
if (!params->get(1)->getArgumentName().is("name"))
return nullptr;
// --- param: value
if (params->get(2)->getArgumentName() != C.Id_value)
return nullptr;
return ctor;
}
return nullptr;
}
NominalTypeDecl *
GetDistributedActorInvocationDecoderRequest::evaluate(Evaluator &evaluator,
NominalTypeDecl *actor) const {
auto &ctx = actor->getASTContext();
auto decoderTy = getAssociatedTypeOfDistributedSystemOfActor(
actor, ctx.Id_InvocationDecoder);
return decoderTy ? decoderTy->getAnyNominal() : nullptr;
}
FuncDecl *
GetDistributedActorConcreteArgumentDecodingMethodRequest::evaluate(
Evaluator &evaluator, NominalTypeDecl *decl) const {
auto &ctx = decl->getASTContext();
if (auto actor = dyn_cast<ClassDecl>(decl)) {
auto *decoder = getDistributedActorInvocationDecoder(actor);
// If distributed actor is generic over actor system, there is not
// going to be a concrete decoder.
if (!decoder)
return nullptr;
auto decoderTy = decoder->getDeclaredInterfaceType();
auto members =
TypeChecker::lookupMember(actor->getDeclContext(), decoderTy,
DeclNameRef(ctx.Id_decodeNextArgument));
// typealias SerializationRequirement = any ...
auto serializationTy = getAssociatedTypeOfDistributedSystemOfActor(
actor, ctx.Id_SerializationRequirement);
if (!serializationTy || !serializationTy->is<ExistentialType>())
return nullptr;
SmallVector<ProtocolDecl *, 4> serializationRequirements;
{
auto layout = serializationTy->getExistentialLayout();
llvm::copy(layout.getProtocols(),
std::back_inserter(serializationRequirements));
}
SmallVector<FuncDecl *, 2> candidates;
// Looking for `decodeNextArgument<Arg: <SerializationReq>>() throws -> Arg`
for (auto &member : members) {
auto *FD = dyn_cast<FuncDecl>(member.getValueDecl());
if (!FD || FD->hasAsync() || !FD->hasThrows())
continue;
auto *params = FD->getParameters();
// No arguments.
if (params->size() != 0)
continue;
auto genericParamList = FD->getGenericParams();
// A single generic parameter.
if (genericParamList->size() != 1)
continue;
auto paramTy =
genericParamList->getParams()[0]->getDeclaredInterfaceType();
// `decodeNextArgument` should return its generic parameter value
if (!FD->getResultInterfaceType()->isEqual(paramTy))
continue;
// Let's find out how many serialization requirements does this method cover e.g. `Codable` is two requirements - `Encodable` and `Decodable`.
auto nextArgumentSig = FD->getGenericSignature();
bool okay =
llvm::all_of(serializationRequirements, [&](ProtocolDecl *p) -> bool {
return nextArgumentSig->requiresProtocol(paramTy, p);
});
// If the current method covers all of the serialization requirements,
// it's a match. Note that it might also have other requirements, but
// we let that go as long as there are no two candidates that differ
// only in generic requirements.
if (okay)
candidates.push_back(FD);
}
// Type-checker should reject any definition of invocation decoder
// that doesn't have a correct version of `decodeNextArgument` declared.
assert(candidates.size() == 1);
return candidates.front();
}
/// No concrete candidate found, return null and perform the call via a
/// witness
return nullptr;
}
llvm::ArrayRef<ValueDecl *>
GetDistributedMethodWitnessedProtocolRequirements::evaluate(
Evaluator &evaluator,
AbstractFunctionDecl *afd) const {
// Only a 'distributed' decl can witness 'distributed' protocol
assert(afd->isDistributed());
auto &C = afd->getASTContext();
auto result = llvm::SmallVector<ValueDecl *, 1>();
for (auto witnessedRequirement : afd->getSatisfiedProtocolRequirements()) {
if (witnessedRequirement->isDistributed()) {
result.push_back(witnessedRequirement);
}
}
return C.AllocateCopy(result);
}