Skip to content

Commit d46bd33

Browse files
authored
Merge pull request #71371 from slavapestov/ncgenerics-fixes-3
Non-copyable generics fixes, part 3
2 parents 5acd366 + 9f0bf00 commit d46bd33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+433
-401
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ namespace swift {
105105
Type getType() const { return t; }
106106
};
107107

108+
struct WitnessType {
109+
Type t;
110+
111+
WitnessType(Type t) : t(t) {}
112+
113+
Type getType() { return t; }
114+
};
115+
108116
/// Describes the kind of diagnostic argument we're storing.
109117
///
110118
enum class DiagnosticArgumentKind {
@@ -117,6 +125,7 @@ namespace swift {
117125
Type,
118126
TypeRepr,
119127
FullyQualifiedType,
128+
WitnessType,
120129
DescriptivePatternKind,
121130
SelfAccessKind,
122131
ReferenceOwnership,
@@ -151,6 +160,7 @@ namespace swift {
151160
Type TypeVal;
152161
TypeRepr *TyR;
153162
FullyQualified<Type> FullyQualifiedTypeVal;
163+
WitnessType WitnessTypeVal;
154164
DescriptivePatternKind DescriptivePatternKindVal;
155165
SelfAccessKind SelfAccessKindVal;
156166
ReferenceOwnership ReferenceOwnershipVal;
@@ -214,6 +224,10 @@ namespace swift {
214224
: Kind(DiagnosticArgumentKind::FullyQualifiedType),
215225
FullyQualifiedTypeVal(FQT) {}
216226

227+
DiagnosticArgument(WitnessType WT)
228+
: Kind(DiagnosticArgumentKind::WitnessType),
229+
WitnessTypeVal(WT) {}
230+
217231
DiagnosticArgument(const TypeLoc &TL) {
218232
if (TypeRepr *tyR = TL.getTypeRepr()) {
219233
Kind = DiagnosticArgumentKind::TypeRepr;
@@ -329,6 +343,11 @@ namespace swift {
329343
return FullyQualifiedTypeVal;
330344
}
331345

346+
WitnessType getAsWitnessType() const {
347+
assert(Kind == DiagnosticArgumentKind::WitnessType);
348+
return WitnessTypeVal;
349+
}
350+
332351
DescriptivePatternKind getAsDescriptivePatternKind() const {
333352
assert(Kind == DiagnosticArgumentKind::DescriptivePatternKind);
334353
return DescriptivePatternKindVal;

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,19 +2883,19 @@ NOTE(no_witnesses,none,
28832883
"protocol requires "
28842884
"%select{initializer %1|function %1|property %1|subscript}0 with type %2"
28852885
"%select{|; add a stub for conformance}3",
2886-
(RequirementKind, const ValueDecl *, Type, bool))
2886+
(RequirementKind, const ValueDecl *, WitnessType, bool))
28872887
NOTE(missing_witnesses_general,none, "add stubs for conformance",
28882888
())
28892889
NOTE(ambiguous_witnesses,none,
28902890
"multiple matching "
28912891
"%select{initializers named %1|functions named %1|properties named %1|"
28922892
"subscript operators}0 with type %2",
2893-
(RequirementKind, const ValueDecl *, Type))
2893+
(RequirementKind, const ValueDecl *, WitnessType))
28942894
NOTE(ambiguous_witnesses_wrong_name,none,
28952895
"multiple matching "
28962896
"%select{initializers named %1|functions named %1|properties named %1|"
28972897
"subscript operators}0 with type %2",
2898-
(RequirementKind, const ValueDecl *, Type))
2898+
(RequirementKind, const ValueDecl *, WitnessType))
28992899
NOTE(no_witnesses_type,none,
29002900
"protocol requires nested type %0; add nested type %0 for conformance",
29012901
(const AssociatedTypeDecl *))
@@ -2982,7 +2982,7 @@ NOTE(protocol_witness_kind_conflict,none,
29822982
"candidate is not %select{an initializer|a function|a variable|"
29832983
"a subscript}0", (RequirementKind))
29842984
NOTE(protocol_witness_type_conflict,none,
2985-
"candidate has non-matching type %0%1", (Type, StringRef))
2985+
"candidate has non-matching type %0%1", (WitnessType, StringRef))
29862986
NOTE(protocol_witness_missing_requirement,none,
29872987
"candidate would match if %0 %select{conformed to|subclassed|"
29882988
"was the same type as|%error|%error}2 %1", (Type, Type, unsigned))

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ GenericSignature buildGenericSignature(
572572
ASTContext &ctx,
573573
GenericSignature baseSignature,
574574
SmallVector<GenericTypeParamType *, 2> addedParameters,
575-
SmallVector<Requirement, 2> addedRequirements);
575+
SmallVector<Requirement, 2> addedRequirements,
576+
bool allowInverses);
576577

577578
/// Summary of error conditions detected by the Requirement Machine.
578579
enum class GenericSignatureErrorFlags {

include/swift/AST/TypeCheckRequests.h

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -475,43 +475,6 @@ class InvertibleAnnotationRequest
475475
bool isCached() const { return true; }
476476
};
477477

478-
/// Determine whether the given type is Escapable.
479-
class IsEscapableRequest
480-
: public SimpleRequest<IsEscapableRequest, bool(CanType),
481-
RequestFlags::Cached> {
482-
public:
483-
using SimpleRequest::SimpleRequest;
484-
485-
private:
486-
friend SimpleRequest;
487-
488-
// Evaluation.
489-
bool evaluate(Evaluator &evaluator, CanType) const;
490-
491-
public:
492-
// Caching.
493-
bool isCached() const { return true; }
494-
};
495-
496-
/// Determine whether the given type is noncopyable. Assumes type parameters
497-
/// have become archetypes.
498-
class IsNoncopyableRequest
499-
: public SimpleRequest<IsNoncopyableRequest, bool(CanType),
500-
RequestFlags::Cached> {
501-
public:
502-
using SimpleRequest::SimpleRequest;
503-
504-
private:
505-
friend SimpleRequest;
506-
507-
// Evaluation.
508-
bool evaluate(Evaluator &evaluator, CanType type) const;
509-
510-
public:
511-
// Caching.
512-
bool isCached() const { return true; }
513-
};
514-
515478
/// Determine whether the given declaration is 'dynamic''.
516479
class IsDynamicRequest :
517480
public SimpleRequest<IsDynamicRequest,
@@ -2012,7 +1975,8 @@ class AbstractGenericSignatureRequest :
20121975
public SimpleRequest<AbstractGenericSignatureRequest,
20131976
GenericSignatureWithError (const GenericSignatureImpl *,
20141977
SmallVector<GenericTypeParamType *, 2>,
2015-
SmallVector<Requirement, 2>),
1978+
SmallVector<Requirement, 2>,
1979+
bool),
20161980
RequestFlags::Cached> {
20171981
public:
20181982
using SimpleRequest::SimpleRequest;
@@ -2025,7 +1989,8 @@ class AbstractGenericSignatureRequest :
20251989
evaluate(Evaluator &evaluator,
20261990
const GenericSignatureImpl *baseSignature,
20271991
SmallVector<GenericTypeParamType *, 2> addedParameters,
2028-
SmallVector<Requirement, 2> addedRequirements) const;
1992+
SmallVector<Requirement, 2> addedRequirements,
1993+
bool allowInverses) const;
20291994

20301995
public:
20311996
// Separate caching.
@@ -2044,7 +2009,7 @@ class InferredGenericSignatureRequest :
20442009
WhereClauseOwner,
20452010
SmallVector<Requirement, 2>,
20462011
SmallVector<TypeLoc, 2>,
2047-
bool),
2012+
bool, bool),
20482013
RequestFlags::Cached> {
20492014
public:
20502015
using SimpleRequest::SimpleRequest;
@@ -2060,7 +2025,7 @@ class InferredGenericSignatureRequest :
20602025
WhereClauseOwner whereClause,
20612026
SmallVector<Requirement, 2> addedRequirements,
20622027
SmallVector<TypeLoc, 2> inferenceSources,
2063-
bool allowConcreteGenericParams) const;
2028+
bool isExtension, bool allowInverses) const;
20642029

20652030
public:
20662031
// Separate caching.

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
SWIFT_REQUEST(TypeChecker, AbstractGenericSignatureRequest,
1919
GenericSignatureWithError (const GenericSignatureImpl *,
2020
SmallVector<GenericTypeParamType *, 2>,
21-
SmallVector<Requirement, 2>),
21+
SmallVector<Requirement, 2>,
22+
bool),
2223
Cached, NoLocationInfo)
2324
SWIFT_REQUEST(TypeChecker, ApplyAccessNoteRequest,
2425
evaluator::SideEffect(ValueDecl *), Cached, NoLocationInfo)
@@ -196,7 +197,7 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
196197
GenericParamList *,
197198
WhereClauseOwner,
198199
SmallVector<Requirement, 2>,
199-
SmallVector<TypeLoc, 2>, bool),
200+
SmallVector<TypeLoc, 2>, bool, bool),
200201
Cached, NoLocationInfo)
201202
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
202203
bool(ModuleDecl *), Cached, NoLocationInfo)
@@ -222,12 +223,6 @@ SWIFT_REQUEST(TypeChecker, IsFinalRequest, bool(ValueDecl *), SeparatelyCached,
222223
SWIFT_REQUEST(TypeChecker, InvertibleAnnotationRequest,
223224
InverseMarking(TypeDecl *, InvertibleProtocolKind),
224225
Cached, NoLocationInfo)
225-
SWIFT_REQUEST(TypeChecker, IsNoncopyableRequest,
226-
bool (CanType),
227-
Cached, NoLocationInfo)
228-
SWIFT_REQUEST(TypeChecker, IsEscapableRequest,
229-
bool (CanType),
230-
Cached, NoLocationInfo)
231226
SWIFT_REQUEST(TypeChecker, IsGetterMutatingRequest, bool(AbstractStorageDecl *),
232227
SeparatelyCached, NoLocationInfo)
233228
SWIFT_REQUEST(TypeChecker, IsImplicitlyUnwrappedOptionalRequest,

include/swift/Subsystems.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ namespace swift {
190190

191191
/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
192192
GenericSignature handleSILGenericParams(GenericParamList *genericParams,
193-
DeclContext *DC);
193+
DeclContext *DC,
194+
bool allowInverses=true);
194195

195196
/// Turn the given module into SIL IR.
196197
///

lib/AST/ASTContext.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,7 +5973,8 @@ ASTContext::getOpenedExistentialSignature(Type type, GenericSignature parentSig)
59735973
constraint);
59745974
auto genericSig = buildGenericSignature(
59755975
*this, canParentSig,
5976-
{genericParam}, {requirement});
5976+
{genericParam}, {requirement},
5977+
/*allowInverses=*/true);
59775978

59785979
CanGenericSignature canGenericSig(genericSig);
59795980

@@ -6085,8 +6086,8 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
60856086
}
60866087

60876088
auto elementSig = buildGenericSignature(
6088-
*this, GenericSignature(), genericParams, requirements)
6089-
.getCanonicalSignature();
6089+
*this, GenericSignature(), genericParams, requirements,
6090+
/*allowInverses=*/false).getCanonicalSignature();
60906091
sigs[key] = elementSig;
60916092
return elementSig;
60926093
}
@@ -6159,7 +6160,8 @@ ASTContext::getOverrideGenericSignature(const NominalTypeDecl *baseNominal,
61596160

61606161
auto genericSig = buildGenericSignature(*this, derivedNominalSig,
61616162
std::move(addedGenericParams),
6162-
std::move(addedRequirements));
6163+
std::move(addedRequirements),
6164+
/*allowInverses=*/false);
61636165
getImpl().overrideSigCache.insert(std::make_pair(key, genericSig));
61646166
return genericSig;
61656167
}

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,8 @@ CanGenericSignature ASTBuilder::demangleGenericSignature(
10931093
decodeRequirement<BuiltType, BuiltRequirement, BuiltLayoutConstraint,
10941094
ASTBuilder>(node, requirements, *this);
10951095

1096-
return buildGenericSignature(Ctx, baseGenericSig, {}, std::move(requirements))
1096+
return buildGenericSignature(Ctx, baseGenericSig, {}, std::move(requirements),
1097+
/*allowInverses=*/true)
10971098
.getCanonicalSignature();
10981099
}
10991100

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7546,11 +7546,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
75467546

75477547
GenericSignature sig = substitutions.getGenericSignature();
75487548

7549+
// The substituted signature is printed without inverse requirement
7550+
// desugaring, but also we drop conformances to Copyable and Escapable
7551+
// when constructing it.
75497552
sub->Printer << "@substituted ";
75507553
sub->printGenericSignature(sig,
75517554
PrintAST::PrintParams |
7552-
PrintAST::PrintRequirements |
7553-
PrintAST::PrintInverseRequirements);
7555+
PrintAST::PrintRequirements);
75547556
sub->Printer << " ";
75557557
}
75567558

lib/AST/AutoDiff.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ GenericSignature autodiff::getConstrainedDerivativeGenericSignature(
374374

375375
return buildGenericSignature(ctx, derivativeGenSig,
376376
/*addedGenericParams*/ {},
377-
std::move(requirements));
377+
std::move(requirements),
378+
/*allowInverses=*/true);
378379
}
379380

380381
// Given the rest of a `Builtin.applyDerivative_{jvp|vjp}` or

lib/AST/Builtins.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,13 @@ synthesizeGenericSignature(SynthesisContext &SC,
310310
CollectGenericParams collector(SC);
311311
list.Params.visit(collector);
312312

313+
// FIXME: Change allowInverses to false and add Copyable/Escapable explicitly
314+
// to those builtins that need it.
313315
return buildGenericSignature(SC.Context,
314316
GenericSignature(),
315317
std::move(collector.GenericParamTypes),
316-
std::move(collector.AddedRequirements));
318+
std::move(collector.AddedRequirements),
319+
/*allowInverses=*/true);
317320
}
318321

319322
/// Build a builtin function declaration.
@@ -732,10 +735,13 @@ namespace {
732735
}
733736

734737
FuncDecl *build(Identifier name) {
738+
// FIXME: Change allowInverses to false and add Copyable/Escapable
739+
// explicitly to those builtins that need it.
735740
auto GenericSig = buildGenericSignature(
736741
Context, GenericSignature(),
737742
std::move(genericParamTypes),
738-
std::move(addedRequirements));
743+
std::move(addedRequirements),
744+
/*allowInverses=*/true);
739745
return getBuiltinGenericFunction(name, InterfaceParams,
740746
InterfaceResult,
741747
TheGenericParamList, GenericSig,

0 commit comments

Comments
 (0)