Skip to content

Commit 5f3b1da

Browse files
authored
Merge pull request #27949 from CodaFi/protoplasmic-supine-jellies
[NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
2 parents eb8182b + e7006a9 commit 5f3b1da

File tree

85 files changed

+847
-918
lines changed

Some content is hidden

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

85 files changed

+847
-918
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,9 +4380,9 @@ class ProtocolDecl final : public NominalTypeDecl {
43804380

43814381
/// Returns the default associated conformance witness for an associated
43824382
/// type, or \c None if there is no default.
4383-
Optional<ProtocolConformanceRef> getDefaultAssociatedConformanceWitness(
4384-
CanType association,
4385-
ProtocolDecl *requirement) const;
4383+
ProtocolConformanceRef
4384+
getDefaultAssociatedConformanceWitness(CanType association,
4385+
ProtocolDecl *requirement) const;
43864386

43874387
/// Set the default associated conformance witness for the given
43884388
/// associated conformance.

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
269269
/// Look up a stored conformance in the generic signature. These are formed
270270
/// from same-type constraints placed on associated types of generic
271271
/// parameters which have conformance constraints on them.
272-
Optional<ProtocolConformanceRef>
273-
lookupConformance(CanType depTy, ProtocolDecl *proto) const;
272+
ProtocolConformanceRef lookupConformance(CanType depTy,
273+
ProtocolDecl *proto) const;
274274

275275
/// Iterate over all generic parameters, passing a flag to the callback
276276
/// indicating if the generic parameter is canonical or not.

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,9 @@ class GenericSignatureBuilder {
519519
explicit LookUpConformanceInBuilder(GenericSignatureBuilder *builder)
520520
: builder(builder) {}
521521

522-
Optional<ProtocolConformanceRef>
523-
operator()(CanType dependentType,
524-
Type conformingReplacementType,
525-
ProtocolDecl *conformedProtocol) const {
522+
ProtocolConformanceRef operator()(CanType dependentType,
523+
Type conformingReplacementType,
524+
ProtocolDecl *conformedProtocol) const {
526525
return builder->lookupConformance(dependentType,
527526
conformingReplacementType,
528527
conformedProtocol);
@@ -534,10 +533,9 @@ class GenericSignatureBuilder {
534533
LookUpConformanceInBuilder getLookupConformanceFn();
535534

536535
/// Lookup a protocol conformance in a module-agnostic manner.
537-
Optional<ProtocolConformanceRef>
538-
lookupConformance(CanType dependentType, Type conformingReplacementType,
539-
ProtocolDecl *conformedProtocol);
540-
536+
ProtocolConformanceRef lookupConformance(CanType dependentType,
537+
Type conformingReplacementType,
538+
ProtocolDecl *conformedProtocol);
541539

542540
/// Retrieve the lazy resolver, if there is one.
543541
LazyResolver *getLazyResolver() const;

include/swift/AST/Module.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,18 @@ class ModuleDecl : public DeclContext, public TypeDecl {
409409
/// \returns The result of the conformance search, which will be
410410
/// None if the type does not conform to the protocol or contain a
411411
/// ProtocolConformanceRef if it does conform.
412-
Optional<ProtocolConformanceRef>
413-
lookupConformance(Type type, ProtocolDecl *protocol);
412+
ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol);
414413

415414
/// Look for the conformance of the given existential type to the given
416415
/// protocol.
417-
Optional<ProtocolConformanceRef>
418-
lookupExistentialConformance(Type type, ProtocolDecl *protocol);
416+
ProtocolConformanceRef lookupExistentialConformance(Type type,
417+
ProtocolDecl *protocol);
419418

420419
/// Exposes TypeChecker functionality for querying protocol conformance.
421420
/// Returns a valid ProtocolConformanceRef only if all conditional
422421
/// requirements are successfully resolved.
423-
Optional<ProtocolConformanceRef>
424-
conformsToProtocol(Type sourceTy, ProtocolDecl *targetProtocol);
422+
ProtocolConformanceRef conformsToProtocol(Type sourceTy,
423+
ProtocolDecl *targetProtocol);
425424

426425
/// Find a member named \p name in \p container that was declared in this
427426
/// module.

include/swift/AST/ProtocolConformanceRef.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ class ProtocolConformanceRef {
6363
"cannot construct ProtocolConformanceRef with null");
6464
}
6565

66+
ProtocolConformanceRef(std::nullptr_t = nullptr)
67+
: Union((ProtocolDecl *)nullptr) {}
68+
6669
static ProtocolConformanceRef forInvalid() {
67-
return ProtocolConformanceRef(UnionType((ProtocolDecl *)nullptr));
70+
return ProtocolConformanceRef();
6871
}
6972

7073
bool isInvalid() const {
7174
return !Union;
7275
}
7376

77+
explicit operator bool() const { return !isInvalid(); }
78+
7479
/// Create either a concrete or an abstract protocol conformance reference,
7580
/// depending on whether ProtocolConformance is null.
7681
explicit ProtocolConformanceRef(ProtocolDecl *protocol,

include/swift/AST/Stmt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ class ForEachStmt : public LabeledStmt {
805805
BraceStmt *Body;
806806

807807
// Set by Sema:
808-
Optional<ProtocolConformanceRef> sequenceConformance;
808+
ProtocolConformanceRef sequenceConformance = ProtocolConformanceRef();
809809
ConcreteDeclRef makeIterator;
810810
ConcreteDeclRef iteratorNext;
811811
VarDecl *iteratorVar = nullptr;
@@ -842,10 +842,10 @@ class ForEachStmt : public LabeledStmt {
842842
void setIteratorNext(ConcreteDeclRef declRef) { iteratorNext = declRef; }
843843
ConcreteDeclRef getIteratorNext() const { return iteratorNext; }
844844

845-
void setSequenceConformance(Optional<ProtocolConformanceRef> conformance) {
845+
void setSequenceConformance(ProtocolConformanceRef conformance) {
846846
sequenceConformance = conformance;
847847
}
848-
Optional<ProtocolConformanceRef> getSequenceConformance() const {
848+
ProtocolConformanceRef getSequenceConformance() const {
849849
return sequenceConformance;
850850
}
851851

include/swift/AST/SubstitutionMap.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ class SubstitutionMap {
128128
ArrayRef<ProtocolConformanceRef> getConformances() const;
129129

130130
/// Look up a conformance for the given type to the given protocol.
131-
Optional<ProtocolConformanceRef>
132-
lookupConformance(CanType type, ProtocolDecl *proto) const;
131+
ProtocolConformanceRef lookupConformance(CanType type,
132+
ProtocolDecl *proto) const;
133133

134134
/// Whether the substitution map is empty.
135135
bool empty() const;
@@ -290,11 +290,10 @@ class LookUpConformanceInSubstitutionMap {
290290
public:
291291
explicit LookUpConformanceInSubstitutionMap(SubstitutionMap Subs)
292292
: Subs(Subs) {}
293-
294-
Optional<ProtocolConformanceRef>
295-
operator()(CanType dependentType,
296-
Type conformingReplacementType,
297-
ProtocolDecl *conformedProtocol) const;
293+
294+
ProtocolConformanceRef operator()(CanType dependentType,
295+
Type conformingReplacementType,
296+
ProtocolDecl *conformedProtocol) const;
298297
};
299298

300299
} // end namespace swift

include/swift/AST/Type.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct QueryTypeSubstitutionMapOrIdentity {
9393
using GenericFunction = auto(CanType dependentType,
9494
Type conformingReplacementType,
9595
ProtocolDecl *conformedProtocol)
96-
-> Optional<ProtocolConformanceRef>;
96+
-> ProtocolConformanceRef;
9797
using LookupConformanceFn = llvm::function_ref<GenericFunction>;
9898

9999
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
@@ -103,22 +103,20 @@ class LookUpConformanceInModule {
103103
public:
104104
explicit LookUpConformanceInModule(ModuleDecl *M)
105105
: M(M) {}
106-
107-
Optional<ProtocolConformanceRef>
108-
operator()(CanType dependentType,
109-
Type conformingReplacementType,
110-
ProtocolDecl *conformedProtocol) const;
106+
107+
ProtocolConformanceRef operator()(CanType dependentType,
108+
Type conformingReplacementType,
109+
ProtocolDecl *conformedProtocol) const;
111110
};
112111

113112
/// Functor class suitable for use as a \c LookupConformanceFn that provides
114113
/// only abstract conformances for generic types. Asserts that the replacement
115114
/// type is an opaque generic type.
116115
class MakeAbstractConformanceForGenericType {
117116
public:
118-
Optional<ProtocolConformanceRef>
119-
operator()(CanType dependentType,
120-
Type conformingReplacementType,
121-
ProtocolDecl *conformedProtocol) const;
117+
ProtocolConformanceRef operator()(CanType dependentType,
118+
Type conformingReplacementType,
119+
ProtocolDecl *conformedProtocol) const;
122120
};
123121

124122
/// Functor class suitable for use as a \c LookupConformanceFn that fetches
@@ -130,11 +128,10 @@ class LookUpConformanceInSignature {
130128
: Sig(Sig) {
131129
assert(Sig && "Cannot lookup conformance in null signature!");
132130
}
133-
134-
Optional<ProtocolConformanceRef>
135-
operator()(CanType dependentType,
136-
Type conformingReplacementType,
137-
ProtocolDecl *conformedProtocol) const;
131+
132+
ProtocolConformanceRef operator()(CanType dependentType,
133+
Type conformingReplacementType,
134+
ProtocolDecl *conformedProtocol) const;
138135
};
139136

140137
/// Flags that can be passed when substituting into a type.

include/swift/AST/Types.h

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3826,7 +3826,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
38263826
// CanType? // if !isCoro && NumAnyResults > 1, all result cache
38273827

38283828
llvm::PointerIntPair<CanGenericSignature, 1, bool> GenericSigAndIsImplied;
3829-
Optional<ProtocolConformanceRef> WitnessMethodConformance;
3829+
ProtocolConformanceRef WitnessMethodConformance;
38303830
SubstitutionMap Substitutions;
38313831

38323832
MutableArrayRef<SILParameterInfo> getMutableParameters() {
@@ -3888,25 +3888,22 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
38883888
ArrayRef<SILYieldInfo> yieldResults,
38893889
ArrayRef<SILResultInfo> normalResults,
38903890
Optional<SILResultInfo> errorResult,
3891-
SubstitutionMap substitutions,
3892-
bool genericSigIsImplied,
3893-
const ASTContext &ctx,
3894-
RecursiveTypeProperties properties,
3895-
Optional<ProtocolConformanceRef> witnessMethodConformance);
3891+
SubstitutionMap substitutions, bool genericSigIsImplied,
3892+
const ASTContext &ctx, RecursiveTypeProperties properties,
3893+
ProtocolConformanceRef witnessMethodConformance);
38963894

38973895
public:
3898-
static CanSILFunctionType get(GenericSignature genericSig,
3899-
ExtInfo ext,
3900-
SILCoroutineKind coroutineKind,
3901-
ParameterConvention calleeConvention,
3902-
ArrayRef<SILParameterInfo> interfaceParams,
3903-
ArrayRef<SILYieldInfo> interfaceYields,
3904-
ArrayRef<SILResultInfo> interfaceResults,
3905-
Optional<SILResultInfo> interfaceErrorResult,
3906-
SubstitutionMap substitutions,
3907-
bool genericSigIsImplied,
3908-
const ASTContext &ctx,
3909-
Optional<ProtocolConformanceRef> witnessMethodConformance = None);
3896+
static CanSILFunctionType
3897+
get(GenericSignature genericSig, ExtInfo ext, SILCoroutineKind coroutineKind,
3898+
ParameterConvention calleeConvention,
3899+
ArrayRef<SILParameterInfo> interfaceParams,
3900+
ArrayRef<SILYieldInfo> interfaceYields,
3901+
ArrayRef<SILResultInfo> interfaceResults,
3902+
Optional<SILResultInfo> interfaceErrorResult,
3903+
SubstitutionMap substitutions, bool genericSigIsImplied,
3904+
const ASTContext &ctx,
3905+
ProtocolConformanceRef witnessMethodConformance =
3906+
ProtocolConformanceRef());
39103907

39113908
/// Return a structurally-identical function type with a slightly tweaked
39123909
/// ExtInfo.
@@ -4105,16 +4102,9 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
41054102
ClassDecl *getWitnessMethodClass(SILModule &M) const;
41064103

41074104
/// If this is a @convention(witness_method) function, return the conformance
4108-
/// for which the method is a witness.
4109-
ProtocolConformanceRef getWitnessMethodConformance() const {
4110-
assert(getRepresentation() == Representation::WitnessMethod);
4111-
return *WitnessMethodConformance;
4112-
}
4113-
4114-
/// If this is a @convention(witness_method) function, return the conformance
4115-
/// for which the method is a witness, if it isn't that convention, return
4116-
/// None.
4117-
Optional<ProtocolConformanceRef> getWitnessMethodConformanceOrNone() const {
4105+
/// for which the method is a witness. If it isn't that convention, return
4106+
/// an invalid conformance.
4107+
ProtocolConformanceRef getWitnessMethodConformanceOrInvalid() const {
41184108
return WitnessMethodConformance;
41194109
}
41204110

@@ -4209,24 +4199,17 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
42094199

42104200
void Profile(llvm::FoldingSetNodeID &ID) {
42114201
Profile(ID, getSubstGenericSignature(), getExtInfo(), getCoroutineKind(),
4212-
getCalleeConvention(), getParameters(), getYields(),
4213-
getResults(), getOptionalErrorResult(),
4214-
getWitnessMethodConformanceOrNone(),
4215-
isGenericSignatureImplied(),
4216-
getSubstitutions());
4217-
}
4218-
static void Profile(llvm::FoldingSetNodeID &ID,
4219-
GenericSignature genericSig,
4220-
ExtInfo info,
4221-
SILCoroutineKind coroutineKind,
4222-
ParameterConvention calleeConvention,
4223-
ArrayRef<SILParameterInfo> params,
4224-
ArrayRef<SILYieldInfo> yields,
4225-
ArrayRef<SILResultInfo> results,
4226-
Optional<SILResultInfo> errorResult,
4227-
Optional<ProtocolConformanceRef> conformance,
4228-
bool isGenericSigImplied,
4229-
SubstitutionMap substitutions);
4202+
getCalleeConvention(), getParameters(), getYields(), getResults(),
4203+
getOptionalErrorResult(), getWitnessMethodConformanceOrInvalid(),
4204+
isGenericSignatureImplied(), getSubstitutions());
4205+
}
4206+
static void
4207+
Profile(llvm::FoldingSetNodeID &ID, GenericSignature genericSig, ExtInfo info,
4208+
SILCoroutineKind coroutineKind, ParameterConvention calleeConvention,
4209+
ArrayRef<SILParameterInfo> params, ArrayRef<SILYieldInfo> yields,
4210+
ArrayRef<SILResultInfo> results, Optional<SILResultInfo> errorResult,
4211+
ProtocolConformanceRef conformance, bool isGenericSigImplied,
4212+
SubstitutionMap substitutions);
42304213

42314214
// Implement isa/cast/dyncast/etc.
42324215
static bool classof(const TypeBase *T) {
@@ -4972,9 +4955,9 @@ class ReplaceOpaqueTypesWithUnderlyingTypes {
49724955
Type operator()(SubstitutableType *maybeOpaqueType) const;
49734956

49744957
/// LookupConformanceFn
4975-
Optional<ProtocolConformanceRef> operator()(CanType maybeOpaqueType,
4976-
Type replacementType,
4977-
ProtocolDecl *protocol) const;
4958+
ProtocolConformanceRef operator()(CanType maybeOpaqueType,
4959+
Type replacementType,
4960+
ProtocolDecl *protocol) const;
49784961

49794962
OpaqueSubstitutionKind
49804963
shouldPerformSubstitution(OpaqueTypeDecl *opaque) const;

include/swift/SIL/SILType.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,10 @@ NON_SIL_TYPE(LValue)
587587

588588
CanSILFunctionType getNativeSILFunctionType(
589589
Lowering::TypeConverter &TC, Lowering::AbstractionPattern origType,
590-
CanAnyFunctionType substType,
591-
Optional<SILDeclRef> origConstant = None,
590+
CanAnyFunctionType substType, Optional<SILDeclRef> origConstant = None,
592591
Optional<SILDeclRef> constant = None,
593592
Optional<SubstitutionMap> reqtSubs = None,
594-
Optional<ProtocolConformanceRef> witnessMethodConformance = None);
593+
ProtocolConformanceRef witnessMethodConformance = ProtocolConformanceRef());
595594

596595
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILType T) {
597596
T.print(OS);

include/swift/SIL/TypeLowering.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ inline CanAnyFunctionType adjustFunctionType(CanAnyFunctionType t,
7272
CanSILFunctionType
7373
adjustFunctionType(CanSILFunctionType type, SILFunctionType::ExtInfo extInfo,
7474
ParameterConvention calleeConv,
75-
Optional<ProtocolConformanceRef> witnessMethodConformance);
75+
ProtocolConformanceRef witnessMethodConformance);
7676
inline CanSILFunctionType
7777
adjustFunctionType(CanSILFunctionType type, SILFunctionType::ExtInfo extInfo,
78-
Optional<ProtocolConformanceRef> witnessMethodConformance) {
78+
ProtocolConformanceRef witnessMethodConformance) {
7979
return adjustFunctionType(type, extInfo, type->getCalleeConvention(),
8080
witnessMethodConformance);
8181
}
8282
inline CanSILFunctionType
8383
adjustFunctionType(CanSILFunctionType t, SILFunctionType::Representation rep,
84-
Optional<ProtocolConformanceRef> witnessMethodConformance) {
84+
ProtocolConformanceRef witnessMethodConformance) {
8585
if (t->getRepresentation() == rep) return t;
8686
auto extInfo = t->getExtInfo().withRepresentation(rep);
8787
auto contextConvention = DefaultThickCalleeConvention;

include/swift/SILOptimizer/Utils/Existential.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ struct ConcreteExistentialInfo {
9898
// Do a conformance lookup on ConcreteType with the given requirement, P. If P
9999
// is satisfiable based on the existential's conformance, return the new
100100
// conformance on P. Otherwise return None.
101-
Optional<ProtocolConformanceRef>
102-
lookupExistentialConformance(ProtocolDecl *P) const {
101+
ProtocolConformanceRef lookupExistentialConformance(ProtocolDecl *P) const {
103102
CanType selfTy = P->getSelfInterfaceType()->getCanonicalType();
104103
return ExistentialSubs.lookupConformance(selfTy, P);
105104
}

0 commit comments

Comments
 (0)