Skip to content

Commit 5916325

Browse files
authored
Merge pull request swiftlang#75151 from tbkka/tbkka-harden-casting-failure
Tailored error reporting when dynamic casts have nonsensical type args
2 parents 42c43e6 + fac8c97 commit 5916325

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

stdlib/public/runtime/Casting.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,48 @@ swift::swift_dynamicCastFailure(const void *sourceType, const char *sourceName,
416416
message ? message : "");
417417
}
418418

419-
SWIFT_NORETURN void swift::swift_dynamicCastFailure(const Metadata *sourceType,
420-
const Metadata *targetType,
421-
const char *message) {
419+
SWIFT_NORETURN SWIFT_NOINLINE void
420+
swift_dynamicCastFailure_SOURCE_AND_TARGET_TYPE_NULL(const char *message) {
421+
swift::fatalError(0, "Unconditional cast failed. "
422+
"Both source and target types were NULL. "
423+
"%s\n",
424+
message ? message : "");
425+
}
426+
427+
SWIFT_NORETURN SWIFT_NOINLINE void
428+
swift_dynamicCastFailure_SOURCE_TYPE_NULL(const Metadata *targetType, const char *message) {
429+
std::string targetName = nameForMetadata(targetType);
430+
swift::fatalError(0, "Unconditional cast failed. "
431+
"Source type was NULL, target was '%s' (%p). "
432+
"%s\n",
433+
targetName.c_str(), targetType,
434+
message ? message : "");
435+
}
436+
437+
SWIFT_NORETURN SWIFT_NOINLINE void
438+
swift_dynamicCastFailure_TARGET_TYPE_NULL(const Metadata *sourceType, const char *message) {
439+
std::string sourceName = nameForMetadata(sourceType);
440+
swift::fatalError(0, "Unconditional cast failed. "
441+
"Source type was '%s' (%p), target type was NULL. "
442+
"%s\n",
443+
sourceName.c_str(), sourceType,
444+
message ? message : "");
445+
}
446+
447+
SWIFT_NORETURN SWIFT_NOINLINE void
448+
swift::swift_dynamicCastFailure(const Metadata *sourceType,
449+
const Metadata *targetType,
450+
const char *message) {
451+
if (sourceType == nullptr) {
452+
if (targetType == nullptr) {
453+
swift_dynamicCastFailure_SOURCE_AND_TARGET_TYPE_NULL(message);
454+
} else {
455+
swift_dynamicCastFailure_SOURCE_TYPE_NULL(targetType, message);
456+
}
457+
} else if (targetType == nullptr) {
458+
swift_dynamicCastFailure_TARGET_TYPE_NULL(sourceType, message);
459+
}
460+
422461
std::string sourceName = nameForMetadata(sourceType);
423462
std::string targetName = nameForMetadata(targetType);
424463

0 commit comments

Comments
 (0)