Skip to content

[SE-0364] Handle retroactive conformance for types and protocols from underlying modules #71302

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
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
14 changes: 14 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ class SourceFile final : public FileUnit {
/// This is \c None until it is filled in by the import resolution phase.
llvm::Optional<ArrayRef<AttributedImport<ImportedModule>>> Imports;

/// The underlying clang module, if imported in this file.
ModuleDecl *ImportedUnderlyingModule = nullptr;

/// Which imports have made use of @preconcurrency.
llvm::SmallDenseSet<AttributedImport<ImportedModule>>
PreconcurrencyImportsUsed;
Expand Down Expand Up @@ -693,6 +696,17 @@ class SourceFile final : public FileUnit {
return isScriptMode() || hasMainDecl();
}

ModuleDecl *getUnderlyingModuleIfOverlay() const override {
return ImportedUnderlyingModule;
}

const clang::Module *getUnderlyingClangModule() const override {
Comment on lines +699 to +703
Copy link
Contributor

Choose a reason for hiding this comment

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

I was a little confused at first, but it looks like these are base class methods used by the existing diagnostic, so overriding them changes its behavior. Nice and clean. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, there are existing ModuleDecl methods that call these methods that I've overridden, and I think it was an oversight that those ModuleDecl methods were not functional on the module instance representing the compiled module for the job.

if (!ImportedUnderlyingModule)
return nullptr;

return ImportedUnderlyingModule->findUnderlyingClangModule();
}

/// Get the root refinement context for the file. The root context may be
/// null if the context hierarchy has not been built yet. Use
/// TypeChecker::getOrBuildTypeRefinementContext() to get a built
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,20 @@ void
SourceFile::setImports(ArrayRef<AttributedImport<ImportedModule>> imports) {
assert(!Imports && "Already computed imports");
Imports = getASTContext().AllocateCopy(imports);

// Find and cache the import of the underlying module, if present.
auto parentModuleName = getParentModule()->getName();
for (auto import : imports) {
if (!import.options.contains(ImportFlags::Exported))
continue;

auto importedModule = import.module.importedModule;
if (importedModule->getName() == parentModuleName &&
importedModule->findUnderlyingClangModule()) {
ImportedUnderlyingModule = import.module.importedModule;
break;
}
}
Comment on lines +2569 to +2582
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than looping over the imports this way, you could modify ImportResolver::getModule() to save off the underlying clang module in a new member variable of ImportResolver (there's a separate code path to look it up—look for the use of getClangModuleLoader()) and then have performImportResolution() set that on the SourceFile immediately before/after calling setImports(). Would that be a cleaner solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does seem like a cleaner solution because it keeps the logic for identifying the underlying clang module more centralized. In the interest of time I think I'm going to land this solution first, but I will follow it up with what you've suggested - thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented here: #71330

}

bool SourceFile::hasImportUsedPreconcurrency(
Expand Down
2 changes: 0 additions & 2 deletions lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,6 @@ swift::dependencies::checkImportNotTautological(const ImportPath::Module moduleP
filename, modulePath.front().Item);

return false;

return false;
}

void SwiftDependencyTracker::addCommonSearchPathDeps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public func ~=(x: NSObject, y: NSObject) -> Bool {
return true
}

extension NSObject : @retroactive Equatable, @retroactive Hashable {
extension NSObject : Equatable, Hashable {
public static func == (lhs: NSObject, rhs: NSObject) -> Bool {
return lhs.isEqual(rhs)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -swift-version 5 %t/OtherLibrary.swift -emit-module -module-name OtherLibrary -o %t
// RUN: %target-swift-frontend -typecheck %t/Library.swift -module-name Library -verify -swift-version 5 -import-underlying-module -I %t
// RUN: %target-swift-frontend -typecheck %t/Library.swift -module-name Library -verify -swift-version 5 -DEXPLICIT_IMPORT -I %t

// REQUIRES: objc_interop

//--- module.modulemap

module Library {
header "Library.h"
}

//--- Library.h

@import Foundation;

@interface UnderlyingLibraryClass : NSObject
@end

@protocol UnderlyingLibraryProtocol
@end

//--- OtherLibrary.swift

public class OtherLibraryClass {}
public protocol OtherLibraryProtocol {}

//--- Library.swift

#if EXPLICIT_IMPORT
@_exported import Library
#endif
import OtherLibrary

public class LibraryClass {}
public protocol LibraryProtocol {}

extension LibraryClass: UnderlyingLibraryProtocol {}
extension LibraryClass: LibraryProtocol {}
extension LibraryClass: OtherLibraryProtocol {}
extension UnderlyingLibraryClass: OtherLibraryProtocol {}
extension UnderlyingLibraryClass: LibraryProtocol {}
extension UnderlyingLibraryClass: UnderlyingLibraryProtocol {}
extension OtherLibraryClass: UnderlyingLibraryProtocol {}
extension OtherLibraryClass: LibraryProtocol {}
extension OtherLibraryClass: OtherLibraryProtocol {} // expected-warning {{extension declares a conformance of imported type 'OtherLibraryClass' to imported protocol 'OtherLibraryProtocol'}}
// expected-note @-1 {{add '@retroactive' to silence this warning}} {{30-50=@retroactive OtherLibraryProtocol}}