Skip to content

Commit fbacdea

Browse files
committed
[Serialization] Show the side-effect of a failure as main diagnostic
Use the diagnostic of the effect on the API as main diagnostic about deserialization failures. For example: Mod.swiftmodule:1:1: error: could not deserialize type for 'foo()' Mod.swiftmodule:1:1: note: reference to type 'MyType' broken by a context change; 'MyType' was expected to be in 'A', but now a candidate is found only in 'B'
1 parent af88a65 commit fbacdea

File tree

5 files changed

+57
-47
lines changed

5 files changed

+57
-47
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,12 +922,12 @@ NOTE(modularization_issue_related_modules,none,
922922
"clang preprocessor macros may affect headers shared between these modules",
923923
(bool, DeclName))
924924

925-
NOTE(modularization_issue_side_effect_extension_error,none,
926-
"could not deserialize extension",
927-
())
928-
NOTE(modularization_issue_side_effect_type_error,none,
929-
"could not deserialize type for %0",
930-
(DeclName))
925+
ERROR(modularization_issue_side_effect_extension_error,none,
926+
"could not deserialize extension",
927+
())
928+
ERROR(modularization_issue_side_effect_type_error,none,
929+
"could not deserialize type for %0",
930+
(DeclName))
931931

932932
NOTE(modularization_issue_worked_around,none,
933933
"attempting forced recovery enabled by "

lib/Serialization/Deserialization.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -338,41 +338,52 @@ ModularizationError::diagnose(const ModuleFile *MF,
338338
}
339339
}
340340

341-
void TypeError::diagnose(const ModuleFile *MF) const {
342-
MF->getContext().Diags.diagnose(MF->getSourceLoc(),
343-
diag::modularization_issue_side_effect_type_error,
344-
name);
341+
void TypeError::diagnose(const ModuleFile *MF,
342+
DiagnosticBehavior limit) const {
343+
auto &diags = MF->getContext().Diags;
344+
auto inFlight = diags.diagnose(MF->getSourceLoc(),
345+
diag::modularization_issue_side_effect_type_error,
346+
name);
347+
inFlight.limitBehavior(limit);
345348
}
346349

347-
void ExtensionError::diagnose(const ModuleFile *MF) const {
348-
MF->getContext().Diags.diagnose(MF->getSourceLoc(),
350+
void ExtensionError::diagnose(const ModuleFile *MF,
351+
DiagnosticBehavior limit) const {
352+
auto &diags = MF->getContext().Diags;
353+
auto inFlight = diags.diagnose(MF->getSourceLoc(),
349354
diag::modularization_issue_side_effect_extension_error);
355+
inFlight.limitBehavior(limit);
350356
}
351357

352358
llvm::Error
353359
ModuleFile::diagnoseModularizationError(llvm::Error error,
354360
DiagnosticBehavior limit) const {
355-
auto handleModularizationError =
361+
auto handleModularizationCause =
356362
[&](const ModularizationError &modularError) -> llvm::Error {
357-
modularError.diagnose(this, limit);
363+
modularError.diagnose(this, DiagnosticBehavior::Note);
358364
return llvm::Error::success();
359365
};
360366
llvm::Error outError = llvm::handleErrors(std::move(error),
361-
handleModularizationError,
362-
[&](TypeError &typeError) -> llvm::Error {
363-
if (typeError.diagnoseUnderlyingReason(handleModularizationError)) {
364-
typeError.diagnose(this);
365-
return llvm::Error::success();
366-
}
367-
return llvm::make_error<TypeError>(std::move(typeError));
368-
},
369-
[&](ExtensionError &extError) -> llvm::Error {
370-
if (extError.diagnoseUnderlyingReason(handleModularizationError)) {
371-
extError.diagnose(this);
372-
return llvm::Error::success();
373-
}
374-
return llvm::make_error<ExtensionError>(std::move(extError));
375-
});
367+
[&](const ModularizationError &modularError) -> llvm::Error {
368+
modularError.diagnose(this, limit);
369+
return llvm::Error::success();
370+
},
371+
[&](TypeError &typeError) -> llvm::Error {
372+
if (typeError.shouldDiagnoseUnderlyingReason(handleModularizationCause)) {
373+
typeError.diagnose(this, limit);
374+
typeError.diagnoseUnderlyingReason(handleModularizationCause);
375+
return llvm::Error::success();
376+
}
377+
return llvm::make_error<TypeError>(std::move(typeError));
378+
},
379+
[&](ExtensionError &extError) -> llvm::Error {
380+
if (extError.shouldDiagnoseUnderlyingReason(handleModularizationCause)) {
381+
extError.diagnose(this, limit);
382+
extError.diagnoseUnderlyingReason(handleModularizationCause);
383+
return llvm::Error::success();
384+
}
385+
return llvm::make_error<ExtensionError>(std::move(extError));
386+
});
376387

377388
return outError;
378389
}

lib/Serialization/DeserializationErrors.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -461,23 +461,22 @@ class ErrorWithUnderlyingReason {
461461
}
462462
}
463463

464-
// Returns \c true if the error was diagnosed.
465464
template <typename HandlerT>
466-
bool diagnoseUnderlyingReason(HandlerT &&handler) {
465+
bool shouldDiagnoseUnderlyingReason(HandlerT &&handler) {
466+
return underlyingReason &&
467+
llvm::ErrorHandlerTraits<HandlerT>::appliesTo(*underlyingReason);
468+
}
469+
470+
template <typename HandlerT>
471+
void diagnoseUnderlyingReason(HandlerT &&handler) {
467472
if (underlyingReason &&
468473
llvm::ErrorHandlerTraits<HandlerT>::appliesTo(*underlyingReason)) {
469474
auto error = llvm::ErrorHandlerTraits<HandlerT>::apply(
470475
std::forward<HandlerT>(handler),
471476
std::move(underlyingReason));
472-
if (!error) {
473-
// The underlying reason was diagnosed.
474-
return true;
475-
} else {
477+
if (error)
476478
underlyingReason = takeErrorInfo(std::move(error));
477-
return false;
478-
}
479479
}
480-
return false;
481480
}
482481
};
483482

@@ -496,7 +495,7 @@ class TypeError : public llvm::ErrorInfo<TypeError, DeclDeserializationError>,
496495
this->numVTableEntries = numVTableEntries;
497496
}
498497

499-
void diagnose(const ModuleFile *MF) const;
498+
void diagnose(const ModuleFile *MF, DiagnosticBehavior limit) const;
500499

501500
void log(raw_ostream &OS) const override {
502501
OS << "Could not deserialize type for '" << name << "'";
@@ -518,7 +517,7 @@ class ExtensionError : public llvm::ErrorInfo<ExtensionError>,
518517
explicit ExtensionError(std::unique_ptr<ErrorInfoBase> reason)
519518
: ErrorWithUnderlyingReason(std::move(reason)) {}
520519

521-
void diagnose(const ModuleFile *MF) const;
520+
void diagnose(const ModuleFile *MF, DiagnosticBehavior limit) const;
522521

523522
void log(raw_ostream &OS) const override {
524523
OS << "could not deserialize extension";

test/Serialization/Recovery/module-recovery-remarks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
// RUN: | %FileCheck --check-prefixes CHECK-MOVED %s
1515

1616
/// 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'
17+
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: remark: could not deserialize type for 'foo()'
18+
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: note: 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'
1819

1920
/// Contextual notes about the modules involved.
2021
// CHECK-MOVED: <unknown>:0: note: the type was expected to be found in module 'A' at '
@@ -33,7 +34,6 @@
3334
// CHECK-MOVED: <unknown>:0: note: declarations in the underlying clang module 'A' may be hidden by clang preprocessor macros
3435
// CHECK-MOVED: <unknown>:0: note: the distributed module 'LibWithXRef' refers to the local module 'A'; this may be caused by header maps or search paths
3536
// CHECK-MOVED: <unknown>:0: note: the type 'MyType' moved between related modules; clang preprocessor macros may affect headers shared between these modules
36-
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: note: could not deserialize type for 'foo()'
3737
// CHECK-MOVED: error: cannot find 'foo' in scope
3838

3939
/// Move A to the SDK, triggering a different note about layering.

test/Serialization/modularization-error.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// RUN: %target-swift-frontend %t/LibOriginal.swift -emit-module-path %t/B.swiftmodule -module-name B
1313
// RUN: not %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t 2>&1 \
1414
// RUN: | %FileCheck --check-prefixes CHECK,CHECK-MOVED %s
15-
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; 'MyType' was expected to be in 'A', but now a candidate is found only in 'B'
15+
// CHECK: LibWithXRef.swiftmodule:1:1: error: could not deserialize extension
16+
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: note: reference to type 'MyType' broken by a context change; 'MyType' was expected to be in 'A', but now a candidate is found only in 'B'
1617

1718
/// Force working around the broken modularization to get a result and no errors.
1819
// RUN: %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t \
@@ -33,23 +34,22 @@
3334
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/B.swiftmodule -module-name B
3435
// RUN: not %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t 2>&1 \
3536
// RUN: | %FileCheck --check-prefixes CHECK,CHECK-KIND-CHANGED %s
36-
// CHECK-KIND-CHANGED: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' from 'A' changed since building 'LibWithXRef'
37+
// CHECK-KIND-CHANGED: LibWithXRef.swiftmodule:1:1: note: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' from 'A' changed since building 'LibWithXRef'
3738

3839
/// Change MyType into a function and move it.
3940
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/A.swiftmodule -module-name A
4041
// RUN: %target-swift-frontend %t/LibTypeChanged.swift -emit-module-path %t/B.swiftmodule -module-name B
4142
// RUN: not %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t 2>&1 \
4243
// RUN: | %FileCheck --check-prefixes CHECK,CHECK-KIND-CHANGED-AND-MOVED %s
43-
// CHECK-KIND-CHANGED-AND-MOVED: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' changed since building 'LibWithXRef', it was in 'A' and is now a candidate is found only in 'B'
44+
// CHECK-KIND-CHANGED-AND-MOVED: LibWithXRef.swiftmodule:1:1: note: reference to type 'MyType' broken by a context change; the declaration kind of 'MyType' changed since building 'LibWithXRef', it was in 'A' and is now a candidate is found only in 'B'
4445

4546
/// Remove MyType from all imported modules.
4647
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/A.swiftmodule -module-name A
4748
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/B.swiftmodule -module-name B
4849
// RUN: not %target-swift-frontend -emit-sil %t/LibWithXRef.swiftmodule -module-name LibWithXRef -I %t 2>&1 \
4950
// RUN: | %FileCheck --check-prefixes CHECK,CHECK-NOT-FOUND %s
50-
// CHECK-NOT-FOUND: LibWithXRef.swiftmodule:1:1: error: reference to type 'MyType' broken by a context change; 'MyType' is not found, it was expected to be in 'A'
51+
// CHECK-NOT-FOUND: LibWithXRef.swiftmodule:1:1: note: reference to type 'MyType' broken by a context change; 'MyType' is not found, it was expected to be in 'A'
5152

52-
// CHECK: LibWithXRef.swiftmodule:1:1: note: could not deserialize extension
5353

5454
//--- Empty.swift
5555
//--- LibOriginal.swift

0 commit comments

Comments
 (0)