Serialization: Skip patterns with invalid types.

Prevents a crash during module emission with `-enable-lazy-typechecking` when
the type of a `Pattern` cannot be resolved.
This commit is contained in:
Allan Shortlidge
2024-02-19 18:32:28 -08:00
parent 65ea9c8313
commit b46d179d46
4 changed files with 46 additions and 4 deletions

View File

@@ -3659,27 +3659,37 @@ private:
}
case PatternKind::Named: {
auto named = cast<NamedPattern>(pattern);
auto ty = getPatternType();
if (S.skipTypeIfInvalid(ty, named->getLoc()))
break;
unsigned abbrCode = S.DeclTypeAbbrCodes[NamedPatternLayout::Code];
NamedPatternLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
S.addDeclRef(named->getDecl()),
S.addTypeRef(getPatternType()));
S.addTypeRef(ty));
break;
}
case PatternKind::Any: {
auto ty = getPatternType();
if (S.skipTypeIfInvalid(ty, pattern->getLoc()))
break;
unsigned abbrCode = S.DeclTypeAbbrCodes[AnyPatternLayout::Code];
auto anyPattern = cast<AnyPattern>(pattern);
AnyPatternLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
S.addTypeRef(getPatternType()),
S.addTypeRef(ty),
anyPattern->isAsyncLet());
break;
}
case PatternKind::Typed: {
auto typed = cast<TypedPattern>(pattern);
auto ty = getPatternType();
if (S.skipTypeIfInvalid(ty, typed->getTypeRepr()))
break;
unsigned abbrCode = S.DeclTypeAbbrCodes[TypedPatternLayout::Code];
TypedPatternLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
S.addTypeRef(getPatternType()));
S.addTypeRef(ty));
writePattern(typed->getSubPattern());
break;
}
@@ -6813,7 +6823,7 @@ bool Serializer::skipDeclIfInvalid(const Decl *decl) {
}
bool Serializer::skipTypeIfInvalid(Type ty, TypeRepr *tyRepr) {
if (ty || allowCompilerErrors())
if ((ty && !ty->hasError()) || allowCompilerErrors())
return false;
if (Options.EnableSerializationRemarks) {
@@ -6825,6 +6835,19 @@ bool Serializer::skipTypeIfInvalid(Type ty, TypeRepr *tyRepr) {
return true;
}
bool Serializer::skipTypeIfInvalid(Type ty, SourceLoc loc) {
if ((ty && !ty->hasError()) || allowCompilerErrors())
return false;
if (Options.EnableSerializationRemarks) {
getASTContext().Diags.diagnose(
loc, diag::serialization_skipped_invalid_type_unknown_name);
}
hadError = true;
return true;
}
void serialization::writeToStream(
raw_ostream &os, ModuleOrSourceFile DC, const SILModule *M,
const SerializationOptions &options,