Skip to content

Commit 4f66fcf

Browse files
committed
[Serialization] Intro -Rmodule-recovery to remark about silent errors
Deserialization recovery silently drops errors and the affected decls. This can lead to surprises when a function from an imported module simply disappears without an explanation. This commit introduces the flag -Rmodule-recovery to report as remarks some of these previously silently dropped issues. It can be used to debug project configuration issues.
1 parent 65b5f82 commit 4f66fcf

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ namespace swift {
238238
/// Emit a remark after loading a module.
239239
bool EnableModuleLoadingRemarks = false;
240240

241+
/// Emit remarks about contextual inconsistencies in loaded modules.
242+
bool EnableModuleRecoveryRemarks = false;
243+
241244
/// Emit a remark when indexing a system module.
242245
bool EnableIndexingSystemModuleRemarks = false;
243246

include/swift/Option/Options.td

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@ def emit_cross_import_remarks : Flag<["-"], "Rcross-import">,
388388

389389
def remark_loading_module : Flag<["-"], "Rmodule-loading">,
390390
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
391-
HelpText<"Emit a remark and file path of each loaded module">;
391+
HelpText<"Emit remarks about loaded module">;
392+
def remark_module_recovery : Flag<["-"], "Rmodule-recovery">,
393+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
394+
HelpText<"Emit remarks about contextual inconsistencies in loaded modules">;
392395

393396
def remark_indexing_system_module : Flag<["-"], "Rindexing-system-module">,
394397
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
929929
Opts.EnableCrossImportRemarks = Args.hasArg(OPT_emit_cross_import_remarks);
930930

931931
Opts.EnableModuleLoadingRemarks = Args.hasArg(OPT_remark_loading_module);
932+
Opts.EnableModuleRecoveryRemarks = Args.hasArg(OPT_remark_module_recovery);
932933

933934
Opts.EnableIndexingSystemModuleRemarks = Args.hasArg(OPT_remark_indexing_system_module);
934935

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7384,6 +7384,14 @@ llvm::Error ModuleFile::consumeExpectedError(llvm::Error &&error) {
73847384
}
73857385

73867386
void ModuleFile::diagnoseAndConsumeError(llvm::Error error) const {
7387+
auto &ctx = getContext();
7388+
if (ctx.LangOpts.EnableModuleRecoveryRemarks) {
7389+
error = diagnoseModularizationError(std::move(error),
7390+
DiagnosticBehavior::Remark);
7391+
// If error was already diagnosed it was also consumed.
7392+
if (!error)
7393+
return;
7394+
}
73877395

73887396
consumeError(std::move(error));
73897397
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/sdk)
3+
// RUN: split-file %s %t
4+
5+
/// Compile two library modules A and A_related, and a middle library LibWithXRef with a reference to a type in A.
6+
// RUN: %target-swift-frontend %t/LibOriginal.swift -emit-module-path %t/A.swiftmodule -module-name A -I %t
7+
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/A_related.swiftmodule -module-name A_related
8+
// RUN: %target-swift-frontend %t/LibWithXRef.swift -emit-module-path %t/sdk/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t -swift-version 5 -enable-library-evolution
9+
10+
/// Move MyType from A to A_related, triggering most notes.
11+
// RUN: %target-swift-frontend %t/EmptyOverlay.swift -emit-module-path %t/A.swiftmodule -module-name A -I %t
12+
// RUN: %target-swift-frontend %t/LibOriginal.swift -emit-module-path %t/A_related.swiftmodule -module-name A_related -I %t
13+
// RUN: not %target-swift-frontend -c -O %t/Client.swift -I %t -I %t/sdk -Rmodule-recovery -sdk %t/sdk -swift-version 4 2>&1 \
14+
// RUN: | %FileCheck --check-prefixes CHECK-MOVED %s
15+
16+
/// Main error downgraded to a remark.
17+
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: remark: reference to type 'MyType' broken by a context change; 'MyType' was expected to be in 'A', but now a candidate is found only in 'A_related'
18+
//--- module.modulemap
19+
module A {
20+
header "A.h"
21+
}
22+
23+
//--- A.h
24+
void foo() {}
25+
26+
//--- Empty.swift
27+
28+
//--- EmptyOverlay.swift
29+
@_exported import A
30+
31+
//--- LibOriginal.swift
32+
@_exported import A
33+
34+
public struct MyType {
35+
public init() {}
36+
}
37+
38+
//--- LibWithXRef.swift
39+
import A
40+
import A_related
41+
42+
public func foo() -> MyType {
43+
fatalError()
44+
}
45+
46+
//--- Client.swift
47+
import LibWithXRef
48+
49+
foo()

0 commit comments

Comments
 (0)