Skip to content

Commit c2545ad

Browse files
committed
SIL: make a linkage mismatch during de-serialization a human readable diagnostic
Although this error can only happen when tinkering with the stdlib's runtime functions, it's nice to get a readable error message. So far, the compiler just crashed later without a hint to the original problem.
1 parent 62b7fe5 commit c2545ad

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ ERROR(deserialize_function_type_mismatch,Fatal,
142142
"type mismatch of function '%0', declared as %1 but used in a swift module as %2",
143143
(StringRef, Type, Type))
144144

145+
ERROR(deserialize_function_linkage_mismatch,Fatal,
146+
"linkage mismatch of function '%0', declared as %1but expected as %2",
147+
(StringRef, StringRef, StringRef))
148+
145149
ERROR(without_actually_escaping_on_isolated_any,none,
146150
"withoutActuallyEscaping is currently unimplemented for '@isolated(any)' "
147151
"function values", ())

include/swift/SIL/SILLinkage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ inline bool fixmeWitnessHasLinkageThatNeedsToBePublic(SILDeclRef witness,
421421
(!hasSharedVisibility(witnessLinkage) || !witness.isSerialized());
422422
}
423423

424+
// Defined in SILPrinter
425+
StringRef getLinkageString(SILLinkage linkage);
426+
424427
} // end swift namespace
425428

426429
#endif

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3505,7 +3505,7 @@ void SILFunction::dump(const char *FileName) const {
35053505
print(os);
35063506
}
35073507

3508-
static StringRef getLinkageString(SILLinkage linkage) {
3508+
StringRef swift::getLinkageString(SILLinkage linkage) {
35093509
switch (linkage) {
35103510
case SILLinkage::Public: return "public ";
35113511
case SILLinkage::PublicNonABI: return "non_abi ";

lib/Serialization/SerializedSILLoader.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "ModuleFile.h"
1717

1818
#include "swift/AST/ASTMangler.h"
19+
#include "swift/AST/DiagnosticsSIL.h"
20+
#include "swift/Demangling/Demangle.h"
1921
#include "swift/SIL/SILModule.h"
2022
#include "swift/SIL/SILMoveOnlyDeinit.h"
2123
#include "swift/Serialization/SerializedModuleLoader.h"
@@ -72,13 +74,16 @@ SerializedSILLoader::lookupSILFunction(StringRef Name,
7274
if (Linkage) {
7375
// This is not the linkage we are looking for.
7476
if (Func->getLinkage() != *Linkage) {
75-
LLVM_DEBUG(llvm::dbgs()
76-
<< "Wrong linkage for Function: "
77-
<< Func->getName() << " : "
78-
<< (int)Func->getLinkage() << "\n");
79-
Des->invalidateFunction(Func);
80-
Func->getModule().eraseFunction(Func);
81-
continue;
77+
78+
auto fnName = Demangle::demangleSymbolAsString(Func->getName(),
79+
Demangle::DemangleOptions::SimplifiedUIDemangleOptions());
80+
auto &diags = Func->getModule().getASTContext().Diags;
81+
diags.diagnose(Func->getLocation().getSourceLoc(),
82+
diag::deserialize_function_linkage_mismatch,
83+
fnName, getLinkageString(Func->getLinkage()),
84+
getLinkageString(*Linkage));
85+
diags.flushConsumers();
86+
exit(1);
8287
}
8388
}
8489
return Func;

0 commit comments

Comments
 (0)