Skip to content

Ncgenerics test fixes kavon v13 #71604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/AST/ASTSynthesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ inline Type synthesizeType(SynthesisContext &SC, Type type) {
enum SingletonTypeSynthesizer {
_any,
_bridgeObject,
_copyable,
_error,
_executor, // the 'BuiltinExecutor' type
_escapable,
_job,
_nativeObject,
_never,
Expand Down Expand Up @@ -77,6 +79,12 @@ inline Type synthesizeType(SynthesisContext &SC,
case _actor:
return SC.Context.getProtocol(KnownProtocolKind::Actor)
->getDeclaredInterfaceType();
case _copyable:
return SC.Context.getProtocol(KnownProtocolKind::Copyable)
->getDeclaredInterfaceType();
case _escapable:
return SC.Context.getProtocol(KnownProtocolKind::Escapable)
->getDeclaredInterfaceType();
}
}

Expand Down
12 changes: 9 additions & 3 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,22 @@ Type ASTBuilder::createSILBoxTypeWithLayout(
ArrayRef<BuiltSubstitution> Substitutions,
ArrayRef<BuiltRequirement> Requirements) {
SmallVector<Type, 4> replacements;
SmallVector<GenericTypeParamType *, 4> genericTypeParams;
SmallVector<GenericTypeParamType *, 2> genericTypeParams;
for (const auto &s : Substitutions) {
if (auto *t = dyn_cast_or_null<GenericTypeParamType>(s.first.getPointer()))
genericTypeParams.push_back(t);
replacements.push_back(s.second);
}

GenericSignature signature;
if (!genericTypeParams.empty())
signature = GenericSignature::get(genericTypeParams, Requirements);
if (!genericTypeParams.empty()) {
SmallVector<BuiltRequirement, 2> RequirementsVec(Requirements);
signature = swift::buildGenericSignature(Ctx,
signature,
genericTypeParams,
std::move(RequirementsVec),
/*allowInverses=*/true);
}
SmallVector<SILField, 4> silFields;
for (auto field: fields)
silFields.emplace_back(field.getPointer()->getCanonicalType(),
Expand Down
93 changes: 66 additions & 27 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ _conformsTo(TypeS type, ProtocolS protocol) {
return {type, protocol};
}

// Convenience macro to say that a type parameter has default
// Copyable & Escapable requirements.
#define _conformsToDefaults(INDEX) \
_conformsTo(_typeparam(INDEX), _copyable), \
_conformsTo(_typeparam(INDEX), _escapable)

/// A synthesizer which generates a layout constraint requirement.
template <class TypeS>
struct LayoutConstraintSynthesizer {
Expand Down Expand Up @@ -288,8 +294,16 @@ struct CollectGenericParams {
void operator()(const ConformsToSynthesizer<TypeS, ProtoS> &conf) {
auto type = synthesizeType(SC, conf.Type);
auto protocolType = synthesizeType(SC, conf.Protocol);
AddedRequirements.push_back({RequirementKind::Conformance,
type, protocolType});
Requirement req = {RequirementKind::Conformance, type, protocolType};

// If it's an invertible protocol and NoncopyableGenerics is disabled
// then skip the requirement.
if (req.getProtocolDecl()->getInvertibleProtocolKind())
if (!(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS ||
SC.Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)))
return;

AddedRequirements.push_back(req);
}

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

// FIXME: Change allowInverses to false and add Copyable/Escapable explicitly
// to those builtins that need it.
return buildGenericSignature(SC.Context,
GenericSignature(),
std::move(collector.GenericParamTypes),
std::move(collector.AddedRequirements),
/*allowInverses=*/true);
/*allowInverses=*/false);
}

/// Build a builtin function declaration.
Expand Down Expand Up @@ -716,9 +728,18 @@ namespace {

template <class G>
void addConformanceRequirement(const G &generator, ProtocolDecl *proto) {
assert(proto && "missing protocol");
Requirement req(RequirementKind::Conformance,
generator.build(*this),
proto->getDeclaredInterfaceType());

// If it's an invertible protocol and NoncopyableGenerics is disabled
// then skip the requirement.
if (req.getProtocolDecl()->getInvertibleProtocolKind())
if (!(SWIFT_ENABLE_EXPERIMENTAL_NONCOPYABLE_GENERICS ||
Context.LangOpts.hasFeature(Feature::NoncopyableGenerics)))
return;

addedRequirements.push_back(req);
}

Expand All @@ -735,13 +756,11 @@ namespace {
}

FuncDecl *build(Identifier name) {
// FIXME: Change allowInverses to false and add Copyable/Escapable
// explicitly to those builtins that need it.
auto GenericSig = buildGenericSignature(
Context, GenericSignature(),
std::move(genericParamTypes),
std::move(addedRequirements),
/*allowInverses=*/true);
/*allowInverses=*/false);
return getBuiltinGenericFunction(name, InterfaceParams,
InterfaceResult,
TheGenericParamList, GenericSig,
Expand Down Expand Up @@ -844,12 +863,21 @@ makePackExpansion(const T &object) {
/// Create a function with type <T> T -> ().
static ValueDecl *getRefCountingOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted,
_conformsTo(_typeparam(0), _copyable)),
_parameters(_typeparam(0)),
_void);
}

static ValueDecl *getLoadOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted,
_conformsTo(_typeparam(0), _copyable)),
_parameters(_rawPointer),
_typeparam(0));
}

static ValueDecl *getTakeOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_parameters(_rawPointer),
Expand All @@ -858,31 +886,33 @@ static ValueDecl *getLoadOperation(ASTContext &ctx, Identifier id) {

static ValueDecl *getStoreOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_owned(_typeparam(0)),
_rawPointer),
_void);
}

static ValueDecl *getDestroyOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_metatype(_typeparam(0)),
_rawPointer),
_void);
}

static ValueDecl *getDestroyArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_metatype(_typeparam(0)),
_rawPointer,
_word),
_void);
}

static ValueDecl *getCopyOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin, _generics(_unrestricted),
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted,
_conformsTo(_typeparam(0), _copyable)),
_parameters(_typeparam(0)), _typeparam(0));
}

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

static ValueDecl *getTransferArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_metatype(_typeparam(0)),
_rawPointer,
_rawPointer,
Expand Down Expand Up @@ -957,7 +987,9 @@ static ValueDecl *getAllocWithTailElemsOperation(ASTContext &Context,
static ValueDecl *getProjectTailElemsOperation(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _unrestricted),
_generics(_unrestricted, _unrestricted,
_conformsToDefaults(0),
_conformsToDefaults(1)),
_parameters(_typeparam(0),
_metatype(_typeparam(1))),
_rawPointer);
Expand All @@ -977,7 +1009,9 @@ static ValueDecl *getGepOperation(ASTContext &ctx, Identifier id,
static ValueDecl *getGetTailAddrOperation(ASTContext &ctx, Identifier id,
Type argType) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _unrestricted),
_generics(_unrestricted, _unrestricted,
_conformsToDefaults(0),
_conformsToDefaults(1)),
_parameters(_rawPointer,
argType,
_metatype(_typeparam(0)),
Expand Down Expand Up @@ -1170,7 +1204,7 @@ static ValueDecl *getNativeObjectCast(ASTContext &Context, Identifier Id,
static ValueDecl *getCastToBridgeObjectOperation(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_owned(_typeparam(0)),
_word),
_bridgeObject);
Expand All @@ -1182,7 +1216,7 @@ static ValueDecl *getCastFromBridgeObjectOperation(ASTContext &ctx,
switch (BV) {
case BuiltinValueKind::CastReferenceFromBridgeObject: {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_owned(_bridgeObject)),
_typeparam(0));
}
Expand Down Expand Up @@ -1212,7 +1246,7 @@ static ValueDecl *getClassifyBridgeObject(ASTContext &C, Identifier Id) {

static ValueDecl *getValueToBridgeObject(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_typeparam(0)),
_bridgeObject);
}
Expand Down Expand Up @@ -1498,7 +1532,7 @@ static ValueDecl *getCreateAsyncTask(ASTContext &ctx, Identifier id,

static ValueDecl *getTaskRunInline(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(
ctx, id, _thin, _generics(_unrestricted),
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
_parameters(
_function(_async(_noescape(_thick)), _typeparam(0), _parameters())),
_typeparam(0));
Expand All @@ -1521,15 +1555,15 @@ static ValueDecl *getDefaultActorInitDestroy(ASTContext &ctx,
static ValueDecl *getDistributedActorInitializeRemote(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted), // TODO(distributed): restrict to DistributedActor
_generics(_unrestricted, _conformsToDefaults(0)), // TODO(distributed): restrict to DistributedActor
_parameters(_metatype(_typeparam(0))),
_rawPointer);
}

static ValueDecl *getResumeContinuationReturning(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_rawUnsafeContinuation,
_owned(_typeparam(0))),
_void);
Expand Down Expand Up @@ -1575,7 +1609,7 @@ static ValueDecl *getEndAsyncLet(ASTContext &ctx, Identifier id) {

static ValueDecl *getCreateTaskGroup(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_metatype(_typeparam(0))),
_rawPointer);
}
Expand Down Expand Up @@ -1613,14 +1647,15 @@ static ValueDecl *getBuildDefaultActorExecutorRef(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted,
_conformsToDefaults(0),
_layout(_typeparam(0), _classLayout())),
_parameters(_typeparam(0)),
_executor);
}

static ValueDecl *getExtractFunctionIsolation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted),
_generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_typeparam(0)),
_optional(_existential(_actor)));
}
Expand Down Expand Up @@ -1661,7 +1696,7 @@ static ValueDecl *getBuildComplexEqualitySerialExecutorRef(ASTContext &ctx,
static ValueDecl *getAutoDiffCreateLinearMapContext(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(
ctx, id, _thin, _generics(_unrestricted),
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_metatype(_typeparam(0))), _nativeObject);
}

Expand All @@ -1674,7 +1709,7 @@ static ValueDecl *getAutoDiffProjectTopLevelSubcontext(ASTContext &ctx,
static ValueDecl *getAutoDiffAllocateSubcontext(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(
ctx, id, _thin, _generics(_unrestricted),
ctx, id, _thin, _generics(_unrestricted, _conformsToDefaults(0)),
_parameters(_nativeObject, _metatype(_typeparam(0))), _rawPointer);
}

Expand Down Expand Up @@ -2729,9 +2764,13 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
case BuiltinValueKind::Load:
case BuiltinValueKind::LoadRaw:
case BuiltinValueKind::LoadInvariant:
case BuiltinValueKind::Take:
if (!Types.empty()) return nullptr;
return getLoadOperation(Context, Id);

case BuiltinValueKind::Take:
if (!Types.empty()) return nullptr;
return getTakeOperation(Context, Id);


case BuiltinValueKind::Destroy:
if (!Types.empty()) return nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ AbstractGenericSignatureRequest::evaluate(

// If there are no added requirements, we can form the signature directly
// with the added parameters.
if (addedRequirements.empty()) {
if (addedRequirements.empty() && !allowInverses) {
auto result = GenericSignature::get(genericParams,
baseSignature.getRequirements());
return GenericSignatureWithError(result, GenericSignatureErrors());
Expand Down