Skip to content

Commit 3243f21

Browse files
authored
Make sure all failures to load module interfaces are diagnosed (#25636)
...specifically, diagnosed in the parent DiagnosticEngine. This not only provides a better user experience, but makes sure that the compiler exits with a nonzero exit code even if the module goes unused. rdar://problem/50789839
1 parent de1c0cd commit 3243f21

File tree

9 files changed

+42
-4
lines changed

9 files changed

+42
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ ERROR(sema_opening_import,Fatal,
628628
"opening import file for module %0: %1", (Identifier, StringRef))
629629

630630
ERROR(serialization_load_failed,Fatal,
631-
"failed to load module %0", (Identifier))
631+
"failed to load module '%0'", (StringRef))
632632
ERROR(serialization_malformed_module,Fatal,
633633
"malformed compiled module: %0", (StringRef))
634634
ERROR(serialization_module_too_new,Fatal,

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/AST/FileSystem.h"
1919
#include "swift/AST/Module.h"
2020
#include "swift/AST/ProtocolConformance.h"
21+
#include "swift/Basic/Defer.h"
2122
#include "swift/Basic/Lazy.h"
2223
#include "swift/Basic/Platform.h"
2324
#include "swift/Basic/STLExtras.h"
@@ -564,6 +565,17 @@ class swift::ParseableInterfaceBuilder {
564565

565566
SubInstance.createDependencyTracker(FEOpts.TrackSystemDeps);
566567

568+
SWIFT_DEFER {
569+
// Make sure to emit a generic top-level error if a module fails to
570+
// load. This is not only good for users; it also makes sure that we've
571+
// emitted an error in the parent diagnostic engine, which is what
572+
// determines whether the process exits with a proper failure status.
573+
if (SubInstance.getASTContext().hadError()) {
574+
diags.diagnose(diagnosticLoc, diag::serialization_load_failed,
575+
moduleName);
576+
}
577+
};
578+
567579
if (SubInstance.setup(subInvocation)) {
568580
SubError = true;
569581
return;

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,8 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
720720
case serialization::Status::FailedToLoadBridgingHeader:
721721
// We already emitted a diagnostic about the bridging header. Just emit
722722
// a generic message here.
723-
Ctx.Diags.diagnose(diagLoc, diag::serialization_load_failed, ModuleName);
723+
Ctx.Diags.diagnose(diagLoc, diag::serialization_load_failed,
724+
ModuleName.str());
724725
break;
725726

726727
case serialization::Status::NameMismatch: {

test/ParseableInterface/ModuleCache/module-cache-diagnostics.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
// RUN: %{python} %S/Inputs/check-is-old.py %t/modulecache/OtherModule-*.swiftmodule %t/modulecache/LeafModule-*.swiftmodule
5858
// RUN: %FileCheck %s -check-prefix=CHECK-ERROR <%t/err.txt
5959
// CHECK-ERROR: LeafModule.swiftinterface:7:8: error: no such module 'NotAModule'
60-
// CHECK-ERROR: OtherModule.swiftinterface:4:8: error: no such module 'LeafModule'
60+
// CHECK-ERROR: OtherModule.swiftinterface:4:8: error: failed to load module 'LeafModule'
61+
// CHECK-ERROR: module-cache-diagnostics.swift:{{[0-9]+}}:8: error: failed to load module 'OtherModule'
62+
// CHECK-ERROR-NOT: error:
6163
//
6264
//
6365
// Next test: same as above, but with a .dia file
@@ -80,7 +82,9 @@
8082
// RUN: %{python} %S/Inputs/check-is-old.py %t/modulecache/OtherModule-*.swiftmodule %t/modulecache/LeafModule-*.swiftmodule
8183
// RUN: %FileCheck %s -check-prefix=CHECK-ERROR-INLINE <%t/err-inline.txt
8284
// CHECK-ERROR-INLINE: LeafModule.swiftinterface:6:33: error: use of unresolved identifier 'unresolved'
83-
// CHECK-ERROR-INLINE: OtherModule.swiftinterface:4:8: error: no such module 'LeafModule'
85+
// CHECK-ERROR-INLINE: OtherModule.swiftinterface:4:8: error: failed to load module 'LeafModule'
86+
// CHECK-ERROR-INLINE: module-cache-diagnostics.swift:{{[0-9]+}}:8: error: failed to load module 'OtherModule'
87+
// CHECK-ERROR-INLINE-NOT: error:
8488
//
8589
//
8690
// Next test: same as above, but with a .dia file

validation-test/ParseableInterface/Inputs/failing-overlay/HasOverlay.h

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags:
3+
4+
garbage
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <HasOverlay.h>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module HasOverlay {
2+
header "HasOverlay.h"
3+
}
4+
5+
module ImportsOverlay {
6+
header "ImportsOverlay.h"
7+
export *
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: not %target-swift-frontend -I %S/Inputs/failing-overlay/ -typecheck %s 2>&1 | %FileCheck %s
3+
4+
import ImportsOverlay
5+
6+
// FIXME: It would be better if this had a useful location, like inside the
7+
// C header that caused the importing of the overlay.
8+
// CHECK: <unknown>:0: error: failed to load module 'HasOverlay'

0 commit comments

Comments
 (0)