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.
This commit is contained in:
Erik Eckstein
2025-11-21 11:47:46 +01:00
parent 95d5b5da33
commit 9d02917087
2 changed files with 25 additions and 2 deletions

View File

@@ -142,6 +142,10 @@ 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))
ERROR(deserialize_function_linkage_mismatch,Fatal,
"linkage mismatch of function '%0', declared as %1 but expected as %2",
(StringRef, StringRef, StringRef))
ERROR(without_actually_escaping_on_isolated_any,none,
"withoutActuallyEscaping is currently unimplemented for '@isolated(any)' "
"function values", ())

View File

@@ -10,7 +10,9 @@
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Demangling/Demangle.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SIL/SILModule.h"
@@ -158,8 +160,25 @@ linkEmbeddedRuntimeFunctionByName(#NAME, EFFECT, StringRef(#CC) == "C_CC"); \
if (auto *Fn = M.lookUpFunction(name, byAsmName)) return Fn;
SILFunction *Fn =
M.getSILLoader()->lookupSILFunction(name, Linkage, byAsmName);
if (!Fn) return nullptr;
M.getSILLoader()->lookupSILFunction(name, Linkage,byAsmName);
if (!Fn) {
SILFunction *fnWithWrongLinkage =
M.getSILLoader()->lookupSILFunction(name, {}, byAsmName);
if (fnWithWrongLinkage) {
auto fnName = Demangle::demangleSymbolAsString(name,
Demangle::DemangleOptions::SimplifiedUIDemangleOptions());
auto &diags = getModule()->getASTContext().Diags;
diags.diagnose(fnWithWrongLinkage->getLocation().getSourceLoc(),
diag::deserialize_function_linkage_mismatch,
fnName, getLinkageString(fnWithWrongLinkage->getLinkage()),
getLinkageString(*Linkage));
diags.flushConsumers();
exit(1);
}
return nullptr;
}
if (M.linkFunction(Fn, LinkMode))
invalidateAnalysis(Fn, SILAnalysis::InvalidationKind::Everything);