[cast-optimizer] Fix incorrect handling of returned pointers.

Put simply, the method Type::getAnyNominal() returns either a nominal type or a
nullptr. We are then casting it using dyn_cast to a ProtocolDecl. dyn_cast is
not allowed to handle nullptrs... hence the crash. The fix is to use the API
dyn_cast_or_null, the specific cast meant for this case!

In terms of how this effects the flow of the code in such a case, we will just
bail early if we have a non-nominal type today (which to me means at least
metatypes, which is this case).

I included a test case that we crash on without my change today and just do not
optimize.

rdar://87989767
This commit is contained in:
Michael Gottesman
2022-01-26 15:09:45 -08:00
parent 359ee9b7ac
commit 52eb3b5612
2 changed files with 23 additions and 1 deletions

View File

@@ -1547,7 +1547,7 @@ static bool optimizeStaticallyKnownProtocolConformance(
auto &Ctx = Mod.getASTContext();
auto *SM = Mod.getSwiftModule();
auto Proto = dyn_cast<ProtocolDecl>(TargetType->getAnyNominal());
auto *Proto = dyn_cast_or_null<ProtocolDecl>(TargetType->getAnyNominal());
if (!Proto)
return false;