Skip to content

Commit 8c0b056

Browse files
authored
Merge pull request #71604 from kavon/ncgenerics-test-fixes-kavon-v13
Ncgenerics test fixes kavon v13
2 parents de885df + d7a19f5 commit 8c0b056

File tree

4 files changed

+84
-31
lines changed

4 files changed

+84
-31
lines changed

include/swift/AST/ASTSynthesis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ inline Type synthesizeType(SynthesisContext &SC, Type type) {
4040
enum SingletonTypeSynthesizer {
4141
_any,
4242
_bridgeObject,
43+
_copyable,
4344
_error,
4445
_executor, // the 'BuiltinExecutor' type
46+
_escapable,
4547
_job,
4648
_nativeObject,
4749
_never,
@@ -77,6 +79,12 @@ inline Type synthesizeType(SynthesisContext &SC,
7779
case _actor:
7880
return SC.Context.getProtocol(KnownProtocolKind::Actor)
7981
->getDeclaredInterfaceType();
82+
case _copyable:
83+
return SC.Context.getProtocol(KnownProtocolKind::Copyable)
84+
->getDeclaredInterfaceType();
85+
case _escapable:
86+
return SC.Context.getProtocol(KnownProtocolKind::Escapable)
87+
->getDeclaredInterfaceType();
8088
}
8189
}
8290

lib/AST/ASTDemangler.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,16 +842,22 @@ Type ASTBuilder::createSILBoxTypeWithLayout(
842842
ArrayRef<BuiltSubstitution> Substitutions,
843843
ArrayRef<BuiltRequirement> Requirements) {
844844
SmallVector<Type, 4> replacements;
845-
SmallVector<GenericTypeParamType *, 4> genericTypeParams;
845+
SmallVector<GenericTypeParamType *, 2> genericTypeParams;
846846
for (const auto &s : Substitutions) {
847847
if (auto *t = dyn_cast_or_null<GenericTypeParamType>(s.first.getPointer()))
848848
genericTypeParams.push_back(t);
849849
replacements.push_back(s.second);
850850
}
851851

852852
GenericSignature signature;
853-
if (!genericTypeParams.empty())
854-
signature = GenericSignature::get(genericTypeParams, Requirements);
853+
if (!genericTypeParams.empty()) {
854+
SmallVector<BuiltRequirement, 2> RequirementsVec(Requirements);
855+
signature = swift::buildGenericSignature(Ctx,
856+
signature,
857+
genericTypeParams,
858+
std::move(RequirementsVec),
859+
/*allowInverses=*/true);
860+
}
855861
SmallVector<SILField, 4> silFields;
856862
for (auto field: fields)
857863
silFields.emplace_back(field.getPointer()->getCanonicalType(),

lib/AST/Builtins.cpp

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ _conformsTo(TypeS type, ProtocolS protocol) {
177177
return {type, protocol};
178178
}
179179

180+
// Convenience macro to say that a type parameter has default
181+
// Copyable & Escapable requirements.
182+
#define _conformsToDefaults(INDEX) \
183+
_conformsTo(_typeparam(INDEX), _copyable), \
184+
_conformsTo(_typeparam(INDEX), _escapable)
185+
180186
/// A synthesizer which generates a layout constraint requirement.
181187
template <class TypeS>
182188
struct LayoutConstraintSynthesizer {
@@ -288,8 +294,16 @@ struct CollectGenericParams {
288294
void operator()(const ConformsToSynthesizer<TypeS, ProtoS> &conf) {
289295
auto type = synthesizeType(SC, conf.Type);
290296
auto protocolType = synthesizeType(SC, conf.Protocol);
291-
AddedRequirements.push_back({RequirementKind::Conformance,
292-
type, protocolType});
297+
Requirement req = {RequirementKind::Conformance, type, protocolType};
298+
299+
// If it's an invertible protocol and NoncopyableGenerics is disabled
300+
// then skip the requirement.
301+
if (req.getProtocolDecl()->getInvertibleProtocolKind())
302+
if (!(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS ||
303+
SC.Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)))
304+
return;
305+
306+
AddedRequirements.push_back(req);
293307
}
294308

295309
template <class TypeS>
@@ -310,13 +324,11 @@ synthesizeGenericSignature(SynthesisContext &SC,
310324
CollectGenericParams collector(SC);
311325
list.Params.visit(collector);
312326

313-
// FIXME: Change allowInverses to false and add Copyable/Escapable explicitly
314-
// to those builtins that need it.
315327
return buildGenericSignature(SC.Context,
316328
GenericSignature(),
317329
std::move(collector.GenericParamTypes),
318330
std::move(collector.AddedRequirements),
319-
/*allowInverses=*/true);
331+
/*allowInverses=*/false);
320332
}
321333

322334
/// Build a builtin function declaration.
@@ -716,9 +728,18 @@ namespace {
716728

717729
template <class G>
718730
void addConformanceRequirement(const G &generator, ProtocolDecl *proto) {
731+
assert(proto && "missing protocol");
719732
Requirement req(RequirementKind::Conformance,
720733
generator.build(*this),
721734
proto->getDeclaredInterfaceType());
735+
736+
// If it's an invertible protocol and NoncopyableGenerics is disabled
737+
// then skip the requirement.
738+
if (req.getProtocolDecl()->getInvertibleProtocolKind())
739+
if (!(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS ||
740+
Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)))
741+
return;
742+
722743
addedRequirements.push_back(req);
723744
}
724745

@@ -735,13 +756,11 @@ namespace {
735756
}
736757

737758
FuncDecl *build(Identifier name) {
738-
// FIXME: Change allowInverses to false and add Copyable/Escapable
739-
// explicitly to those builtins that need it.
740759
auto GenericSig = buildGenericSignature(
741760
Context, GenericSignature(),
742761
std::move(genericParamTypes),
743762
std::move(addedRequirements),
744-
/*allowInverses=*/true);
763+
/*allowInverses=*/false);
745764
return getBuiltinGenericFunction(name, InterfaceParams,
746765
InterfaceResult,
747766
TheGenericParamList, GenericSig,
@@ -844,12 +863,21 @@ makePackExpansion(const T &object) {
844863
/// Create a function with type <T> T -> ().
845864
static ValueDecl *getRefCountingOperation(ASTContext &ctx, Identifier id) {
846865
return getBuiltinFunction(ctx, id, _thin,
847-
_generics(_unrestricted),
866+
_generics(_unrestricted,
867+
_conformsTo(_typeparam(0), _copyable)),
848868
_parameters(_typeparam(0)),
849869
_void);
850870
}
851871

852872
static ValueDecl *getLoadOperation(ASTContext &ctx, Identifier id) {
873+
return getBuiltinFunction(ctx, id, _thin,
874+
_generics(_unrestricted,
875+
_conformsTo(_typeparam(0), _copyable)),
876+
_parameters(_rawPointer),
877+
_typeparam(0));
878+
}
879+
880+
static ValueDecl *getTakeOperation(ASTContext &ctx, Identifier id) {
853881
return getBuiltinFunction(ctx, id, _thin,
854882
_generics(_unrestricted),
855883
_parameters(_rawPointer),
@@ -858,31 +886,33 @@ static ValueDecl *getLoadOperation(ASTContext &ctx, Identifier id) {
858886

859887
static ValueDecl *getStoreOperation(ASTContext &ctx, Identifier id) {
860888
return getBuiltinFunction(ctx, id, _thin,
861-
_generics(_unrestricted),
889+
_generics(_unrestricted, _conformsToDefaults(0)),
862890
_parameters(_owned(_typeparam(0)),
863891
_rawPointer),
864892
_void);
865893
}
866894

867895
static ValueDecl *getDestroyOperation(ASTContext &ctx, Identifier id) {
868896
return getBuiltinFunction(ctx, id, _thin,
869-
_generics(_unrestricted),
897+
_generics(_unrestricted, _conformsToDefaults(0)),
870898
_parameters(_metatype(_typeparam(0)),
871899
_rawPointer),
872900
_void);
873901
}
874902

875903
static ValueDecl *getDestroyArrayOperation(ASTContext &ctx, Identifier id) {
876904
return getBuiltinFunction(ctx, id, _thin,
877-
_generics(_unrestricted),
905+
_generics(_unrestricted, _conformsToDefaults(0)),
878906
_parameters(_metatype(_typeparam(0)),
879907
_rawPointer,
880908
_word),
881909
_void);
882910
}
883911

884912
static ValueDecl *getCopyOperation(ASTContext &ctx, Identifier id) {
885-
return getBuiltinFunction(ctx, id, _thin, _generics(_unrestricted),
913+
return getBuiltinFunction(ctx, id, _thin,
914+
_generics(_unrestricted,
915+
_conformsTo(_typeparam(0), _copyable)),
886916
_parameters(_typeparam(0)), _typeparam(0));
887917
}
888918

@@ -894,7 +924,7 @@ static ValueDecl *getAssumeAlignment(ASTContext &ctx, Identifier id) {
894924

895925
static ValueDecl *getTransferArrayOperation(ASTContext &ctx, Identifier id) {
896926
return getBuiltinFunction(ctx, id, _thin,
897-
_generics(_unrestricted),
927+
_generics(_unrestricted, _conformsToDefaults(0)),
898928
_parameters(_metatype(_typeparam(0)),
899929
_rawPointer,
900930
_rawPointer,
@@ -957,7 +987,9 @@ static ValueDecl *getAllocWithTailElemsOperation(ASTContext &Context,
957987
static ValueDecl *getProjectTailElemsOperation(ASTContext &ctx,
958988
Identifier id) {
959989
return getBuiltinFunction(ctx, id, _thin,
960-
_generics(_unrestricted, _unrestricted),
990+
_generics(_unrestricted, _unrestricted,
991+
_conformsToDefaults(0),
992+
_conformsToDefaults(1)),
961993
_parameters(_typeparam(0),
962994
_metatype(_typeparam(1))),
963995
_rawPointer);
@@ -977,7 +1009,9 @@ static ValueDecl *getGepOperation(ASTContext &ctx, Identifier id,
9771009
static ValueDecl *getGetTailAddrOperation(ASTContext &ctx, Identifier id,
9781010
Type argType) {
9791011
return getBuiltinFunction(ctx, id, _thin,
980-
_generics(_unrestricted, _unrestricted),
1012+
_generics(_unrestricted, _unrestricted,
1013+
_conformsToDefaults(0),
1014+
_conformsToDefaults(1)),
9811015
_parameters(_rawPointer,
9821016
argType,
9831017
_metatype(_typeparam(0)),
@@ -1170,7 +1204,7 @@ static ValueDecl *getNativeObjectCast(ASTContext &Context, Identifier Id,
11701204
static ValueDecl *getCastToBridgeObjectOperation(ASTContext &ctx,
11711205
Identifier id) {
11721206
return getBuiltinFunction(ctx, id, _thin,
1173-
_generics(_unrestricted),
1207+
_generics(_unrestricted, _conformsToDefaults(0)),
11741208
_parameters(_owned(_typeparam(0)),
11751209
_word),
11761210
_bridgeObject);
@@ -1182,7 +1216,7 @@ static ValueDecl *getCastFromBridgeObjectOperation(ASTContext &ctx,
11821216
switch (BV) {
11831217
case BuiltinValueKind::CastReferenceFromBridgeObject: {
11841218
return getBuiltinFunction(ctx, id, _thin,
1185-
_generics(_unrestricted),
1219+
_generics(_unrestricted, _conformsToDefaults(0)),
11861220
_parameters(_owned(_bridgeObject)),
11871221
_typeparam(0));
11881222
}
@@ -1212,7 +1246,7 @@ static ValueDecl *getClassifyBridgeObject(ASTContext &C, Identifier Id) {
12121246

12131247
static ValueDecl *getValueToBridgeObject(ASTContext &ctx, Identifier id) {
12141248
return getBuiltinFunction(ctx, id, _thin,
1215-
_generics(_unrestricted),
1249+
_generics(_unrestricted, _conformsToDefaults(0)),
12161250
_parameters(_typeparam(0)),
12171251
_bridgeObject);
12181252
}
@@ -1498,7 +1532,7 @@ static ValueDecl *getCreateAsyncTask(ASTContext &ctx, Identifier id,
14981532

14991533
static ValueDecl *getTaskRunInline(ASTContext &ctx, Identifier id) {
15001534
return getBuiltinFunction(
1501-
ctx, id, _thin, _generics(_unrestricted),
1535+
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
15021536
_parameters(
15031537
_function(_async(_noescape(_thick)), _typeparam(0), _parameters())),
15041538
_typeparam(0));
@@ -1521,15 +1555,15 @@ static ValueDecl *getDefaultActorInitDestroy(ASTContext &ctx,
15211555
static ValueDecl *getDistributedActorInitializeRemote(ASTContext &ctx,
15221556
Identifier id) {
15231557
return getBuiltinFunction(ctx, id, _thin,
1524-
_generics(_unrestricted), // TODO(distributed): restrict to DistributedActor
1558+
_generics(_unrestricted, _conformsToDefaults(0)), // TODO(distributed): restrict to DistributedActor
15251559
_parameters(_metatype(_typeparam(0))),
15261560
_rawPointer);
15271561
}
15281562

15291563
static ValueDecl *getResumeContinuationReturning(ASTContext &ctx,
15301564
Identifier id) {
15311565
return getBuiltinFunction(ctx, id, _thin,
1532-
_generics(_unrestricted),
1566+
_generics(_unrestricted, _conformsToDefaults(0)),
15331567
_parameters(_rawUnsafeContinuation,
15341568
_owned(_typeparam(0))),
15351569
_void);
@@ -1575,7 +1609,7 @@ static ValueDecl *getEndAsyncLet(ASTContext &ctx, Identifier id) {
15751609

15761610
static ValueDecl *getCreateTaskGroup(ASTContext &ctx, Identifier id) {
15771611
return getBuiltinFunction(ctx, id, _thin,
1578-
_generics(_unrestricted),
1612+
_generics(_unrestricted, _conformsToDefaults(0)),
15791613
_parameters(_metatype(_typeparam(0))),
15801614
_rawPointer);
15811615
}
@@ -1613,14 +1647,15 @@ static ValueDecl *getBuildDefaultActorExecutorRef(ASTContext &ctx,
16131647
Identifier id) {
16141648
return getBuiltinFunction(ctx, id, _thin,
16151649
_generics(_unrestricted,
1650+
_conformsToDefaults(0),
16161651
_layout(_typeparam(0), _classLayout())),
16171652
_parameters(_typeparam(0)),
16181653
_executor);
16191654
}
16201655

16211656
static ValueDecl *getExtractFunctionIsolation(ASTContext &ctx, Identifier id) {
16221657
return getBuiltinFunction(ctx, id, _thin,
1623-
_generics(_unrestricted),
1658+
_generics(_unrestricted, _conformsToDefaults(0)),
16241659
_parameters(_typeparam(0)),
16251660
_optional(_existential(_actor)));
16261661
}
@@ -1661,7 +1696,7 @@ static ValueDecl *getBuildComplexEqualitySerialExecutorRef(ASTContext &ctx,
16611696
static ValueDecl *getAutoDiffCreateLinearMapContext(ASTContext &ctx,
16621697
Identifier id) {
16631698
return getBuiltinFunction(
1664-
ctx, id, _thin, _generics(_unrestricted),
1699+
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
16651700
_parameters(_metatype(_typeparam(0))), _nativeObject);
16661701
}
16671702

@@ -1674,7 +1709,7 @@ static ValueDecl *getAutoDiffProjectTopLevelSubcontext(ASTContext &ctx,
16741709
static ValueDecl *getAutoDiffAllocateSubcontext(ASTContext &ctx,
16751710
Identifier id) {
16761711
return getBuiltinFunction(
1677-
ctx, id, _thin, _generics(_unrestricted),
1712+
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
16781713
_parameters(_nativeObject, _metatype(_typeparam(0))), _rawPointer);
16791714
}
16801715

@@ -2729,9 +2764,13 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
27292764
case BuiltinValueKind::Load:
27302765
case BuiltinValueKind::LoadRaw:
27312766
case BuiltinValueKind::LoadInvariant:
2732-
case BuiltinValueKind::Take:
27332767
if (!Types.empty()) return nullptr;
27342768
return getLoadOperation(Context, Id);
2769+
2770+
case BuiltinValueKind::Take:
2771+
if (!Types.empty()) return nullptr;
2772+
return getTakeOperation(Context, Id);
2773+
27352774

27362775
case BuiltinValueKind::Destroy:
27372776
if (!Types.empty()) return nullptr;

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ AbstractGenericSignatureRequest::evaluate(
565565

566566
// If there are no added requirements, we can form the signature directly
567567
// with the added parameters.
568-
if (addedRequirements.empty()) {
568+
if (addedRequirements.empty() && !allowInverses) {
569569
auto result = GenericSignature::get(genericParams,
570570
baseSignature.getRequirements());
571571
return GenericSignatureWithError(result, GenericSignatureErrors());

0 commit comments

Comments
 (0)