Skip to content

Deserializer: better error message if types of referenced functions don't match #71612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ ERROR(function_type_mismatch,none,
NOTE(function_declared_here,none,
"function declared here", ())

ERROR(deserialize_function_type_mismatch,Fatal,
"type mismatch of function '%0', declared as %1 but used in a swift module as %2",
(StringRef, Type, Type))

// Capture before declaration diagnostics.
ERROR(capture_before_declaration,none,
"closure captures %0 before it is declared", (Identifier))
Expand Down
35 changes: 28 additions & 7 deletions lib/Serialization/DeserializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "SILFormat.h"
#include "SILSerializationFunctionBuilder.h"

#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
Expand Down Expand Up @@ -392,7 +393,8 @@ SILDeserializer::getSILDifferentiabilityWitnessForReference(

/// Helper function to find a SILFunction, given its name and type.
SILFunction *SILDeserializer::getFuncForReference(StringRef name,
SILType type) {
SILType type,
TypeExpansionContext context) {
// Check to see if we have a function by this name already.
SILFunction *fn = SILMod.lookUpFunction(name);
if (!fn) {
Expand All @@ -410,11 +412,27 @@ SILFunction *SILDeserializer::getFuncForReference(StringRef name,
}
}

// FIXME: check for matching types.

// At this point, if fn is set, we know that we have a good function to use.
if (fn)
if (fn) {
SILType fnType = fn->getLoweredTypeInContext(context);
if (fnType != type &&
// It can happen that opaque return types cause a mismatch when merging
// modules, without causing any further assertion failures.
// TODO: fix the underlying problem
fnType.hasOpaqueArchetype() == type.hasOpaqueArchetype()) {
StringRef fnName = fn->getName();
if (auto *dc = fn->getDeclContext()) {
if (auto *decl = dyn_cast_or_null<AbstractFunctionDecl>(dc->getAsDecl()))
fnName = decl->getNameStr();
}
fn->getModule().getASTContext().Diags.diagnose(
fn->getLocation().getSourceLoc(),
diag::deserialize_function_type_mismatch,
fnName, fnType.getASTType(), type.getASTType());
exit(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something we can do other than exit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find anything better. We need to abort here immediately to not run into subsequent asserts/crashes.
I'm open to suggestions :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe llvm::report_fatal_error()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That also let it look like a compiler crash

}
return fn;
}

// Otherwise, create a function declaration with the right type and a bogus
// source location. This ensures that we can at least parse the rest of the
Expand Down Expand Up @@ -1969,7 +1987,8 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
ResultInst = Builder.createFunctionRef(
Loc,
getFuncForReference(
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr)));
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr),
Builder.getTypeExpansionContext()));
break;
}
case SILInstructionKind::DynamicFunctionRefInst: {
Expand All @@ -1978,7 +1997,8 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
ResultInst = Builder.createDynamicFunctionRef(
Loc,
getFuncForReference(
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr)));
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr),
Builder.getTypeExpansionContext()));
break;
}
case SILInstructionKind::PreviousDynamicFunctionRefInst: {
Expand All @@ -1987,7 +2007,8 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
ResultInst = Builder.createPreviousDynamicFunctionRef(
Loc,
getFuncForReference(
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr)));
FuncName, getSILType(Ty, (SILValueCategory)TyCategory, nullptr),
Builder.getTypeExpansionContext()));
break;
}
case SILInstructionKind::MarkDependenceInst: {
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialization/DeserializeSIL.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ namespace swift {
SILDifferentiabilityWitness *
getSILDifferentiabilityWitnessForReference(StringRef mangledKey);

SILFunction *getFuncForReference(StringRef Name, SILType Ty);
SILFunction *getFuncForReference(StringRef Name, SILType Ty, TypeExpansionContext context);
SILFunction *getFuncForReference(StringRef Name);
SILVTable *readVTable(serialization::DeclID);
SILMoveOnlyDeinit *readMoveOnlyDeinit(serialization::DeclID);
Expand Down