-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathOpenedExistentials.cpp
925 lines (782 loc) · 33.2 KB
/
OpenedExistentials.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
//===--- OpenedExistentials.cpp - Utilities for existential types ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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 defines common utilities for existential opening and some related
// things, such as the checks around covariant `Self` in class conformances.
//
//===----------------------------------------------------------------------===//
#include "OpenedExistentials.h"
#include "TypeChecker.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
GenericParameterReferenceInfo &
GenericParameterReferenceInfo::operator|=(const GenericParameterReferenceInfo &other) {
DirectRefs |= other.DirectRefs;
DepMemberTyRefs |= other.DepMemberTyRefs;
HasCovariantGenericParamResult |= other.HasCovariantGenericParamResult;
return *this;
}
/// Forward declaration.
static GenericParameterReferenceInfo
findGenericParameterReferencesRec(CanGenericSignature,
GenericTypeParamType *,
GenericTypeParamType *,
Type, TypePosition, bool);
/// Determine whether a function type with the given result type may have
/// a covariant generic parameter type result. This is true if the result type
/// is either a function type, or a generic parameter, possibly wrapped in some
/// level of optionality.
static bool canResultTypeHaveCovariantGenericParameterResult(Type resultTy) {
if (resultTy->is<AnyFunctionType>())
return true;
resultTy = resultTy->lookThroughAllOptionalTypes();
return resultTy->is<GenericTypeParamType>();
}
/// Report references to the given generic parameter within the given function
/// type using the given generic signature.
///
/// \param position The current position in terms of variance.
/// \param skipParamIndex The index of the parameter that shall be skipped.
static GenericParameterReferenceInfo findGenericParameterReferencesInFunction(
CanGenericSignature genericSig,
GenericTypeParamType *origParam,
GenericTypeParamType *openedParam,
const AnyFunctionType *fnType, TypePosition position,
bool canBeCovariantResult, std::optional<unsigned> skipParamIndex) {
// If there are no type parameters, we're done.
if (!isa<GenericFunctionType>(fnType) && !fnType->hasTypeParameter())
return GenericParameterReferenceInfo();
auto inputInfo = GenericParameterReferenceInfo();
const auto params = fnType->getParams();
for (const auto paramIdx : indices(params)) {
// If this is the parameter we were supposed to skip, do so.
if (skipParamIndex && paramIdx == *skipParamIndex)
continue;
const auto ¶m = params[paramIdx];
// inout types are invariant.
if (param.isInOut()) {
inputInfo |= ::findGenericParameterReferencesRec(
genericSig, origParam, openedParam, param.getPlainType(),
TypePosition::Invariant, /*canBeCovariantResult=*/false);
continue;
}
// Parameters are contravariant, but if we're prior to the skipped
// parameter treat them as invariant because we're not allowed to
// reference the parameter at all.
TypePosition paramPos = position.flipped();
if (skipParamIndex && paramIdx < *skipParamIndex)
paramPos = TypePosition::Invariant;
inputInfo |= ::findGenericParameterReferencesRec(
genericSig, origParam, openedParam, param.getParameterType(), paramPos,
/*canBeCovariantResult=*/false);
}
canBeCovariantResult =
// &= does not short-circuit.
canBeCovariantResult &&
canResultTypeHaveCovariantGenericParameterResult(fnType->getResult());
const auto resultInfo = ::findGenericParameterReferencesRec(
genericSig, origParam, openedParam, fnType->getResult(),
position, canBeCovariantResult);
return inputInfo |= resultInfo;
}
/// Report references to the given generic parameter within the given type
/// using the given generic signature.
///
/// \param position The current position in terms of variance.
static GenericParameterReferenceInfo
findGenericParameterReferencesRec(CanGenericSignature genericSig,
GenericTypeParamType *origParam,
GenericTypeParamType *openedParam,
Type type,
TypePosition position,
bool canBeCovariantResult) {
// If there are no type parameters, we're done.
if (!type->getCanonicalType()->hasTypeParameter())
return GenericParameterReferenceInfo();
// Tuples preserve variance.
if (auto tuple = type->getAs<TupleType>()) {
auto info = GenericParameterReferenceInfo();
for (auto &elt : tuple->getElements()) {
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, elt.getType(), position,
/*canBeCovariantResult=*/false);
}
return info;
}
// Function types preserve variance in the result type, and flip variance in
// the parameter type.
if (auto funcTy = type->getAs<AnyFunctionType>()) {
return findGenericParameterReferencesInFunction(
genericSig, origParam, openedParam, funcTy,
position, canBeCovariantResult,
/*skipParamIndex=*/std::nullopt);
}
// Metatypes preserve variance.
if (auto metaTy = type->getAs<AnyMetatypeType>()) {
return findGenericParameterReferencesRec(genericSig, origParam, openedParam,
metaTy->getInstanceType(),
position, canBeCovariantResult);
}
// Optionals preserve variance.
if (auto optType = type->getOptionalObjectType()) {
return findGenericParameterReferencesRec(
genericSig, origParam, openedParam, optType,
position, canBeCovariantResult);
}
// DynamicSelfType preserves variance.
if (auto selfType = type->getAs<DynamicSelfType>()) {
return findGenericParameterReferencesRec(genericSig, origParam, openedParam,
selfType->getSelfType(), position,
/*canBeCovariantResult=*/false);
}
if (auto *const nominal = type->getAs<NominalOrBoundGenericNominalType>()) {
auto info = GenericParameterReferenceInfo();
// Don't forget to look in the parent.
if (const auto parent = nominal->getParent()) {
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, parent, TypePosition::Invariant,
/*canBeCovariantResult=*/false);
}
// Most bound generic types are invariant.
if (auto *const bgt = type->getAs<BoundGenericType>()) {
if (bgt->isArray()) {
// Swift.Array preserves variance in its 'Value' type.
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, bgt->getGenericArgs().front(),
position, /*canBeCovariantResult=*/false);
} else if (bgt->isDictionary()) {
// Swift.Dictionary preserves variance in its 'Element' type.
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, bgt->getGenericArgs().front(),
TypePosition::Invariant, /*canBeCovariantResult=*/false);
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, bgt->getGenericArgs().back(),
position, /*canBeCovariantResult=*/false);
} else {
for (const auto ¶mType : bgt->getGenericArgs()) {
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, paramType,
TypePosition::Invariant, /*canBeCovariantResult=*/false);
}
}
}
return info;
}
// If the signature of an opaque result type has a same-type constraint
// that references Self, it's invariant.
if (auto opaque = type->getAs<OpaqueTypeArchetypeType>()) {
auto info = GenericParameterReferenceInfo();
auto opaqueSig = opaque->getDecl()->getOpaqueInterfaceGenericSignature();
for (const auto &req : opaqueSig.getRequirements()) {
switch (req.getKind()) {
case RequirementKind::SameShape:
llvm_unreachable("Same-shape requirement not supported here");
case RequirementKind::Conformance:
case RequirementKind::Layout:
continue;
case RequirementKind::SameType:
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, req.getFirstType(),
TypePosition::Invariant, /*canBeCovariantResult=*/false);
LLVM_FALLTHROUGH;
case RequirementKind::Superclass:
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, req.getSecondType(),
TypePosition::Invariant, /*canBeCovariantResult=*/false);
break;
}
}
return info;
}
if (auto *existential = type->getAs<ExistentialType>())
type = existential->getConstraintType();
// Protocol compositions are invariant.
if (auto *comp = type->getAs<ProtocolCompositionType>()) {
auto info = GenericParameterReferenceInfo();
for (auto member : comp->getMembers()) {
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, member,
TypePosition::Invariant, /*canBeCovariantResult=*/false);
}
return info;
}
// Packs are invariant.
if (auto *pack = type->getAs<PackType>()) {
auto info = GenericParameterReferenceInfo();
// FIXME: Source compatibility remedy to allow existential opening in
// the following case:
// ```
// protocol P {}
// struct S<each T> {}
// func foo<T: P>(_: T, _: S<T>? = nil) {}
// let p: any P
// foo(p)
// ```
//
// for (auto arg : pack->getElementTypes()) {
// info |= findGenericParameterReferencesRec(
// genericSig, origParam, openedParam, arg,
// TypePosition::Invariant, /*canBeCovariantResult=*/false);
// }
(void)pack;
return info;
}
// Pack expansions are invariant.
if (auto *expansion = type->getAs<PackExpansionType>()) {
return findGenericParameterReferencesRec(
genericSig, origParam, openedParam, expansion->getPatternType(),
TypePosition::Invariant, /*canBeCovariantResult=*/false);
}
// Specifically ignore parameterized protocols because we can erase them to
// the upper bound.
if (type->is<ParameterizedProtocolType>()) {
return GenericParameterReferenceInfo();
}
// Everything else should be a type parameter.
if (!type->isTypeParameter()) {
llvm::errs() << "Unhandled type:\n";
type->dump(llvm::errs());
abort();
}
if (!type->getRootGenericParam()->isEqual(origParam)) {
return GenericParameterReferenceInfo();
}
// A direct reference to 'Self'.
if (type->is<GenericTypeParamType>()) {
if (position == TypePosition::Covariant && canBeCovariantResult)
return GenericParameterReferenceInfo::forCovariantGenericParamResult();
return GenericParameterReferenceInfo::forDirectRef(position);
}
if (origParam != openedParam) {
// Replace the original parameter with the parameter in the opened
// signature.
type = type.subst(
[&](SubstitutableType *type) {
ASSERT(type->isEqual(origParam));
return openedParam;
},
MakeAbstractConformanceForGenericType());
}
if (genericSig) {
// If the type parameter is beyond the domain of the opened
// signature, ignore it.
if (!genericSig->isValidTypeParameter(type)) {
return GenericParameterReferenceInfo();
}
if (auto reducedTy = genericSig.getReducedType(type)) {
if (!reducedTy->isEqual(type)) {
// Note: origParam becomes openedParam for the recursive call,
// because concreteTy is written in terms of genericSig and not
// the signature of the old origParam.
return findGenericParameterReferencesRec(
CanGenericSignature(), openedParam, openedParam, reducedTy,
position, canBeCovariantResult);
}
}
}
// A reference to an associated type rooted on 'Self'.
return GenericParameterReferenceInfo::forDependentMemberTypeRef(position);
}
GenericParameterReferenceInfo
swift::findGenericParameterReferences(const ValueDecl *value,
CanGenericSignature sig,
GenericTypeParamType *origParam,
GenericTypeParamType *openedParam,
std::optional<unsigned> skipParamIndex) {
if (isa<TypeDecl>(value))
return GenericParameterReferenceInfo();
auto type = value->getInterfaceType();
// Skip invalid declarations.
if (type->hasError())
return GenericParameterReferenceInfo();
// For functions and subscripts, take skipParamIndex into account.
if (isa<AbstractFunctionDecl>(value) || isa<SubscriptDecl>(value)) {
// And for a method, skip the 'self' parameter.
if (value->hasCurriedSelf())
type = type->castTo<AnyFunctionType>()->getResult();
return ::findGenericParameterReferencesInFunction(
sig, origParam, openedParam, type->castTo<AnyFunctionType>(),
TypePosition::Covariant, /*canBeCovariantResult=*/true,
skipParamIndex);
}
return ::findGenericParameterReferencesRec(sig, origParam, openedParam, type,
TypePosition::Covariant,
/*canBeCovariantResult=*/true);
}
GenericParameterReferenceInfo swift::findExistentialSelfReferences(
const ValueDecl *value) {
auto *dc = value->getDeclContext();
ASSERT(dc->getSelfProtocolDecl());
auto sig = dc->getGenericSignatureOfContext().getCanonicalSignature();
auto genericParam = dc->getSelfInterfaceType()->castTo<GenericTypeParamType>();
return findGenericParameterReferences(value, sig, genericParam, genericParam,
std::nullopt);
}
bool HasSelfOrAssociatedTypeRequirementsRequest::evaluate(
Evaluator &evaluator, ProtocolDecl *decl) const {
// ObjC protocols do not require `any`.
if (decl->isObjC())
return false;
for (auto member : decl->getMembers()) {
// Existential types require `any` if the protocol has an associated type.
if (isa<AssociatedTypeDecl>(member))
return true;
// For value members, look at their type signatures.
if (auto valueMember = dyn_cast<ValueDecl>(member)) {
const auto info = findExistentialSelfReferences(valueMember);
if (info.hasNonCovariantRef() || info.hasDependentMemberTypeRef()) {
return true;
}
}
}
// Check whether any of the inherited protocols require `any`.
for (auto proto : decl->getInheritedProtocols()) {
if (proto->hasSelfOrAssociatedTypeRequirements())
return true;
}
return false;
}
/// A protocol member accessed with an existential value might have generic
/// constraints that require the ability to spell an opened archetype in order
/// to be satisfied. Such are
/// - superclass requirements, when the object is a non-'Self'-rooted type
/// parameter, and the subject is dependent on 'Self', e.g. U : G<Self.A>
/// - same-type requirements, when one side is dependent on 'Self', and the
/// other is a non-'Self'-rooted type parameter, e.g. U.Element == Self.
///
/// Because opened archetypes are not part of the surface language, these
/// constraints render the member inaccessible.
static bool doesMemberHaveUnfulfillableConstraintsWithExistentialBase(
OpenedExistentialSignature existentialSig, const ValueDecl *member) {
const auto sig =
member->getInnermostDeclContext()->getGenericSignatureOfContext();
// Fast path: the member is generic only over 'Self'.
if (sig.getGenericParams().size() == 1) {
return false;
}
class IsDependentOnOpenedExistentialSelf : public TypeWalker {
OpenedExistentialSignature existentialSig;
public:
explicit IsDependentOnOpenedExistentialSelf(OpenedExistentialSignature existentialSig)
: existentialSig(existentialSig) {}
Action walkToTypePre(Type ty) override {
// We're looking at the interface type of a protocol member, so it's written
// in terms of `Self` (tau_0_0) and possibly type parameters at higher depth:
//
// <Self, ... where Self: P, ...>
if (!ty->isTypeParameter()) {
return Action::Continue;
}
if (ty->getRootGenericParam()->getDepth() > 0) {
return Action::SkipNode;
}
// Ok, we found a type parameter rooted in `Self`. Replace `Self` with the
// opened Self type in the existential signature, which looks like this:
//
// <..., Self where ..., Self: P>
ty = ty.subst(
[&](SubstitutableType *type) -> Type {
return existentialSig.SelfType;
},
MakeAbstractConformanceForGenericType());
// Make sure this is valid first.
if (!existentialSig.OpenedSig->isValidTypeParameter(ty)) {
return Action::SkipNode;
}
// If the existential type constrains Self.U to a type from the outer
// context, then the reduced type of Self.U in the existential signature
// will no longer contain Self.
ty = existentialSig.OpenedSig.getReducedType(ty);
if (!ty.findIf([&](Type t) -> bool {
if (auto *paramTy = t->getAs<GenericTypeParamType>())
return paramTy->isEqual(existentialSig.SelfType);
return false;
})) {
return Action::SkipNode;
}
// Ok, we found a type that depends on the opened existential Self.
return Action::Stop;
}
} isDependentOnSelf(existentialSig);
for (const auto &req : sig.getRequirements()) {
switch (req.getKind()) {
case RequirementKind::Superclass: {
if (req.getFirstType()->getRootGenericParam()->getDepth() > 0 &&
req.getSecondType().walk(isDependentOnSelf)) {
return true;
}
break;
}
case RequirementKind::SameType:
case RequirementKind::SameShape: {
const auto isNonSelfRootedTypeParam = [](Type ty) {
return ty->isTypeParameter() &&
ty->getRootGenericParam()->getDepth() > 0;
};
if ((isNonSelfRootedTypeParam(req.getFirstType()) &&
req.getSecondType().walk(isDependentOnSelf)) ||
(isNonSelfRootedTypeParam(req.getSecondType()) &&
req.getFirstType().walk(isDependentOnSelf))) {
return true;
}
break;
}
case RequirementKind::Conformance:
case RequirementKind::Layout:
break;
}
}
return false;
}
ExistentialMemberAccessLimitation
swift::isMemberAvailableOnExistential(Type baseTy, const ValueDecl *member) {
auto *dc = member->getDeclContext();
if (!dc->getSelfProtocolDecl()) {
return ExistentialMemberAccessLimitation::None;
}
auto &ctx = member->getASTContext();
auto existentialSig = ctx.getOpenedExistentialSignature(baseTy);
auto origParam = dc->getSelfInterfaceType()->castTo<GenericTypeParamType>();
auto openedParam = existentialSig.SelfType->castTo<GenericTypeParamType>();
// An accessor or non-storage member is not available if its interface type
// contains a non-covariant reference to a 'Self'-rooted type parameter in the
// context of the base type's existential signature.
auto info = findGenericParameterReferences(
member, existentialSig.OpenedSig, origParam, openedParam,
std::nullopt);
auto result = ExistentialMemberAccessLimitation::None;
if (!info) {
// Nothing to do.
} else if (info.hasRef(TypePosition::Invariant)) {
// An invariant reference is decisive.
result = ExistentialMemberAccessLimitation::Unsupported;
} else if (isa<AbstractFunctionDecl>(member)) {
// Anything non-covariant is decisive for functions.
if (info.hasRef(TypePosition::Contravariant)) {
result = ExistentialMemberAccessLimitation::Unsupported;
}
} else {
const auto isGetterUnavailable = info.hasRef(TypePosition::Contravariant);
auto isSetterUnavailable = true;
if (isa<VarDecl>(member)) {
// For properties, the setter is unavailable if the interface type has a
// covariant reference, which becomes contravariant is the setter.
isSetterUnavailable = info.hasRef(TypePosition::Covariant);
} else {
// For subscripts specifically, we must scan the setter directly because
// whether a covariant reference in the interface type becomes
// contravariant in the setter depends on the location of the reference
// (in the indices or the result type).
auto *setter =
cast<SubscriptDecl>(member)->getAccessor(AccessorKind::Set);
const auto setterInfo = setter ? findGenericParameterReferences(
setter, existentialSig.OpenedSig,
origParam, openedParam, std::nullopt)
: GenericParameterReferenceInfo();
isSetterUnavailable = setterInfo.hasRef(TypePosition::Contravariant);
}
if (isGetterUnavailable && isSetterUnavailable) {
result = ExistentialMemberAccessLimitation::Unsupported;
} else if (isGetterUnavailable) {
result = ExistentialMemberAccessLimitation::WriteOnly;
} else if (isSetterUnavailable) {
result = ExistentialMemberAccessLimitation::ReadOnly;
}
}
// If the member access is not supported whatsoever, we are done.
if (result == ExistentialMemberAccessLimitation::Unsupported)
return result;
// Before proceeding with the result, see if we find a generic requirement
// that cannot be satisfied; if we do, the member is unavailable after all.
if (doesMemberHaveUnfulfillableConstraintsWithExistentialBase(existentialSig,
member)) {
return ExistentialMemberAccessLimitation::Unsupported;
}
return result;
}
std::optional<std::pair<TypeVariableType *, Type>>
swift::canOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
Type paramTy, Type argTy) {
if (!callee)
return std::nullopt;
// Only applies to functions and subscripts.
if (!isa<AbstractFunctionDecl>(callee) && !isa<SubscriptDecl>(callee))
return std::nullopt;
// Special semantics prohibit opening existentials.
switch (TypeChecker::getDeclTypeCheckingSemantics(callee)) {
case DeclTypeCheckingSemantics::OpenExistential:
case DeclTypeCheckingSemantics::TypeOf:
// type(of:) and _openExistential handle their own opening.
return std::nullopt;
case DeclTypeCheckingSemantics::Normal:
case DeclTypeCheckingSemantics::WithoutActuallyEscaping:
break;
}
// C++ function templates require specialization, which is not possible with
// opened existential archetypes, so do not open.
if (isa_and_nonnull<clang::FunctionTemplateDecl>(callee->getClangDecl()))
return std::nullopt;
// The actual parameter type needs to involve a type variable, otherwise
// type inference won't be possible.
if (!paramTy->hasTypeVariable())
return std::nullopt;
auto param = getParameterAt(callee, paramIdx);
if (!param)
return std::nullopt;
// If the parameter is non-generic variadic, don't open.
if (param->isVariadic())
return std::nullopt;
// The rvalue argument type needs to be an existential type or metatype
// thereof.
const auto rValueArgTy = argTy->getWithoutSpecifierType();
if (!rValueArgTy->isAnyExistentialType())
return std::nullopt;
GenericTypeParamType *genericParam;
TypeVariableType *typeVar;
Type bindingTy;
std::tie(genericParam, typeVar, bindingTy) = [=] {
// Look through an inout and optional type.
Type genericParam = param->getInterfaceType()
->getInOutObjectType()
->lookThroughSingleOptionalType();
Type typeVar =
paramTy->getInOutObjectType()->lookThroughSingleOptionalType();
Type bindingTy = rValueArgTy;
// Look through a metatype.
if (genericParam->is<AnyMetatypeType>()) {
genericParam = genericParam->getMetatypeInstanceType();
typeVar = typeVar->getMetatypeInstanceType();
bindingTy = bindingTy->getMetatypeInstanceType();
}
return std::tuple(genericParam->getAs<GenericTypeParamType>(),
typeVar->getAs<TypeVariableType>(), bindingTy);
}();
// The should have reached a type variable and corresponding generic
// parameter.
if (!typeVar || !genericParam)
return std::nullopt;
// Only allow opening the innermost generic parameters.
auto genericContext = callee->getAsGenericContext();
if (!genericContext || !genericContext->isGeneric())
return std::nullopt;
auto genericSig = callee->getInnermostDeclContext()
->getGenericSignatureOfContext().getCanonicalSignature();
if (genericParam->getDepth() < genericSig->getMaxDepth())
return std::nullopt;
// The binding could be an existential metatype. Get the instance type for
// conformance checks and to build an opened existential signature. If the
// instance type is not an existential type, i.e., the metatype is nested,
// bail out.
const Type existentialTy = bindingTy->getMetatypeInstanceType();
if (!existentialTy->isExistentialType())
return std::nullopt;
auto &ctx = callee->getASTContext();
// If the existential argument conforms to all of protocol requirements on
// the formal parameter's type, don't open unless ImplicitOpenExistentials is
// enabled.
// If all of the conformance requirements on the formal parameter's type
// are self-conforming, don't open.
if (!ctx.LangOpts.hasFeature(Feature::ImplicitOpenExistentials)) {
bool containsNonSelfConformance = false;
for (auto proto : genericSig->getRequiredProtocols(genericParam)) {
auto conformance = lookupExistentialConformance(
existentialTy, proto);
if (conformance.isInvalid()) {
containsNonSelfConformance = true;
break;
}
}
if (!containsNonSelfConformance)
return std::nullopt;
}
auto existentialSig = ctx.getOpenedExistentialSignature(existentialTy);
// Ensure that the formal parameter is only used in covariant positions,
// because it won't match anywhere else.
auto referenceInfo = findGenericParameterReferences(
callee, existentialSig.OpenedSig, genericParam,
existentialSig.SelfType->castTo<GenericTypeParamType>(),
/*skipParamIdx=*/paramIdx);
if (referenceInfo.hasNonCovariantRef())
return std::nullopt;
return std::pair(typeVar, bindingTy);
}
/// For each occurrence of a type **type** in `refTy` that satisfies
/// `predicateFn` in covariant position, **type** is erased to an
/// existential using `eraseFn`.
static Type typeEraseExistentialSelfReferences(
Type refTy, TypePosition outermostPosition,
llvm::function_ref<bool(Type)> containsFn,
llvm::function_ref<bool(Type)> predicateFn,
llvm::function_ref<Type(Type, TypePosition)> eraseFn) {
if (!containsFn(refTy))
return refTy;
return refTy.transformWithPosition(
outermostPosition,
[&](TypeBase *t, TypePosition currPos) -> std::optional<Type> {
if (!containsFn(t)) {
return Type(t);
}
if (t->is<MetatypeType>()) {
const auto instanceTy = t->getMetatypeInstanceType();
auto erasedTy = typeEraseExistentialSelfReferences(
instanceTy, currPos,
containsFn, predicateFn, eraseFn);
if (instanceTy.getPointer() == erasedTy.getPointer()) {
return Type(t);
}
// - If the output instance type is an existential, but the input is
// not, wrap the output in an existential metatype.
//
// X.Type → X → any Y → any Y.Type
//
// - Otherwise, both are existential or the output instance type is
// not existential; wrap the output in a singleton metatype.
if (erasedTy->isAnyExistentialType() &&
!erasedTy->isConstraintType() &&
!(instanceTy->isAnyExistentialType() &&
!instanceTy->isConstraintType())) {
return Type(ExistentialMetatypeType::get(erasedTy));
}
return Type(MetatypeType::get(erasedTy));
}
// Opaque types whose substitutions involve this type parameter are
// erased to their upper bound.
if (auto opaque = dyn_cast<OpaqueTypeArchetypeType>(t)) {
for (auto replacementType :
opaque->getSubstitutions().getReplacementTypes()) {
auto erasedReplacementType = typeEraseExistentialSelfReferences(
replacementType, TypePosition::Covariant,
containsFn, predicateFn, eraseFn);
if (erasedReplacementType.getPointer() !=
replacementType.getPointer())
return opaque->getExistentialType();
}
}
// Parameterized protocol types whose arguments involve this type
// parameter are erased to the base type.
if (auto parameterized = dyn_cast<ParameterizedProtocolType>(t)) {
for (auto argType : parameterized->getArgs()) {
auto erasedArgType = typeEraseExistentialSelfReferences(
argType, TypePosition::Covariant,
containsFn, predicateFn, eraseFn);
if (erasedArgType.getPointer() != argType.getPointer())
return parameterized->getBaseType();
}
}
if (!predicateFn(t)) {
// Recurse.
return std::nullopt;
}
auto erasedTy = eraseFn(t, currPos);
if (!erasedTy)
return Type(t);
return erasedTy;
});
}
Type swift::typeEraseOpenedExistentialReference(
Type type, Type existentialBaseType, TypeVariableType *openedTypeVar,
TypePosition outermostPosition) {
auto existentialSig =
type->getASTContext().getOpenedExistentialSignature(
existentialBaseType);
auto applyOuterSubstitutions = [&](Type t) -> Type {
if (t->hasTypeParameter()) {
auto outerSubs = existentialSig.Generalization;
unsigned depth = existentialSig.OpenedSig->getMaxDepth();
OuterSubstitutions replacer{outerSubs, depth};
return t.subst(replacer, replacer);
}
return t;
};
auto erase = [&](Type paramTy, TypePosition currPos) -> Type {
switch (currPos) {
case TypePosition::Covariant:
break;
case TypePosition::Contravariant:
case TypePosition::Invariant:
case TypePosition::Shape:
return Type();
}
// The upper bounds of 'Self' is the existential base type.
if (paramTy->is<GenericTypeParamType>())
return existentialBaseType;
return applyOuterSubstitutions(
existentialSig.OpenedSig->getExistentialType(paramTy));
};
return typeEraseExistentialSelfReferences(
type,
outermostPosition,
/*containsFn=*/[](Type t) {
return t->hasTypeVariable();
},
/*predicateFn=*/[](Type t) {
return t->isTypeVariableOrMember();
},
/*eraseFn=*/[&](Type t, TypePosition currPos) -> Type {
bool found = false;
auto paramTy = t.transformRec([&](Type t) -> std::optional<Type> {
if (t.getPointer() == openedTypeVar) {
found = true;
return existentialSig.SelfType;
}
return std::nullopt;
});
if (!found)
return Type();
assert(paramTy->isTypeParameter());
// This can happen with invalid code.
if (!existentialSig.OpenedSig->isValidTypeParameter(paramTy)) {
return Type(t);
}
// Check if this existential fixes this `Self`-rooted type to something
// in the existential's outer generic signature.
Type reducedTy = existentialSig.OpenedSig.getReducedType(paramTy);
if (!reducedTy->isEqual(paramTy)) {
reducedTy = applyOuterSubstitutions(reducedTy);
auto erasedTy = typeEraseExistentialSelfReferences(
reducedTy, currPos,
[&](Type t) { return t->hasTypeParameter(); },
[&](Type t) { return t->isTypeParameter(); },
[&](Type t, TypePosition currPos) { return erase(t, currPos); });
if (erasedTy.getPointer() == reducedTy.getPointer()) {
return Type(t);
}
return erasedTy;
}
return erase(paramTy, currPos);
});
}
Type swift::typeEraseOpenedArchetypesFromEnvironment(
Type type, GenericEnvironment *env) {
assert(env->getKind() == GenericEnvironment::Kind::OpenedExistential);
return typeEraseExistentialSelfReferences(
type,
TypePosition::Covariant,
/*containsFn=*/[](Type t) {
return t->hasOpenedExistential();
},
/*predicateFn=*/[](Type t) {
return t->is<ExistentialArchetypeType>();
},
/*eraseFn=*/[&](Type t, TypePosition currPos) {
auto *openedTy = t->castTo<ExistentialArchetypeType>();
if (openedTy->getGenericEnvironment() == env)
return openedTy->getExistentialType();
return Type();
});
}