Skip to content

[Serialization] Recover from failures under AnyObjectLookup #41647

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 1 commit into from
Mar 4, 2022
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
34 changes: 31 additions & 3 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,15 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
// one.
if (name.isSimpleName()) {
for (auto item : *iter) {
auto vd = cast<ValueDecl>(getDecl(item.second));
auto declOrError = getDeclChecked(item.second);
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
continue;
}

auto vd = cast<ValueDecl>(declOrError.get());
auto dc = vd->getDeclContext();
while (!dc->getParent()->isModuleScopeContext())
dc = dc->getParent();
Expand All @@ -784,7 +792,15 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
}
} else {
for (auto item : *iter) {
auto vd = cast<ValueDecl>(getDecl(item.second));
auto declOrError = getDeclChecked(item.second);
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
continue;
}
Comment on lines +795 to +801
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this block in... quite a few places now. Should we abstract it into another method? Probably fine to do in a follow up either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use an alternative to getDeclChecked that drops the error. It could clean up these call sites a bit and we could use it to apply more precise filtering of the errors, to drop only XRef/implementation-only errors for example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me!


auto vd = cast<ValueDecl>(declOrError.get());
if (!vd->getName().matchesRef(name))
continue;

Expand All @@ -800,7 +816,15 @@ void ModuleFile::lookupClassMember(ImportPath::Access accessPath,
}

for (auto item : *iter) {
auto vd = cast<ValueDecl>(getDecl(item.second));
auto declOrError = getDeclChecked(item.second);
if (!declOrError) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(declOrError.takeError());
consumeError(declOrError.takeError());
continue;
}

auto vd = cast<ValueDecl>(declOrError.get());
results.push_back(vd);
}
}
Expand All @@ -818,6 +842,8 @@ void ModuleFile::lookupClassMembers(ImportPath::Access accessPath,
for (auto item : list) {
auto decl = getDeclChecked(item.second);
if (!decl) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(decl.takeError());
llvm::consumeError(decl.takeError());
continue;
}
Expand All @@ -839,6 +865,8 @@ void ModuleFile::lookupClassMembers(ImportPath::Access accessPath,
for (auto item : list) {
auto decl = getDeclChecked(item.second);
if (!decl) {
if (!getContext().LangOpts.EnableDeserializationRecovery)
fatal(decl.takeError());
llvm::consumeError(decl.takeError());
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#import <Foundation.h>

@interface ImplOnlyBase: NSObject @end
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ module Typedefs { header "Typedefs.h" }
module TypeRemovalObjC { header "TypeRemovalObjC.h" }
module Types { header "Types.h" }
module Conformance { header "Conformance.h" }
module AnyObjectLookup { header "AnyObjectLookup.h" }
29 changes: 29 additions & 0 deletions test/Serialization/Recovery/anyobject-lookup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// Recover from searching though all possible methods on AnyObject.
/// rdar://89494507

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -DLIB_B %s -module-name B -emit-module-path %t/B.swiftmodule -I %t -I %S/Inputs/custom-modules -enable-library-evolution -swift-version 5
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -DLIB_C %s -I %t -verify
// Add this flag to the previous line to get back the original crash: -disable-deserialization-recovery

// REQUIRES: objc_interop

#if LIB_B

import Foundation
@_implementationOnly import AnyObjectLookup

@objc internal class InternalClass: ImplOnlyBase {
@objc func invisibleMethod() {
}
}

#elseif LIB_C

import B

func use(_ obj: AnyObject) {
obj.invisibleMethod() // expected-error {{value of type 'AnyObject' has no member 'invisibleMethod'}}
}

#endif