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;

0 commit comments

Comments
 (0)