Skip to content

Commit 0823fb7

Browse files
slavapestovspestov@apple.com
authored andcommitted
Reflection: Add an unreachable() function instead of doing assert(false && ...
1 parent a663e79 commit 0823fb7

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

stdlib/public/Reflection/TypeLowering.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
#include <iostream>
2626

27+
[[noreturn]]
28+
static void unreachable(const char *Message) {
29+
std::cerr << "fatal error: " << Message << "\n";
30+
std::abort();
31+
}
32+
2733
using namespace swift;
2834
using namespace reflection;
2935

@@ -167,7 +173,7 @@ class PrintTypeInfo {
167173
}
168174
}
169175

170-
assert(false && "Bad TypeInfo kind");
176+
unreachable("Bad TypeInfo kind");
171177
}
172178
};
173179

@@ -675,13 +681,13 @@ class HasSingletonMetatype
675681

676682
MetatypeRepresentation
677683
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
678-
assert(false && "Must have concrete TypeRef");
684+
unreachable("Must have concrete TypeRef");
679685
return MetatypeRepresentation::Unknown;
680686
}
681687

682688
MetatypeRepresentation
683689
visitDependentMemberTypeRef(const DependentMemberTypeRef *DM) {
684-
assert(false && "Must have concrete TypeRef");
690+
unreachable("Must have concrete TypeRef");
685691
return MetatypeRepresentation::Unknown;
686692
}
687693

@@ -893,13 +899,13 @@ class LowerType
893899

894900
const TypeInfo *
895901
visitGenericTypeParameterTypeRef(const GenericTypeParameterTypeRef *GTP) {
896-
assert(false && "Must have concrete TypeRef");
902+
unreachable("Must have concrete TypeRef");
897903
return nullptr;
898904
}
899905

900906
const TypeInfo *
901907
visitDependentMemberTypeRef(const DependentMemberTypeRef *DM) {
902-
assert(false && "Must have concrete TypeRef");
908+
unreachable("Must have concrete TypeRef");
903909
return nullptr;
904910
}
905911

@@ -980,7 +986,7 @@ class LowerType
980986
}
981987

982988
const TypeInfo *visitOpaqueTypeRef(const OpaqueTypeRef *O) {
983-
assert(false && "Can't lower opaque TypeRef");
989+
unreachable("Can't lower opaque TypeRef");
984990
return nullptr;
985991
}
986992
};

stdlib/public/Reflection/TypeRef.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
using namespace swift;
2323
using namespace reflection;
2424

25+
[[noreturn]]
26+
static void unreachable(const char *Message) {
27+
std::cerr << "fatal error: " << Message << "\n";
28+
std::abort();
29+
}
30+
2531
class PrintTypeRef : public TypeRefVisitor<PrintTypeRef, void> {
2632
std::ostream &OS;
2733
unsigned Indent;
@@ -354,7 +360,7 @@ unsigned NominalTypeTrait::getDepth() const {
354360
case TypeRefKind::BoundGeneric:
355361
return 1 + cast<BoundGenericTypeRef>(P)->getDepth();
356362
default:
357-
assert(false && "Asked for depth on non-nominal typeref");
363+
unreachable("Asked for depth on non-nominal typeref");
358364
}
359365
}
360366

@@ -663,7 +669,7 @@ class TypeRefSubstitution
663669
break;
664670
}
665671
default:
666-
assert(false && "Unknown base type");
672+
unreachable("Unknown base type");
667673
}
668674

669675
assert(TypeWitness);

0 commit comments

Comments
 (0)