Skip to content

Commit b849e51

Browse files
committed
Use operator bool to claw back some readability
1 parent 37e82a6 commit b849e51

Some content is hidden

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

45 files changed

+120
-156
lines changed

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,

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,8 +3168,8 @@ void SILFunctionType::Profile(
31683168
if (errorResult) errorResult->profile(id);
31693169
id.AddBoolean(isGenericSignatureImplied);
31703170
substitutions.profile(id);
3171-
id.AddBoolean(!conformance.isInvalid());
3172-
if (!conformance.isInvalid())
3171+
id.AddBoolean((bool)conformance);
3172+
if (conformance)
31733173
id.AddPointer(conformance.getRequirement());
31743174
}
31753175

@@ -4167,7 +4167,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
41674167
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
41684168
auto conformance = dc->getParentModule()->lookupConformance(
41694169
nominal->getDeclaredType(), objcBridgeable);
4170-
if (!conformance.isInvalid()) {
4170+
if (conformance) {
41714171
result =
41724172
ForeignRepresentationInfo::forBridged(conformance.getConcrete());
41734173
}
@@ -4320,8 +4320,8 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
43204320
};
43214321

43224322
// Do we conform to _ObjectiveCBridgeable?
4323-
auto conformance = findConformance(KnownProtocolKind::ObjectiveCBridgeable);
4324-
if (!conformance.isInvalid()) {
4323+
if (auto conformance =
4324+
findConformance(KnownProtocolKind::ObjectiveCBridgeable)) {
43254325
// The corresponding value type is... the type.
43264326
if (bridgedValueType)
43274327
*bridgedValueType = type;
@@ -4331,7 +4331,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
43314331
}
43324332

43334333
// Do we conform to Error?
4334-
if (!findConformance(KnownProtocolKind::Error).isInvalid()) {
4334+
if (findConformance(KnownProtocolKind::Error)) {
43354335
// The corresponding value type is Error.
43364336
if (bridgedValueType)
43374337
*bridgedValueType = getErrorDecl()->getDeclaredInterfaceType();
@@ -4465,8 +4465,7 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44654465
auto lookupConformanceFn =
44664466
[&](CanType depTy, Type substTy,
44674467
ProtocolDecl *proto) -> ProtocolConformanceRef {
4468-
auto conf = subMap.lookupConformance(depTy, proto);
4469-
if (!conf.isInvalid())
4468+
if (auto conf = subMap.lookupConformance(depTy, proto))
44704469
return conf;
44714470

44724471
return ProtocolConformanceRef(proto);

lib/AST/GenericSignature.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,8 @@ bool GenericSignatureImpl::isRequirementSatisfied(Requirement requirement) {
536536
if (canFirstType->isTypeParameter())
537537
return conformsToProtocol(canFirstType, protocol);
538538
else
539-
return !GSB->lookupConformance(/*dependentType=*/CanType(), canFirstType,
540-
protocol)
541-
.isInvalid();
539+
return (bool)GSB->lookupConformance(/*dependentType=*/CanType(),
540+
canFirstType, protocol);
542541
}
543542

544543
case RequirementKind::SameType: {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4416,8 +4416,8 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
44164416
auto conformance =
44174417
getLookupConformanceFn()(dependentType, subjectType, proto->getDecl());
44184418

4419-
// FIXME: diagnose if there's an invalid conformance.
4420-
if (!conformance.isInvalid()) {
4419+
// FIXME: diagnose if there's no conformance.
4420+
if (conformance) {
44214421
if (addConditionalRequirements(conformance, inferForModule,
44224422
source.getLoc()))
44234423
return ConstraintResult::Conflicting;

lib/AST/Module.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,7 @@ ModuleDecl::lookupExistentialConformance(Type type, ProtocolDecl *protocol) {
748748
// If the existential is class-constrained, the class might conform
749749
// concretely.
750750
if (auto superclass = layout.explicitSuperclass) {
751-
auto result = lookupConformance(superclass, protocol);
752-
if (!result.isInvalid())
751+
if (auto result = lookupConformance(superclass, protocol))
753752
return result;
754753
}
755754

@@ -765,8 +764,7 @@ ModuleDecl::lookupExistentialConformance(Type type, ProtocolDecl *protocol) {
765764
// If the protocol has a superclass constraint, we might conform
766765
// concretely.
767766
if (auto superclass = protoDecl->getSuperclass()) {
768-
auto result = lookupConformance(superclass, protocol);
769-
if (!result.isInvalid())
767+
if (auto result = lookupConformance(superclass, protocol))
770768
return result;
771769
}
772770

@@ -800,8 +798,7 @@ ProtocolConformanceRef ModuleDecl::lookupConformance(Type type,
800798
// able to be resolved by a substitution that makes the archetype
801799
// concrete.
802800
if (auto super = archetype->getSuperclass()) {
803-
auto inheritedConformance = lookupConformance(super, protocol);
804-
if (!inheritedConformance.isInvalid()) {
801+
if (auto inheritedConformance = lookupConformance(super, protocol)) {
805802
return ProtocolConformanceRef(ctx.getInheritedConformance(
806803
type, inheritedConformance.getConcrete()));
807804
}
@@ -860,7 +857,7 @@ ProtocolConformanceRef ModuleDecl::lookupConformance(Type type,
860857

861858
// Compute the conformance for the inherited type.
862859
auto inheritedConformance = lookupConformance(superclassTy, protocol);
863-
assert(!inheritedConformance.isInvalid() &&
860+
assert(inheritedConformance &&
864861
"We already found the inherited conformance");
865862

866863
// Create the inherited conformance entry.

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ProtocolConformanceRef::subst(Type origType,
131131
auto optConformance =
132132
proto->getModuleContext()->lookupExistentialConformance(substType,
133133
proto);
134-
if (!optConformance.isInvalid())
134+
if (optConformance)
135135
return optConformance;
136136

137137
return ProtocolConformanceRef::forInvalid();

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
355355
// Check whether the superclass conforms.
356356
if (auto superclass = genericSig->getSuperclassBound(type)) {
357357
LookUpConformanceInSignature lookup(getGenericSignature().getPointer());
358-
auto conformance = lookup(type->getCanonicalType(), superclass, proto);
359-
if (!conformance.isInvalid())
358+
if (auto conformance = lookup(type->getCanonicalType(), superclass, proto))
360359
return conformance;
361360
}
362361

lib/IDE/ConformingMethodList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void ConformingMethodListCallbacks::getMatchingMethods(
141141

142142
// The return type conforms to any of the requested protocols.
143143
for (auto Proto : ExpectedTypes) {
144-
if (!CurModule->conformsToProtocol(declTy, Proto).isInvalid())
144+
if (CurModule->conformsToProtocol(declTy, Proto))
145145
return true;
146146
}
147147

lib/IDE/IDETypeChecking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ class ExpressionTypeCollector: public SourceEntityWalker {
619619

620620
// Collecting protocols conformed by this expressions that are in the list.
621621
for (auto Proto: InterestedProtocols) {
622-
if (!Module.conformsToProtocol(E->getType(), Proto.first).isInvalid()) {
622+
if (Module.conformsToProtocol(E->getType(), Proto.first)) {
623623
Conformances.push_back(Proto.second);
624624
}
625625
}

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,7 @@ emitConditionalConformancesBuffer(IRGenFunction &IGF,
956956
rootConformance, [&](unsigned, CanType type, ProtocolDecl *proto) {
957957
auto substType = type.subst(subMap)->getCanonicalType();
958958
auto reqConformance = subMap.lookupConformance(type, proto);
959-
assert(!reqConformance.isInvalid() &&
960-
"conditional conformance must be valid");
959+
assert(reqConformance && "conditional conformance must be valid");
961960

962961
tables.push_back(emitWitnessTableRef(IGF, substType, reqConformance));
963962
return /*finished?*/ false;

lib/IRGen/MetadataRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ namespace {
473473
requirements.enumerateFulfillments(
474474
IGF.IGM, subs,
475475
[&](unsigned reqtIndex, CanType type, ProtocolConformanceRef conf) {
476-
if (!conf.isInvalid()) {
476+
if (conf) {
477477
Values.push_back(emitWitnessTableRef(IGF, type, conf));
478478
} else {
479479
Values.push_back(IGF.emitAbstractTypeMetadataRef(type));

lib/ParseSIL/ParseSIL.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,14 +1766,11 @@ static bool getConformancesForSubstitution(Parser &P,
17661766
for (auto protoTy : protocols) {
17671767
auto conformance = M->lookupConformance(subReplacement,
17681768
protoTy->getDecl());
1769-
if (!conformance.isInvalid()) {
1770-
conformances.push_back(conformance);
1771-
continue;
1769+
if (conformance.isInvalid()) {
1770+
P.diagnose(loc, diag::sil_substitution_mismatch, subReplacement, protoTy);
1771+
return true;
17721772
}
1773-
1774-
P.diagnose(loc, diag::sil_substitution_mismatch, subReplacement,
1775-
protoTy);
1776-
return true;
1773+
conformances.push_back(conformance);
17771774
}
17781775

17791776
return false;
@@ -1821,8 +1818,7 @@ SubstitutionMap getApplySubstitutionsFromParsed(
18211818
[&](CanType dependentType, Type replacementType,
18221819
ProtocolDecl *proto) -> ProtocolConformanceRef {
18231820
auto M = SP.P.SF.getParentModule();
1824-
auto conformance = M->lookupConformance(replacementType, proto);
1825-
if (!conformance.isInvalid())
1821+
if (auto conformance = M->lookupConformance(replacementType, proto))
18261822
return conformance;
18271823

18281824
SP.P.diagnose(loc, diag::sil_substitution_mismatch, replacementType,

lib/SIL/DynamicCasts.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ classifyDynamicCastToProtocol(ModuleDecl *M, CanType source, CanType target,
9696

9797
// If conformsToProtocol returns a valid conformance, then all requirements
9898
// were proven by the type checker.
99-
if (!M->conformsToProtocol(source, TargetProtocol).isInvalid())
99+
if (M->conformsToProtocol(source, TargetProtocol))
100100
return DynamicCastFeasibility::WillSucceed;
101101

102102
auto *SourceNominalTy = source.getAnyNominal();
@@ -134,8 +134,7 @@ classifyDynamicCastToProtocol(ModuleDecl *M, CanType source, CanType target,
134134
// the conformsToProtocol interface needs to be reformulated as a query, and
135135
// the implementation, including checkGenericArguments, needs to be taught to
136136
// recognize that types with archetypes may potentially succeed.
137-
auto conformance = M->lookupConformance(source, TargetProtocol);
138-
if (!conformance.isInvalid()) {
137+
if (auto conformance = M->lookupConformance(source, TargetProtocol)) {
139138
assert(!conformance.getConditionalRequirements().empty());
140139
return DynamicCastFeasibility::MaySucceed;
141140
}
@@ -218,7 +217,7 @@ bool swift::isObjectiveCBridgeable(ModuleDecl *M, CanType Ty) {
218217
if (bridgedProto) {
219218
// Find the conformance of the value type to _BridgedToObjectiveC.
220219
// Check whether the type conforms to _BridgedToObjectiveC.
221-
return !M->lookupConformance(Ty, bridgedProto).isInvalid();
220+
return (bool)M->lookupConformance(Ty, bridgedProto);
222221
}
223222
return false;
224223
}
@@ -232,7 +231,7 @@ bool swift::isError(ModuleDecl *M, CanType Ty) {
232231
if (errorTypeProto) {
233232
// Find the conformance of the value type to Error.
234233
// Check whether the type conforms to Error.
235-
return !M->lookupConformance(Ty, errorTypeProto).isInvalid();
234+
return (bool)M->lookupConformance(Ty, errorTypeProto);
236235
}
237236
return false;
238237
}

lib/SIL/SILFunctionType.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,8 +2423,7 @@ class SILTypeSubstituter :
24232423
}
24242424

24252425
auto witnessMethodConformance = ProtocolConformanceRef::forInvalid();
2426-
auto conformance = origType->getWitnessMethodConformanceOrInvalid();
2427-
if (!conformance.isInvalid()) {
2426+
if (auto conformance = origType->getWitnessMethodConformanceOrInvalid()) {
24282427
assert(origType->getExtInfo().hasSelfParam());
24292428
auto selfType = origType->getSelfParameter().getInterfaceType();
24302429
// The Self type can be nested in a few layers of metatypes (etc.).

lib/SILGen/SILGenBridging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static bool shouldBridgeThroughError(SILGenModule &SGM, CanType type,
7777
}
7878
}
7979

80-
return !SGM.SwiftModule->lookupConformance(type, errorProtocol).isInvalid();
80+
return (bool)SGM.SwiftModule->lookupConformance(type, errorProtocol);
8181
}
8282

8383
/// Bridge the given Swift value to its corresponding Objective-C

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ ManagedValue SILGenFunction::emitExistentialErasure(
643643
// then just erase the NSError.
644644
auto storedNSErrorConformance =
645645
SGM.getConformanceToBridgedStoredNSError(loc, concreteFormalType);
646-
if (!storedNSErrorConformance.isInvalid()) {
646+
if (storedNSErrorConformance) {
647647
auto nsErrorVar = SGM.getNSErrorRequirement(loc);
648648
if (!nsErrorVar) return emitUndef(existentialTL.getLoweredType());
649649

lib/SILGen/SILGenLazyConformance.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,14 @@ void SILGenModule::useConformancesFromObjectiveCType(CanType type) {
109109
return;
110110

111111
if (objectiveCBridgeable) {
112-
auto subConformance = SwiftModule->lookupConformance(
113-
t, objectiveCBridgeable);
114-
if (!subConformance.isInvalid())
112+
if (auto subConformance =
113+
SwiftModule->lookupConformance(t, objectiveCBridgeable))
115114
useConformance(subConformance);
116115
}
117116

118117
if (bridgedStoredNSError) {
119-
auto subConformance = SwiftModule->lookupConformance(
120-
t, bridgedStoredNSError);
121-
if (!subConformance.isInvalid())
118+
if (auto subConformance =
119+
SwiftModule->lookupConformance(t, bridgedStoredNSError))
122120
useConformance(subConformance);
123121
}
124122
});

lib/SILGen/SILGenPoly.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ collectExistentialConformances(ModuleDecl *M, CanType fromType, CanType toType)
177177
for (auto proto : protocols) {
178178
auto conformance =
179179
M->lookupConformance(fromType, proto->getDecl());
180-
assert(!conformance.isInvalid());
180+
assert(conformance);
181181
conformances.push_back(conformance);
182182
}
183183

lib/SILGen/SILGenType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ class SILGenConformance : public SILGenWitnessTable<SILGenConformance> {
558558
auto conformance =
559559
Conformance->getGenericSignature()->lookupConformance(type,
560560
protocol);
561-
assert(!conformance.isInvalid() &&
561+
assert(conformance &&
562562
"unable to find conformance that should be known");
563563

564564
ConditionalConformances.push_back(

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ findBridgeToObjCFunc(SILOptFunctionBuilder &functionBuilder,
483483
mod.getASTContext().getProtocol(KnownProtocolKind::ObjectiveCBridgeable);
484484

485485
auto conf = mod.getSwiftModule()->lookupConformance(sourceType, bridgedProto);
486-
assert(!conf.isInvalid() && "_ObjectiveCBridgeable conformance should exist");
486+
assert(conf && "_ObjectiveCBridgeable conformance should exist");
487487
(void)conf;
488488

489489
// Generate code to invoke _bridgeToObjectiveC

0 commit comments

Comments
 (0)