Skip to content

Serialization: Report deserialization remarks from lookup services #79092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ ModuleFile::loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
} else {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(mem.takeError());
consumeError(mem.takeError());
diagnoseAndConsumeError(mem.takeError());
}
}
}
Expand Down Expand Up @@ -860,7 +860,7 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}

Expand All @@ -878,7 +878,7 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}

Expand All @@ -902,7 +902,7 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}

Expand Down Expand Up @@ -975,7 +975,7 @@ void ModuleFile::lookupObjCMethods(
// Deserialize the method and add it to the list.
auto declOrError = getDeclChecked(std::get<2>(result));
if (!declOrError) {
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}

Expand All @@ -1002,13 +1002,16 @@ void ModuleFile::getTopLevelDecls(
if (declOrError.errorIsA<DeclAttributesDidNotMatch>()) {
// Decl rejected by matchAttributes, ignore it.
assert(matchAttributes);
consumeError(declOrError.takeError());

// We don't diagnose DeclAttributesDidNotMatch at the moment but
// let's use the diagnose consume variant for consistency.
diagnoseAndConsumeError(declOrError.takeError());
continue;
}

if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}
if (!ABIRoleInfo(declOrError.get()).providesAPI()) // FIXME: flags
Expand All @@ -1024,7 +1027,7 @@ void ModuleFile::getExportedPrespecializations(
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
diagnoseAndConsumeError(declOrError.takeError());
continue;
}
results.push_back(declOrError.get());
Expand Down
29 changes: 23 additions & 6 deletions test/Serialization/Recovery/module-recovery-remarks.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/sdk)
// RUN: split-file %s %t
// RUN: split-file %s %t --leading-lines

/// Compile two library modules A and A_related, and a middle library LibWithXRef with a reference to a type in A.
// RUN: %target-swift-frontend %t/LibOriginal.swift -emit-module-path %t/A.swiftmodule -module-name A -I %t
// RUN: %target-swift-frontend %t/Empty.swift -emit-module-path %t/A_related.swiftmodule -module-name A_related
// 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

/// Move MyType from A to A_related, triggering most notes.
/// Move BrokenType from A to A_related, triggering most notes.
// RUN: %target-swift-frontend %t/EmptyOverlay.swift -emit-module-path %t/A.swiftmodule -module-name A -I %t
// RUN: %target-swift-frontend %t/LibOriginal.swift -emit-module-path %t/A_related.swiftmodule -module-name A_related -I %t
// 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 \
// RUN: | %FileCheck --check-prefixes CHECK-MOVED %s

/// Main error downgraded to a remark.
// 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'
// CHECK-MOVED: LibWithXRef.swiftmodule:1:1: remark: reference to type 'BrokenType' broken by a context change; 'BrokenType' was expected to be in 'A', but now a candidate is found only in 'A_related'

/// Contextual notes about the modules involved.
// CHECK-MOVED: note: the type was expected to be found in module 'A' at '
Expand All @@ -32,10 +32,16 @@
// CHECK-MOVED-SAME: LibWithXRef.swiftmodule'
// CHECK-MOVED: note: declarations in the underlying clang module 'A' may be hidden by clang preprocessor macros
// CHECK-MOVED: note: the distributed module 'LibWithXRef' refers to the local module 'A'; this may be caused by header maps or search paths
// CHECK-MOVED: note: the type 'MyType' moved between related modules; clang preprocessor macros may affect headers shared between these modules
// CHECK-MOVED: note: the type 'BrokenType' moved between related modules; clang preprocessor macros may affect headers shared between these modules
// CHECK-MOVED: note: could not deserialize type for 'foo()'
// CHECK-MOVED: error: cannot find 'foo' in scope

// CHECK-MOVED: remark: reference to type 'BrokenType' broken by a context change; 'BrokenType' was expected to be in 'A'
// CHECK-MOVED: note: could not deserialize type for 'init(t:)'

// CHECK-MOVED: remark: reference to type 'BrokenType' broken by a context change; 'BrokenType' was expected to be in 'A'
// CHECK-MOVED: note: could not deserialize type for 'member()'

/// Move A to the SDK, triggering a different note about layering.
// RUN: mv %t/A.swiftmodule %t/sdk/A.swiftmodule
// RUN: not %target-swift-frontend -c -O %t/Client.swift -I %t -I %t/sdk -Rmodule-recovery -sdk %t/sdk 2>&1 \
Expand Down Expand Up @@ -68,19 +74,30 @@ void foo() {}
//--- LibOriginal.swift
@_exported import A

public struct MyType {
public struct BrokenType {
public init() {}
}

//--- LibWithXRef.swift
import A
import A_related

public func foo() -> MyType {
public func foo() -> BrokenType {
fatalError()
}

public class StableType {
public init() {}
public convenience init(t: BrokenType) { self.init() }
public func member() -> BrokenType { fatalError() }
}

//--- Client.swift
import LibWithXRef

foo()

let s = StableType()
s.member()

let s2 = StableType(42)