Skip to content

Commit 48fdd58

Browse files
Merge branch 'master' of https://github.com/apple/swift into SR-11295-warning-unecessary-casts
2 parents 3dc82a6 + 2f34df9 commit 48fdd58

File tree

19 files changed

+426
-342
lines changed

19 files changed

+426
-342
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.
33

44
<!-- If this pull request resolves any bugs in the Swift bug tracker, provide a link: -->
5-
Resolves [SR-NNNN](https://bugs.swift.org/browse/SR-NNNN).
5+
Resolves SR-NNNN.
66

77
<!--
88
Before merging this pull request, you must run the Swift continuous integration tests.

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ class SerializedModuleLoaderBase : public ModuleLoader {
9494
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
9595

9696
void
97-
openModuleSourceInfoFileIfPresent(AccessPathElem ModuleID,
98-
StringRef ModulePath,
99-
StringRef ModuleSourceInfoFileName,
97+
openModuleSourceInfoFileIfPresent(
98+
AccessPathElem ModuleID,
99+
StringRef ModulePath,
100+
StringRef ModuleSourceInfoFileName,
100101
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer);
101102

102103
/// If the module loader subclass knows that all options have been tried for

lib/ClangImporter/ClangImporter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ bool ClangImporter::importHeader(StringRef header, ModuleDecl *adapter,
13541354

13551355
// If we've made it to here, this is some header other than the bridging
13561356
// header, which means we can no longer rely on one file's modification time
1357-
// to invalid code completion caches. :-(
1357+
// to invalidate code completion caches. :-(
13581358
Impl.setSinglePCHImport(None);
13591359

13601360
if (!cachedContents.empty() && cachedContents.back() == '\0')
@@ -3702,8 +3702,16 @@ EffectiveClangContext ClangImporter::Implementation::getEffectiveClangContext(
37023702
(nominal->getAttrs().hasAttribute<ObjCAttr>() ||
37033703
(!nominal->getParentSourceFile() && nominal->isObjC()))) {
37043704
// Map the name. If we can't represent the Swift name in Clang.
3705-
// FIXME: We should be using the Objective-C name here!
3706-
auto clangName = exportName(nominal->getName());
3705+
Identifier name = nominal->getName();
3706+
if (auto objcAttr = nominal->getAttrs().getAttribute<ObjCAttr>()) {
3707+
if (auto objcName = objcAttr->getName()) {
3708+
if (objcName->getNumArgs() == 0) {
3709+
// This is an error if not 0, but it should be caught later.
3710+
name = objcName->getSimpleName();
3711+
}
3712+
}
3713+
}
3714+
auto clangName = exportName(name);
37073715
if (!clangName)
37083716
return EffectiveClangContext();
37093717

lib/ClangImporter/ImportDecl.cpp

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4535,24 +4535,64 @@ namespace {
45354535
}
45364536

45374537
template <typename T, typename U>
4538-
T *resolveSwiftDeclImpl(const U *decl, Identifier name, ModuleDecl *overlay) {
4538+
T *resolveSwiftDeclImpl(const U *decl, Identifier name,
4539+
bool hasKnownSwiftName, ModuleDecl *overlay) {
45394540
const auto &languageVersion =
45404541
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
45414542

4543+
auto isMatch = [&](const T *singleResult, bool baseNameMatches) -> bool {
4544+
const DeclAttributes &attrs = singleResult->getAttrs();
4545+
4546+
// Skip versioned variants.
4547+
if (attrs.isUnavailableInSwiftVersion(languageVersion))
4548+
return false;
4549+
4550+
// Skip if type not exposed to Objective-C.
4551+
// If the base name doesn't match, then a matching
4552+
// custom name in an @objc attribute is required.
4553+
if (baseNameMatches && !singleResult->isObjC())
4554+
return false;
4555+
4556+
// If Clang decl has a custom Swift name, then we know that
4557+
// `name` is the base name we're looking for.
4558+
if (hasKnownSwiftName)
4559+
return baseNameMatches;
4560+
4561+
// Skip if a different name is used for Objective-C.
4562+
if (auto objcAttr = attrs.getAttribute<ObjCAttr>())
4563+
if (auto objcName = objcAttr->getName())
4564+
return objcName->getSimpleName() == name;
4565+
4566+
return baseNameMatches;
4567+
};
4568+
4569+
// First look at Swift types with the same name.
45424570
SmallVector<ValueDecl *, 4> results;
45434571
overlay->lookupValue(name, NLKind::QualifiedLookup, results);
45444572
T *found = nullptr;
45454573
for (auto result : results) {
45464574
if (auto singleResult = dyn_cast<T>(result)) {
4547-
// Skip versioned variants.
4548-
const DeclAttributes &attrs = singleResult->getAttrs();
4549-
if (attrs.isUnavailableInSwiftVersion(languageVersion))
4550-
continue;
4551-
4552-
if (found)
4553-
return nullptr;
4575+
if (isMatch(singleResult, /*baseNameMatches=*/true)) {
4576+
if (found)
4577+
return nullptr;
4578+
found = singleResult;
4579+
}
4580+
}
4581+
}
45544582

4555-
found = singleResult;
4583+
if (!found && !hasKnownSwiftName) {
4584+
// Try harder to find a match looking at just custom Objective-C names.
4585+
SmallVector<Decl *, 64> results;
4586+
overlay->getTopLevelDecls(results);
4587+
for (auto result : results) {
4588+
if (auto singleResult = dyn_cast<T>(result)) {
4589+
// The base name _could_ match but it's irrelevant here.
4590+
if (isMatch(singleResult, /*baseNameMatches=*/false)) {
4591+
if (found)
4592+
return nullptr;
4593+
found = singleResult;
4594+
}
4595+
}
45564596
}
45574597
}
45584598

@@ -4565,15 +4605,16 @@ namespace {
45654605

45664606
template <typename T, typename U>
45674607
T *resolveSwiftDecl(const U *decl, Identifier name,
4568-
ClangModuleUnit *clangModule) {
4608+
bool hasKnownSwiftName, ClangModuleUnit *clangModule) {
45694609
if (auto overlay = clangModule->getOverlayModule())
4570-
return resolveSwiftDeclImpl<T>(decl, name, overlay);
4610+
return resolveSwiftDeclImpl<T>(decl, name, hasKnownSwiftName, overlay);
45714611
if (clangModule == Impl.ImportedHeaderUnit) {
45724612
// Use an index-based loop because new owners can come in as we're
45734613
// iterating.
45744614
for (size_t i = 0; i < Impl.ImportedHeaderOwners.size(); ++i) {
45754615
ModuleDecl *owner = Impl.ImportedHeaderOwners[i];
4576-
if (T *result = resolveSwiftDeclImpl<T>(decl, name, owner))
4616+
if (T *result = resolveSwiftDeclImpl<T>(decl, name,
4617+
hasKnownSwiftName, owner))
45774618
return result;
45784619
}
45794620
}
@@ -4586,7 +4627,8 @@ namespace {
45864627
if (!importer::hasNativeSwiftDecl(decl))
45874628
return false;
45884629
auto wrapperUnit = cast<ClangModuleUnit>(dc->getModuleScopeContext());
4589-
swiftDecl = resolveSwiftDecl<T>(decl, name, wrapperUnit);
4630+
swiftDecl = resolveSwiftDecl<T>(decl, name, /*hasCustomSwiftName=*/true,
4631+
wrapperUnit);
45904632
return true;
45914633
}
45924634

@@ -4615,13 +4657,15 @@ namespace {
46154657
*correctSwiftName);
46164658

46174659
Identifier name = importedName.getDeclName().getBaseIdentifier();
4660+
bool hasKnownSwiftName = importedName.hasCustomName();
46184661

46194662
// FIXME: Figure out how to deal with incomplete protocols, since that
46204663
// notion doesn't exist in Swift.
46214664
if (!decl->hasDefinition()) {
46224665
// Check if this protocol is implemented in its overlay.
46234666
if (auto clangModule = Impl.getClangModuleForDecl(decl, true))
46244667
if (auto native = resolveSwiftDecl<ProtocolDecl>(decl, name,
4668+
hasKnownSwiftName,
46254669
clangModule))
46264670
return native;
46274671

@@ -4733,11 +4777,13 @@ namespace {
47334777
*correctSwiftName);
47344778

47354779
auto name = importedName.getDeclName().getBaseIdentifier();
4780+
bool hasKnownSwiftName = importedName.hasCustomName();
47364781

47374782
if (!decl->hasDefinition()) {
47384783
// Check if this class is implemented in its overlay.
47394784
if (auto clangModule = Impl.getClangModuleForDecl(decl, true)) {
47404785
if (auto native = resolveSwiftDecl<ClassDecl>(decl, name,
4786+
hasKnownSwiftName,
47414787
clangModule)) {
47424788
return native;
47434789
}

lib/ClangImporter/ImportName.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,9 +1781,11 @@ ImportedName NameImporter::importName(const clang::NamedDecl *decl,
17811781
ImportNameVersion version,
17821782
clang::DeclarationName givenName) {
17831783
CacheKeyType key(decl, version);
1784-
if (importNameCache.count(key) && !givenName) {
1785-
++ImportNameNumCacheHits;
1786-
return importNameCache[key];
1784+
if (!givenName) {
1785+
if (auto cachedRes = importNameCache[key]) {
1786+
++ImportNameNumCacheHits;
1787+
return cachedRes;
1788+
}
17871789
}
17881790
++ImportNameNumCacheMisses;
17891791
auto res = importNameImpl(decl, version, givenName);

lib/Parse/ParsedSyntaxRecorder.cpp.gyb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ ParsedSyntaxRecorder::make${node.syntax_kind}(
146146
for (auto &element : elements) {
147147
layout.push_back(element.takeRaw());
148148
}
149-
#ifndef NDEBUG
150-
ParsedRawSyntaxRecorder::verifyElementRanges(layout);
151-
#endif
152149
if (SPCtx.shouldDefer())
153150
return defer${node.syntax_kind}(layout, SPCtx);
154151
return record${node.syntax_kind}(layout, SPCtx.getRecorder());

0 commit comments

Comments
 (0)