-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[ClangImporter] Add direct access for import-as-member types. #10956
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
Changes from all commits
03e1e3b
925f291
bc460a2
ac567ac
3d42828
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1319,9 +1319,7 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) { | |
&blobData); | ||
switch (recordID) { | ||
case XREF_TYPE_PATH_PIECE: { | ||
if (values.size() == 1) { | ||
ModuleDecl *module = values.front()->getModuleContext(); | ||
|
||
if (values.size() == 1 && isa<NominalTypeDecl>(values.front())) { | ||
// Fast path for nested types that avoids deserializing all | ||
// members of the parent type. | ||
IdentifierID IID; | ||
|
@@ -1334,29 +1332,23 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) { | |
"If you're seeing a crash here, try passing " | ||
"-Xfrontend -disable-serialization-nested-type-lookup-table"}; | ||
|
||
auto *baseType = cast<NominalTypeDecl>(values.front()); | ||
TypeDecl *nestedType = nullptr; | ||
if (onlyInNominal) { | ||
// Only look in the file containing the type itself. | ||
const DeclContext *dc = values.front()->getDeclContext(); | ||
auto *serializedFile = | ||
dyn_cast<SerializedASTFile>(dc->getModuleScopeContext()); | ||
if (serializedFile) { | ||
nestedType = | ||
serializedFile->File.lookupNestedType(memberName, | ||
values.front()); | ||
auto *containingFile = | ||
dyn_cast<FileUnit>(dc->getModuleScopeContext()); | ||
if (containingFile) { | ||
nestedType = containingFile->lookupNestedType(memberName, baseType); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can any of this logic be factored out into a separate function with early returns? Perhaps return when the nested type is actually found? I'm having a hard time understanding the logic here. I realized that refactoring this method is probably outside the scope of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would certainly be reasonable; |
||
} else { | ||
// Fault in extensions, then ask every serialized AST in the module. | ||
(void)cast<NominalTypeDecl>(values.front())->getExtensions(); | ||
for (FileUnit *file : module->getFiles()) { | ||
(void)baseType->getExtensions(); | ||
for (FileUnit *file : baseType->getModuleContext()->getFiles()) { | ||
if (file == getFile()) | ||
continue; | ||
auto *serializedFile = dyn_cast<SerializedASTFile>(file); | ||
if (!serializedFile) | ||
continue; | ||
nestedType = | ||
serializedFile->File.lookupNestedType(memberName, | ||
values.front()); | ||
nestedType = file->lookupNestedType(memberName, baseType); | ||
if (nestedType) | ||
break; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
struct IAMOuter { int x; }; | ||
struct IAMSOuter { int x; }; | ||
|
||
struct IAMInner { int y; }; | ||
struct IAMSInner { int y; }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// The order of these forward-declarations affects whether there was a bug. | ||
struct IAMOuter; | ||
struct IAMInner; | ||
struct IAMSOuter; | ||
struct IAMSInner; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Name: ImportAsMemberSubmodules | ||
Tags: | ||
- Name: IAMInner | ||
SwiftName: IAMOuter.Inner | ||
- Name: IAMSInner | ||
SwiftName: IAMSOuter.Inner |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -F %S/Inputs/frameworks %s -verify | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -F %S/Inputs/frameworks -I %S/Inputs/custom-modules %s -verify | ||
|
||
import ImportAsMember | ||
import ImportAsMemberSubmodules | ||
|
||
let _: IAMOuter.Inner? | ||
let _: IAMSOuter.Inner? | ||
let _: IAMMultipleNested.Inner? // expected-error {{ambiguous type name 'Inner' in 'IAMMultipleNested'}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
struct Outer { | ||
int value; | ||
}; | ||
|
||
struct __attribute__((swift_name("Outer.InterestingValue"))) Inner { | ||
int value; | ||
}; | ||
|
||
#if __OBJC__ | ||
|
||
@import Foundation; | ||
|
||
extern NSString * const ErrorCodeDomain; | ||
enum __attribute__((ns_error_domain(ErrorCodeDomain))) ErrorCodeEnum { | ||
ErrorCodeEnumA | ||
}; | ||
|
||
#endif // __OBJC__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module NestedClangTypes { | ||
header "NestedClangTypes.h" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t -I %S/Inputs/xref-nested-clang-type/ %s -module-name Lib | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %t -I %S/Inputs/xref-nested-clang-type/ %s -DCLIENT -verify | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: cp %S/Inputs/xref-nested-clang-type/NestedClangTypes.h %t | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t -import-objc-header %t/NestedClangTypes.h %s -module-name Lib -DNO_MODULE | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %t -import-objc-header %t/NestedClangTypes.h %s -DCLIENT -verify | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: cp %S/Inputs/xref-nested-clang-type/NestedClangTypes.h %t | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t -import-objc-header %t/NestedClangTypes.h -pch-output-dir %t/PCHCache %s -module-name Lib -DNO_MODULE | ||
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %t -pch-output-dir %t/PCHCache -import-objc-header %t/NestedClangTypes.h %s -DCLIENT -verify | ||
|
||
#if CLIENT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this PR is about fast paths, then should you be testing the value of the stats like NumNestedTypeShortcuts? Or is this a correctness test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a correctness test in this case; master will crash with circular dependencies without the "fast path". Maybe I should call it a "direct path". |
||
|
||
import Lib | ||
|
||
func test(x: MyInner) {} | ||
|
||
#if _runtime(_ObjC) | ||
import Foundation | ||
func test(x: MyCode) {} | ||
#endif // ObjC | ||
|
||
#else // CLIENT | ||
|
||
#if !NO_MODULE | ||
import NestedClangTypes | ||
#endif | ||
|
||
public typealias MyOuter = Outer | ||
public typealias MyInner = Outer.InterestingValue | ||
|
||
extension MyOuter { | ||
public func use(inner: MyInner) {} | ||
} | ||
|
||
#if _runtime(_ObjC) | ||
import Foundation | ||
|
||
public typealias MyCode = ErrorCodeEnum.Code | ||
extension ErrorCodeEnum { | ||
public func whatever(code: MyCode) {} | ||
} | ||
#endif // ObjC | ||
|
||
#endif // CLIENT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used as a failable fast path? That is, if this succeeds we're fast and if it returns nullptr we go through slower lookups?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct. It could probably be non-failable but since the eventual goal is to remove it altogether…no reason not to be cautious.