Skip to content

Non-copyable generics fixes, part 3 #71371

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 13 commits into from
Feb 6, 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
19 changes: 19 additions & 0 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ namespace swift {
Type getType() const { return t; }
};

struct WitnessType {
Type t;

WitnessType(Type t) : t(t) {}

Type getType() { return t; }
};

/// Describes the kind of diagnostic argument we're storing.
///
enum class DiagnosticArgumentKind {
Expand All @@ -117,6 +125,7 @@ namespace swift {
Type,
TypeRepr,
FullyQualifiedType,
WitnessType,
DescriptivePatternKind,
SelfAccessKind,
ReferenceOwnership,
Expand Down Expand Up @@ -151,6 +160,7 @@ namespace swift {
Type TypeVal;
TypeRepr *TyR;
FullyQualified<Type> FullyQualifiedTypeVal;
WitnessType WitnessTypeVal;
DescriptivePatternKind DescriptivePatternKindVal;
SelfAccessKind SelfAccessKindVal;
ReferenceOwnership ReferenceOwnershipVal;
Expand Down Expand Up @@ -214,6 +224,10 @@ namespace swift {
: Kind(DiagnosticArgumentKind::FullyQualifiedType),
FullyQualifiedTypeVal(FQT) {}

DiagnosticArgument(WitnessType WT)
: Kind(DiagnosticArgumentKind::WitnessType),
WitnessTypeVal(WT) {}

DiagnosticArgument(const TypeLoc &TL) {
if (TypeRepr *tyR = TL.getTypeRepr()) {
Kind = DiagnosticArgumentKind::TypeRepr;
Expand Down Expand Up @@ -329,6 +343,11 @@ namespace swift {
return FullyQualifiedTypeVal;
}

WitnessType getAsWitnessType() const {
assert(Kind == DiagnosticArgumentKind::WitnessType);
return WitnessTypeVal;
}

DescriptivePatternKind getAsDescriptivePatternKind() const {
assert(Kind == DiagnosticArgumentKind::DescriptivePatternKind);
return DescriptivePatternKindVal;
Expand Down
8 changes: 4 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2883,19 +2883,19 @@ NOTE(no_witnesses,none,
"protocol requires "
"%select{initializer %1|function %1|property %1|subscript}0 with type %2"
"%select{|; add a stub for conformance}3",
(RequirementKind, const ValueDecl *, Type, bool))
(RequirementKind, const ValueDecl *, WitnessType, bool))
NOTE(missing_witnesses_general,none, "add stubs for conformance",
())
NOTE(ambiguous_witnesses,none,
"multiple matching "
"%select{initializers named %1|functions named %1|properties named %1|"
"subscript operators}0 with type %2",
(RequirementKind, const ValueDecl *, Type))
(RequirementKind, const ValueDecl *, WitnessType))
NOTE(ambiguous_witnesses_wrong_name,none,
"multiple matching "
"%select{initializers named %1|functions named %1|properties named %1|"
"subscript operators}0 with type %2",
(RequirementKind, const ValueDecl *, Type))
(RequirementKind, const ValueDecl *, WitnessType))
NOTE(no_witnesses_type,none,
"protocol requires nested type %0; add nested type %0 for conformance",
(const AssociatedTypeDecl *))
Expand Down Expand Up @@ -2982,7 +2982,7 @@ NOTE(protocol_witness_kind_conflict,none,
"candidate is not %select{an initializer|a function|a variable|"
"a subscript}0", (RequirementKind))
NOTE(protocol_witness_type_conflict,none,
"candidate has non-matching type %0%1", (Type, StringRef))
"candidate has non-matching type %0%1", (WitnessType, StringRef))
NOTE(protocol_witness_missing_requirement,none,
"candidate would match if %0 %select{conformed to|subclassed|"
"was the same type as|%error|%error}2 %1", (Type, Type, unsigned))
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ GenericSignature buildGenericSignature(
ASTContext &ctx,
GenericSignature baseSignature,
SmallVector<GenericTypeParamType *, 2> addedParameters,
SmallVector<Requirement, 2> addedRequirements);
SmallVector<Requirement, 2> addedRequirements,
bool allowInverses);

/// Summary of error conditions detected by the Requirement Machine.
enum class GenericSignatureErrorFlags {
Expand Down
47 changes: 6 additions & 41 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,43 +475,6 @@ class InvertibleAnnotationRequest
bool isCached() const { return true; }
};

/// Determine whether the given type is Escapable.
class IsEscapableRequest
: public SimpleRequest<IsEscapableRequest, bool(CanType),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
bool evaluate(Evaluator &evaluator, CanType) const;

public:
// Caching.
bool isCached() const { return true; }
};

/// Determine whether the given type is noncopyable. Assumes type parameters
/// have become archetypes.
class IsNoncopyableRequest
: public SimpleRequest<IsNoncopyableRequest, bool(CanType),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
bool evaluate(Evaluator &evaluator, CanType type) const;

public:
// Caching.
bool isCached() const { return true; }
};

/// Determine whether the given declaration is 'dynamic''.
class IsDynamicRequest :
public SimpleRequest<IsDynamicRequest,
Expand Down Expand Up @@ -2012,7 +1975,8 @@ class AbstractGenericSignatureRequest :
public SimpleRequest<AbstractGenericSignatureRequest,
GenericSignatureWithError (const GenericSignatureImpl *,
SmallVector<GenericTypeParamType *, 2>,
SmallVector<Requirement, 2>),
SmallVector<Requirement, 2>,
bool),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -2025,7 +1989,8 @@ class AbstractGenericSignatureRequest :
evaluate(Evaluator &evaluator,
const GenericSignatureImpl *baseSignature,
SmallVector<GenericTypeParamType *, 2> addedParameters,
SmallVector<Requirement, 2> addedRequirements) const;
SmallVector<Requirement, 2> addedRequirements,
bool allowInverses) const;

public:
// Separate caching.
Expand All @@ -2044,7 +2009,7 @@ class InferredGenericSignatureRequest :
WhereClauseOwner,
SmallVector<Requirement, 2>,
SmallVector<TypeLoc, 2>,
bool),
bool, bool),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -2060,7 +2025,7 @@ class InferredGenericSignatureRequest :
WhereClauseOwner whereClause,
SmallVector<Requirement, 2> addedRequirements,
SmallVector<TypeLoc, 2> inferenceSources,
bool allowConcreteGenericParams) const;
bool isExtension, bool allowInverses) const;

public:
// Separate caching.
Expand Down
11 changes: 3 additions & 8 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
SWIFT_REQUEST(TypeChecker, AbstractGenericSignatureRequest,
GenericSignatureWithError (const GenericSignatureImpl *,
SmallVector<GenericTypeParamType *, 2>,
SmallVector<Requirement, 2>),
SmallVector<Requirement, 2>,
bool),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ApplyAccessNoteRequest,
evaluator::SideEffect(ValueDecl *), Cached, NoLocationInfo)
Expand Down Expand Up @@ -196,7 +197,7 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
GenericParamList *,
WhereClauseOwner,
SmallVector<Requirement, 2>,
SmallVector<TypeLoc, 2>, bool),
SmallVector<TypeLoc, 2>, bool, bool),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
bool(ModuleDecl *), Cached, NoLocationInfo)
Expand All @@ -222,12 +223,6 @@ SWIFT_REQUEST(TypeChecker, IsFinalRequest, bool(ValueDecl *), SeparatelyCached,
SWIFT_REQUEST(TypeChecker, InvertibleAnnotationRequest,
InverseMarking(TypeDecl *, InvertibleProtocolKind),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsNoncopyableRequest,
bool (CanType),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsEscapableRequest,
bool (CanType),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsGetterMutatingRequest, bool(AbstractStorageDecl *),
SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsImplicitlyUnwrappedOptionalRequest,
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ namespace swift {

/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
GenericSignature handleSILGenericParams(GenericParamList *genericParams,
DeclContext *DC);
DeclContext *DC,
bool allowInverses=true);

/// Turn the given module into SIL IR.
///
Expand Down
10 changes: 6 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5973,7 +5973,8 @@ ASTContext::getOpenedExistentialSignature(Type type, GenericSignature parentSig)
constraint);
auto genericSig = buildGenericSignature(
*this, canParentSig,
{genericParam}, {requirement});
{genericParam}, {requirement},
/*allowInverses=*/true);

CanGenericSignature canGenericSig(genericSig);

Expand Down Expand Up @@ -6085,8 +6086,8 @@ ASTContext::getOpenedElementSignature(CanGenericSignature baseGenericSig,
}

auto elementSig = buildGenericSignature(
*this, GenericSignature(), genericParams, requirements)
.getCanonicalSignature();
*this, GenericSignature(), genericParams, requirements,
/*allowInverses=*/false).getCanonicalSignature();
sigs[key] = elementSig;
return elementSig;
}
Expand Down Expand Up @@ -6159,7 +6160,8 @@ ASTContext::getOverrideGenericSignature(const NominalTypeDecl *baseNominal,

auto genericSig = buildGenericSignature(*this, derivedNominalSig,
std::move(addedGenericParams),
std::move(addedRequirements));
std::move(addedRequirements),
/*allowInverses=*/false);
getImpl().overrideSigCache.insert(std::make_pair(key, genericSig));
return genericSig;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ CanGenericSignature ASTBuilder::demangleGenericSignature(
decodeRequirement<BuiltType, BuiltRequirement, BuiltLayoutConstraint,
ASTBuilder>(node, requirements, *this);

return buildGenericSignature(Ctx, baseGenericSig, {}, std::move(requirements))
return buildGenericSignature(Ctx, baseGenericSig, {}, std::move(requirements),
/*allowInverses=*/true)
.getCanonicalSignature();
}

Expand Down
6 changes: 4 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7546,11 +7546,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {

GenericSignature sig = substitutions.getGenericSignature();

// The substituted signature is printed without inverse requirement
// desugaring, but also we drop conformances to Copyable and Escapable
// when constructing it.
sub->Printer << "@substituted ";
sub->printGenericSignature(sig,
PrintAST::PrintParams |
PrintAST::PrintRequirements |
PrintAST::PrintInverseRequirements);
PrintAST::PrintRequirements);
sub->Printer << " ";
}

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/AutoDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ GenericSignature autodiff::getConstrainedDerivativeGenericSignature(

return buildGenericSignature(ctx, derivativeGenSig,
/*addedGenericParams*/ {},
std::move(requirements));
std::move(requirements),
/*allowInverses=*/true);
}

// Given the rest of a `Builtin.applyDerivative_{jvp|vjp}` or
Expand Down
10 changes: 8 additions & 2 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,13 @@ 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));
std::move(collector.AddedRequirements),
/*allowInverses=*/true);
}

/// Build a builtin function declaration.
Expand Down Expand Up @@ -732,10 +735,13 @@ 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));
std::move(addedRequirements),
/*allowInverses=*/true);
return getBuiltinGenericFunction(name, InterfaceParams,
InterfaceResult,
TheGenericParamList, GenericSig,
Expand Down
Loading