Skip to content

Commit 32443e2

Browse files
authored
Merge pull request #33011 from hamishknight/type-safety
[AST] Assert dereferencing null Type or GenericSignature
2 parents 4a6a684 + 58166fc commit 32443e2

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ class GenericSignature {
124124

125125
bool isNull() const { return Ptr == 0; }
126126

127-
const GenericSignatureImpl *operator->() const { return Ptr; }
127+
const GenericSignatureImpl *operator->() const {
128+
assert(Ptr && "Cannot dereference a null GenericSignature!");
129+
return Ptr;
130+
}
128131

129132
explicit operator bool() const { return Ptr != 0; }
130133

include/swift/AST/Type.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ class Type {
213213

214214
bool isNull() const { return Ptr == 0; }
215215

216-
TypeBase *operator->() const { return Ptr; }
216+
TypeBase *operator->() const {
217+
assert(Ptr && "Cannot dereference a null Type!");
218+
return Ptr;
219+
}
217220

218221
explicit operator bool() const { return Ptr != 0; }
219222

@@ -530,7 +533,10 @@ public: \
530533
TYPE *getPointer() const { \
531534
return static_cast<TYPE*>(Type::getPointer()); \
532535
} \
533-
TYPE *operator->() const { return getPointer(); } \
536+
TYPE *operator->() const { \
537+
assert(getPointer() && "Cannot dereference a null " #TYPE); \
538+
return getPointer(); \
539+
} \
534540
operator TYPE *() const { return getPointer(); } \
535541
explicit operator bool() const { return getPointer() != nullptr; }
536542

0 commit comments

Comments
 (0)