Skip to content

Commit 9d02917

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 95d5b5d commit 9d02917

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
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 %1 but 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", ())

lib/SILOptimizer/UtilityPasses/Link.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/AST/DiagnosticsSIL.h"
1314
#include "swift/AST/ProtocolConformance.h"
15+
#include "swift/Demangling/Demangle.h"
1416
#include "swift/SILOptimizer/PassManager/Passes.h"
1517
#include "swift/SILOptimizer/PassManager/Transforms.h"
1618
#include "swift/SIL/SILModule.h"
@@ -158,8 +160,25 @@ linkEmbeddedRuntimeFunctionByName(#NAME, EFFECT, StringRef(#CC) == "C_CC"); \
158160
if (auto *Fn = M.lookUpFunction(name, byAsmName)) return Fn;
159161

160162
SILFunction *Fn =
161-
M.getSILLoader()->lookupSILFunction(name, Linkage, byAsmName);
162-
if (!Fn) return nullptr;
163+
M.getSILLoader()->lookupSILFunction(name, Linkage,byAsmName);
164+
165+
if (!Fn) {
166+
SILFunction *fnWithWrongLinkage =
167+
M.getSILLoader()->lookupSILFunction(name, {}, byAsmName);
168+
169+
if (fnWithWrongLinkage) {
170+
auto fnName = Demangle::demangleSymbolAsString(name,
171+
Demangle::DemangleOptions::SimplifiedUIDemangleOptions());
172+
auto &diags = getModule()->getASTContext().Diags;
173+
diags.diagnose(fnWithWrongLinkage->getLocation().getSourceLoc(),
174+
diag::deserialize_function_linkage_mismatch,
175+
fnName, getLinkageString(fnWithWrongLinkage->getLinkage()),
176+
getLinkageString(*Linkage));
177+
diags.flushConsumers();
178+
exit(1);
179+
}
180+
return nullptr;
181+
}
163182

164183
if (M.linkFunction(Fn, LinkMode))
165184
invalidateAnalysis(Fn, SILAnalysis::InvalidationKind::Everything);

0 commit comments

Comments
 (0)